当サイトではアフィリエイト広告を利用しています。

PythonからMySQLに接続する環境を構築する方法【Windows11】

2025年11月4日

作業端末にPythonのインストールとMySQLのインストールができていることが前提の作業で、pipが使えることが条件です。

もし、pipが使えない場合は以下の記事をご覧ください。
下記の記事ではVS Codeで使えるようにする手順ですが、実質WindowsとしてPythonが使えるようにするためのトラブルシューティングの記事です!

PythonからMySQLに接続するための公式ライブラリをインストールします。
これを入れないと、PythonはMySQLに接続できません。

インストール手順

まずは、pipでMySQLコネクタをインストールします。

>pip install mysql-connector-python

出力結果に「Successfully installed mysql-connector-python-xxx」があれば大丈夫です!

出力結果例

Collecting mysql-connector-python
Downloading mysql_connector_python-9.5.0-cp313-cp313-win_amd64.whl.metadata (7.7 kB)
Downloading mysql_connector_python-9.5.0-cp313-cp313-win_amd64.whl (16.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16.5/16.5 MB 18.3 MB/s eta 0:00:00
Installing collected packages: mysql-connector-python
Successfully installed mysql-connector-python-9.5.0
(base) 

インストールが完了したら、確認として以下を実行します。

pip show mysql-connector-python

出力結果例

Name: mysql-connector-python
Version: 9.5.0
Summary: A self-contained Python driver for communicating with MySQL servers, using an API that is compliant with the Python Database API Specification v2.0 (PEP 249).
Home-page: https://dev.mysql.com/doc/connector-python/en/
Author: Oracle and/or its affiliates
Author-email:
License: GNU GPLv2 (with FOSS License Exception)
Location: C:\ProgramData\anaconda3\Lib\site-packages
Requires:
Required-by:

このような出力結果が出ていればOKです!

これで、PythonからMySQLにアクセスするためのライブラリを入れることができました!