进入 mysql 数据库
在数据库中添加如下代码:
create database xxxx(你需要的库名) charset=utf8;
create user xxx(你需要的用户名) identified by '123456';
grant all on xxxx(你需要的库名).* to 'itcast'@'%';
flush privileges;
在配置文件中:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # 数据库引擎 'HOST': '127.0.0.1', # 数据库主机 'PORT': 3306, # 数据库端口 'USER': 'xxx', # 数据库用户名 'PASSWORD': '123456', # 数据库用户密码 'NAME': 'xxxx' # 数据库名字 }, }
运行测试
如果是在虚拟环境下 发生错误
安装mysqlclient==1.4.6
# 进入虚拟环境 pip install mysqlclient==1.4.6 -i https://pypi.tuna.tsinghua.edu.cn/simple/
解决mysqlclient安装出错的问题
1. 更换ubuntu中默认的源为国内的源:提升软件下载速度 2. 更新apt-get的源和升级 3. 安装libmysqlclient-dev:因为mysqlclient依赖这个软件 4. 虚拟环境中安装mysqlclient==1.4.6 5. 测试:重启Django程序Copy
更换ubuntu中默认的源为国内的源
# 第一步:备份 /etc/apt/sources.list # 第二步:在/etc/apt/sources.list中添加以下阿里源 deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
# 备份 cp /etc/apt/sources.list /etc/apt/sources.list.bak # 添加阿里源 sudo vim /etc/apt/sources.list
更新apt-get的源和升级
sudo apt-get update sudo apt-get upgrade
安装libmysqlclient-dev
sudo apt-get install libmysqlclient-dev
虚拟环境中安装mysqlclient==1.4.6