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报错解决》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

深度学习轻松学

深度学习轻松学

冯超 / 电子工业出版社 / 2017-7 / 79.00

《深度学习轻松学:核心算法与视觉实践》介绍了深度学习基本算法和视觉领域的应用实例。书中以轻松直白的语言,生动详细地介绍了深层模型相关的基础知识,并深入剖析了算法的原理与本质。同时,书中还配有大量案例与源码,帮助读者切实体会深度学习的核心思想和精妙之处。除此之外,书中还介绍了深度学习在视觉领域的应用,从原理层面揭示其思路思想,帮助读者在此领域中夯实技术基础。 《深度学习轻松学:核心算法与视觉实......一起来看看 《深度学习轻松学》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具