Django ---- 更改默认数据库mysql

Django 更改默认数据库mysql

1、将 setting.py 的设置文件里数据库配置:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

更改为:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', #固定配置
        'HOST': '127.0.0.1',#mysql地址
        'PORT': '3306',#端口号
        'NAME': 'guest',#库名(组名)
        'USER': 'root',#用户
        'PASSWORD': '123456',#密码
        'OPTIONS': {'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", },
    }
}

2、执行迁移数据库

python manage.py makemigrations  # 生成迁移文件

python manage.py migrate   # 迁移文件

3、报错处理
错误 1

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

解决方法:
1、安装pymysql

pip install pymysql

2、安装完毕以后,打开__init__.py 添加:

import pymysql

pymysql.install_as_MySQLdb()

错误 2:

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3

解决方法:
django2.2内部的一个版本限制在作怪。

1.修复源码
按照文中配置,报错django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
原因:django2.2和pymysql版本不匹配。mysqldb不支持python3.


1raise 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.  
解决办法:
C:\Python37\Lib\site-packages\django\db\backends\mysql(python安装目录)打开base.py,注释掉以下内容:        
if version < (1, 3, 13):           raise ImproperlyConfigured(‘mysqlclient 1.3.13 or newer is required; you have %s.% Database.version)   


2、File “C:\Python37\lib\site-packages\django\db\backends\mysql\operations.py”, line 146, in last_executed_query   
query = query.decode(errors=‘replace’)   
AttributeError:strobject has no attribute ‘decode’   
# 解决办法:
# 打开此文件把146行的decode修改为encode

这样就能正常迁移数据库了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值