mysql8使用mysqlclient报错解决

栏目: 数据库 · Mysql · 发布时间: 6年前

内容简介:在公司的时候搭建的项目正常运行,但回家后clone代码后安装依赖包后总报错:what? 然后我卸载安装了两遍还是不行,安装也没有报错。公司跟家里的系统都是macOS10.14.3,python包也都是通过pipenv安装的版本都是一样的,唯一不同的是mysq版本,家里的是8.0.12而公司是5.7。 那么只可能是mysql版本导致的,于是上网查找原因,首先找到一个是因为mysql8的用户认证加密方式不同导致连不上数据库,这应该跟我的情况不同,因为看报错明显是因为找不到果然还是不好使,后来找到

在公司的时候搭建的项目正常运行,但回家后clone代码后安装依赖包后总报错:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

what? 然后我卸载安装了两遍还是不行,安装也没有报错。公司跟家里的系统都是macOS10.14.3,python包也都是通过pipenv安装的版本都是一样的,唯一不同的是mysq版本,家里的是8.0.12而公司是5.7。 那么只可能是 mysql 版本导致的,于是上网查找原因,首先找到一个是因为mysql8的用户认证加密方式不同导致连不上数据库,这应该跟我的情况不同,因为看报错明显是因为找不到 mysqlclinet 包, 但还是得试一试,而且既然有这原因,肯定后面也会遇到。

-- 由caching_sha2_password加密方式改为mysql_native_password
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword';  
FLUSH PRIVILEGES;

果然还是不好使,后来找到 mysqlclient的github页面 按照其为macOS安装了 mysql-connector-c 也不好使,并且查看了 mysql_congfig 也没有其提到的问题。

后来只好自己查看了报错位置的代码:

try:
    import MySQLdb as Database
except ImportError as err:
    raise ImproperlyConfigured(
        'Error loading MySQLdb module.\n'
        'Did you install mysqlclient?'
    ) from err

我自己debug了这段代码发现在导入的时候报缺少了某个 .so ,正好我又查到了一条相关问题。链接地址如下

https://stackoverflow.com/questions/46902357/error-loading-mysqldb-module-did-you-install-mysqlclient-or-mysql-python/53589824

不是那条改用 pymysql 的回复,不过改用 pymsql 应该也好使,我也记录一下:

# 安装pymysql,在setting.py同级目录的__init__.py里加入以下两行代码

import pymysql
pymysql.install_as_MySQLdb()

对我有用的是这条回复:

I had this issue just recently even with using the python 3 compatible mysqlclient library and managed to solve my issue albeit in a bit of an unorthodox manner. If you are using MySQL 8, give this a try and see if it helps! :) I simply made a copy of the libmysqlclient.21.dylib file located in my up-to-date installation of MySQL 8.0.13 which is was in /usr/local/mysql/lib and moved that copy under the same name to /usr/lib. You will need to temporarily disable security integrity protection on your mac however to do this since you won’t have or be able to change permissions to anything in /usr/lib without disabling it. You can do this by booting up into the recovery system, click Utilities on the menu at the top, and open up the terminal and enter csrutil disable into the terminal. Just remember to turn security integrity protection back on when you’re done doing this! The only difference from the above process will be that you run csrutil enable instead. You can find out more about how to disable and enable macOS’s security integrity protection here.

回复中说启用mac的csrutil,然后把/usr/local/mysql/lib中的libmysqlclient.21.dylib拷贝到/usr/lib中。其实不用这么干,直接软连过去就可以

sudo ln -s /usr/local/mysql/lib/libmysqlclient.21.dylib /usr/lib/libmysqlclient.21.dylib

再运行django就没有报错了


以上所述就是小编给大家介绍的《mysql8使用mysqlclient报错解决》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

深度学习入门

深度学习入门

[ 日] 斋藤康毅 / 陆宇杰 / 人民邮电出版社 / 2018-7 / 59.00元

本书是深度学习真正意义上的入门书,深入浅出地剖析了深度学习的原理和相关技术。书中使用Python3,尽量不依赖外部库或工具,从基本的数学知识出发,带领读者从零创建一个经典的深度学习网络,使读者在此过程中逐步理解深度学习。书中不仅介绍了深度学习和神经网络的概念、特征等基础知识,对误差反向传播法、卷积神经网络等也有深入讲解,此外还介绍了深度学习相关的实用技巧,自动驾驶、图像生成、强化学习等方面的应用,......一起来看看 《深度学习入门》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具