You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 连接 MySQL ,语法:$ mysql -h host -u user -p# 这里是:$ mysql -u root -p# 创建数据库:# 这样可以确保所有表和列在默认情况下都将使用UTF-8:
mysql> CREATE DATABASE <dbname> CHARACTER SET utf8;
# 建议:创建数据库,设置 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci:
mysql> CREATE DATABASE database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# 如果执行 $ python manage.py makemigrations 生成迁移文件时,报错如下:
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
第一步: 打开 base.py 文件 ==> 搜索 version = Database.version_info ==> 在 if 语句中放入 pass 并注释原来的命令行输出 ==> 保存文件:
version=Database.version_infoifversion< (1, 3, 13):
# raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)pass# 修改为pass by Fatli 20191215
deflast_executed_query(self, cursor, sql, params):
# With MySQLdb, cursor objects have an (undocumented) "_executed"# attribute where the exact query sent to the database is saved.# See MySQLdb/cursors.py in the source distribution.query=getattr(cursor, '_executed', None)
ifqueryisnotNone:
# query = query.decode(errors='replace')query=query.encode(errors='replace') # 修改 by Fatli 20191215returnquery
# DATABASES设置为'read_default_file': './my.cnf'后报错:# django.db.utils.OperationalError: (1045, "Access denied for user 'fatli'@'localhost' (using password: NO)")# 解决方案(无效,依然报错如上):# https://stackoverflow.com/questions/10299148/mysql-error-1045-28000-access-denied-for-user-billlocalhost-using-passw
mysql>SELECT USER(), CURRENT_USER();
+----------------+----------------+
| USER() | CURRENT_USER() |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+1 row inset (0.00 sec)
mysql> SHOW GRANTS FOR 'fatli'@'localhost';
ERROR 1141 (42000): There is no such grant defined for user 'fatli'on host 'localhost'
mysql> SHOW GRANTS FOR 'root'@'localhost';
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON*.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+2 rows inset (0.00 sec)
# 给 fatli 权限(依然报同样的错)
mysql>GRANT ALL PRIVILEGES ON*.* TO 'fatli'@'localhost' IDENTIFIED BY 'Syy31=root' WITH GRANT OPTION;
环境:
参考:
一、为 Django 项目创建数据库
二、安装 pymysql 驱动
三、配置 Django 项目的数据库设置
四、运行
附:pymysql(mysqlclient)版本报错解决方案
此处此时(2019.12)因为安装的是 pymysql 0.9.3 ,不支持 Django 2.2 ,解决方案参考:https://stackoverflow.com/questions/55657752/django-installing-mysqlclient-error-mysqlclient-1-3-13-or-newer-is-required
注意:
2020年07月18日发布的 pymysql 0.10.0 似乎解决了以上问题,所以不再需要修改以下的 base.py 文件。但是,执行
python manage.py migrate
,依然会报错:query = query.decode(errors='replace') AttributeError: 'str' object has no attribute 'decode'
,所以依然需要修改以下的 operations.py 文件。实例:
第一步: 打开 base.py 文件 ==> 搜索
version = Database.version_info
==> 在if
语句中放入pass
并注释原来的命令行输出 ==> 保存文件:第二步: 打开 operations.py 文件 ==> 搜索
query = query.decode(errors='replace')
==> change decode to encode ==> 保存文件:Now, try to run the server.
附:记录配置 mysqlclient 的一些坑,请忽略
$ conda install mysqlclient # 安装 mysqlclient 驱动程序(用以 Python驱动 MySQL)
The text was updated successfully, but these errors were encountered: