mac中启动django项目mysqlclient报错
记录一次Mac中已安装mysqlclient的报错及解决
在安装mysqlclient时,或者已经安装后还是无法启动django项目
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
解决办法
- 使用homebrew安装mysql-client
brew install mysql-client@8.4
- 配置好环境变量
# 使用的是8.4版本的,需要修改mysql-client为mysql-client@8.4 echo 'export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"' >> ~/.zshrc echo 'export LDFLAGS="-L/opt/homebrew/opt/mysql-client/lib"' >> ~/.zshrc echo 'export CPPFLAGS="-I/opt/homebrew/opt/mysql-client/include"' >> ~/.zshrc source ~/.zshrc
出现认证方法不对,依旧无法连接mysql
- 出现错误
原因是MySQL客户端在尝试加载mysql_native_password插件时失败了,安装小于9.0版本的mysql-clietndjango.db.utils.OperationalError: (2059, "Authentication plugin 'mysql_native_password' cannot be loaded:dlopen(/opt/homebrew/Cellar/mysqlclient/9.0.1/lib/plugin/mysql_native_password.so, 0x0002): tried: '/opt/homebrew/Cellar/mysql-client/9.0.1/lib/plugin/mysql_native_password.so' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Cellar/mysql-client/9.0.1/lib/plugin/mysql_native_password.so' (no such file), '/opt/homebrew/Cellar/mysql-client/9.0.1/lib/plugin/mysql_native_password.so' (no such file)")
brew install mysql-client@8.4
pkg-config
- 如果在安装时出现
则是表明在构建mysqlclient时,无法找到有效的pkg-config,使用homebrew安装对应包Can not find valid pkg-config name. Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually [end of output]
brew install pkg-config
Homebrew太慢,brew换国内源,推荐清华源
在macOS上,您可以通过更改Homebrew的源来加速软件包的下载速度。以下是更改Homebrew源的步骤:
-
备份现有的Homebrew源:
首先,备份现有的Homebrew源,以防需要恢复:cd "$(brew --repo)" git remote -v
-
更改Homebrew的主仓库源:
将Homebrew的主仓库源更改为国内镜像源,例如清华大学的镜像源:git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
-
更改Homebrew Core仓库源:
将Homebrew Core仓库源更改为国内镜像源:git -C "$(brew --repo)/Library/Taps/homebrew/homebrew-core" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
-
更改Homebrew Cask仓库源:
将Homebrew Cask仓库源更改为国内镜像源:git -C "$(brew --repo)/Library/Taps/homebrew/homebrew-cask" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
-
更新Homebrew:
更改源后,更新Homebrew以应用更改:brew update
-
验证更改:
验证更改是否生效:git -C "$(brew --repo)" remote -v git -C "$(brew --repo)/Library/Taps/homebrew/homebrew-core" remote -v git -C "$(brew --repo)/Library/Taps/homebrew/homebrew-cask" remote -v
以上 lee分享