mysqlclientのinstallがうまくいかない

原因:mysqlがOSに2つ入ってしまっていることが原因っぽい

公式ページ経由でinstallした
/usr/local/mysql-8.0.29-macos12-arm64/bin

Homebrew経由でinstallしたmysql

この2つが競合してしまったためにmysqlclientのinstallがうまくいかなかったみたいです。

最終的にパスの設定などをいろいろ試しましたが結局2つのmysqlをuninstallして再度installするという方法を取りました。

以下やったこと

pip install mysqlclientができない

pip install mysqlclient
Collecting mysqlclient
  Downloading mysqlclient-2.2.4.tar.gz (90 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 90.4/90.4 kB 8.6 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [27 lines of output]
      Trying pkg-config --exists mysqlclient
      Command 'pkg-config --exists mysqlclient' returned non-zero exit status 1.
      Trying pkg-config --exists mariadb
      Command 'pkg-config --exists mariadb' returned non-zero exit status 1.
      Trying pkg-config --exists libmariadb
      Command 'pkg-config --exists libmariadb' returned non-zero exit status 1.
      Traceback (most recent call last):
        File "/Users/shirotabi/pyenv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/Users/shirotabi/pyenv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/Users/shirotabi/pyenv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "/private/var/folders/k_/533jgzwx087cszqmz50vvstw0000gn/T/pip-build-env-2bsimr27/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 327, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=[])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/private/var/folders/k_/533jgzwx087cszqmz50vvstw0000gn/T/pip-build-env-2bsimr27/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 297, in _get_build_requires
          self.run_setup()
        File "/private/var/folders/k_/533jgzwx087cszqmz50vvstw0000gn/T/pip-build-env-2bsimr27/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 313, in run_setup
          exec(code, locals())
        File "<string>", line 155, in <module>
        File "<string>", line 49, in get_config_posix
        File "<string>", line 28, in find_package_name
      Exception: Can not find valid pkg-config name.
      Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

MySQLがインストールされている場合でも、mysqlclient パッケージのビルドに必要なヘッダーファイルやライブラリが見つからないことが原因でインストールが失敗することがあるということで以下のコマンドを実行

export PATH="/usr/local/opt/mysql-client/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/mysql-client/lib"
export CPPFLAGS="-I/usr/local/opt/mysql-client/include"
export PKG_CONFIG_PATH="/usr/local/opt/mysql-client/lib/pkgconfig"
pip install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.2.4.tar.gz (90 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: SQLAlchemy in /Users/shirotabi/pyenv/lib/python3.12/site-packages (2.0.29)
Requirement already satisfied: typing-extensions>=4.6.0 in /Users/shirotabi/pyenv/lib/python3.12/site-packages (from SQLAlchemy) (4.10.0)
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for mysqlclient (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [44 lines of output]
      # Options for building extension module:
        extra_compile_args: ['-I/usr/local/opt/mysql-client/include', '-std=c99']
        extra_link_args: ['-L/usr/local/opt/mysql-client/lib', '-lmysqlclient']
        define_macros: [('version_info', (2, 2, 4, 'final', 0)), ('__version__', '2.2.4')]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.macosx-14.0-arm64-cpython-312
      creating build/lib.macosx-14.0-arm64-cpython-312/MySQLdb
      copying src/MySQLdb/release.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb
      copying src/MySQLdb/cursors.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb
      copying src/MySQLdb/connections.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb
      copying src/MySQLdb/__init__.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb
      copying src/MySQLdb/times.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb
      copying src/MySQLdb/converters.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb
      copying src/MySQLdb/_exceptions.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb
      creating build/lib.macosx-14.0-arm64-cpython-312/MySQLdb/constants
      copying src/MySQLdb/constants/FLAG.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb/constants
      copying src/MySQLdb/constants/CLIENT.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb/constants
      copying src/MySQLdb/constants/__init__.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb/constants
      copying src/MySQLdb/constants/ER.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb/constants
      copying src/MySQLdb/constants/CR.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb/constants
      copying src/MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb/constants
      running egg_info
      writing src/mysqlclient.egg-info/PKG-INFO
      writing dependency_links to src/mysqlclient.egg-info/dependency_links.txt
      writing top-level names to src/mysqlclient.egg-info/top_level.txt
      reading manifest file 'src/mysqlclient.egg-info/SOURCES.txt'
      reading manifest template 'MANIFEST.in'
      adding license file 'LICENSE'
      writing manifest file 'src/mysqlclient.egg-info/SOURCES.txt'
      copying src/MySQLdb/_mysql.c -> build/lib.macosx-14.0-arm64-cpython-312/MySQLdb
      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.macosx-14.0-arm64-cpython-312
      creating build/temp.macosx-14.0-arm64-cpython-312/src
      creating build/temp.macosx-14.0-arm64-cpython-312/src/MySQLdb
      clang -fno-strict-overflow -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk "-Dversion_info=(2, 2, 4, 'final', 0)" -D__version__=2.2.4 -I/Users/shirotabi/pyenv/include -I/opt/homebrew/opt/python@3.12/Frameworks/Python.framework/Versions/3.12/include/python3.12 -c src/MySQLdb/_mysql.c -o build/temp.macosx-14.0-arm64-cpython-312/src/MySQLdb/_mysql.o -I/usr/local/opt/mysql-client/include -std=c99
      src/MySQLdb/_mysql.c:29:10: fatal error: 'mysql.h' file not found
      #include  "mysql.h"
               ^~~~~~~~~
      1 error generated.
      error: command '/usr/bin/clang' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for mysqlclient
Failed to build mysqlclient
ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects 

エラーメッセージから見ると、mysql.h ファイルが見つからないためにビルドが失敗
じゃあどこやねんってことで探してみた。

find /usr -name "mysql.h"
/usr/local/mysql-8.0.29-macos12-arm64/include/mysql.h
find: /usr/local/mysql-8.0.29-macos12-arm64/keyring: Permission denied
find: /usr/local/mysql-8.0.29-macos12-arm64/data: Permission denied
/opt/homebrew/Cellar/mysql-client/8.3.0/include/mysql/mysql.h
/opt/homebrew/Cellar/mysql/8.3.0_1/include/mysql/mysql.h

ここで複数のmysqlが入ってしまっていることが判明

紆余曲折chatGPTに聞いたりして頑張ってみたものの解決せず
最終的に以下の方法で解決

# 既存のDBをすべてバックアップとして保存
mysqldump -u root -p --all-databases > all_databases_backup.sql

# 止める
sudo mysqladmin -u root -p shutdown

# homebrewと公式HP経由でinstallしたusr/localのmysqlを削除
brew uninstall mysql
brew uninstall mysql-client
sudo rm -rf /usr/local/mysql-8.0.29-macos12-arm64
sudo rm -rf /usr/local/mysql
sudo rm -rf /usr/local/var/mysql

# brewで再install
brew install mysql
brew install mysql-client

# 開始と初期設定
brew services start mysql
mysql_secure_installation

# HomebrewのMySQLクライアント設定
export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/mysql-client/lib"
export CPPFLAGS="-I/opt/homebrew/opt/mysql-client/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/mysql-client/lib/pkgconfig"

# Homebrewのパス追加(その他のライブラリ)
export PATH="/opt/homebrew/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/lib"
export CPPFLAGS="-I/opt/homebrew/include"
export PKG_CONFIG_PATH="/opt/homebrew/lib/pkgconfig"

source ~/.zshrc

brew services start mysql







この記事が気に入ったらサポートをしてみませんか?