01.创建数据库
1.1 创建mysql库
mysql> create database testdb charset utf8; # 创建数据库
mysql> drop database testdb; # 删除数据库
mysql> show databases; # 查看刚刚创建的数据库
1.2 创建用户并授权
# 1、创建用户
create user 'django'@'%' identified by '123456';
# 2、授予django用户授予对 testdb 数据库的操作权限
GRANT ALL ON testdb.* TO 'django'@'%';
flush privileges;
select host,user from mysql.user;
# 3、删除用户
Delete FROM mysql.user Where User='django' and Host=”localhost”;
Delete FROM mysql.user Where User='django';
02.配置django
2.1 安装PyMySQL
pip install PyMySQL
2.2 主动修改为pymysql
- Django默认使用MySQLdb模块链接MySQL,但在python3中还没有MySQLdb
- 主动修改为pymysql,在project同名文件夹下的__init__文件中添加如下代码即可
from pymysql import install_as_MySQLdb
install_as_MySQLdb()
2.3 修改DATABASES配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'testdb',
'USER': 'django',
'PASSWORD': '123456',
'HOST': '123.56.94.237',
'PORT': '3306',
}
}
2.4 创建表
python manage.py makemigrations
python manage.py migrate
__END__