入门2:mysql数据库模块的配置

将项目名/项目名内的文件夹中的setting.py文件中的数据库配置修改为mysql

DATABASES = {
    'default': {
        #'ENGINE': 'django.db.backends.sqlite3',
         'ENGINE': 'django.db.backends.mysql',
         'NAME': 'mysql中对应的数据库名(自己在飞root用户下创建)',
        #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
         'USER': '账号名',
         'PASSWORD': '密码',
         'HOST': '127.0.0.1',
         'PORT': '3306'
    }
}


PS: 因为多个app处于同一网站之下,使用同一种数据库,所以只需要在网站根目录下进行配置即可
1.安装mysql:
yum install mysql -y
yum install mysql-server -y
yum install mysql-devel -y

但是在CentOS中mysql因为潜在的商业原因被改为了mariadb,所以需要到官网下载
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
service mysqld restart
[root@localhost mysite]# mysql -u root 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# Set new password
mysql> set password for 'root'@'localhost' =password('password');

不过推荐使用非管理员账号做后台管理....权限最好不要授予太多
Create  user  '用户名'@'localhost'  identified  by  '密码';
Grant   all  privileges(读写权限)   on  数据库名.表名(或者偷懒用*.*)   to   '用户名'@'localhost' ;
flush privileges;

后来发现少装了Mysql-Python,但是Mysql-Python不支持py3,
所以需要pip install pymysql
然后在/项目名/项目名/ _init_.py的文件中加入:
import pymysql
pymysql.install_as_MySQLdb()


python manage.py migrate

创建数据库表项:
入门2:mysql数据库模块的配置_第1张图片
image.png

然后就会在你配置的数据库中初始化一系列的表
入门2:mysql数据库模块的配置_第2张图片
image.png

如果mysql配置出了点问题想卸载mysql怎么办?
rpm -qa |grep -i mysql
#查看安装了哪些依赖:
[root@localhost mysite]# rpm -qa |grep -i mysql
mysql-community-release-el7-5.noarch
mysql-community-server-5.6.36-2.el7.x86_64
mysql-community-common-5.6.36-2.el7.x86_64
mysql-community-devel-5.6.36-2.el7.x86_64
mysql-community-libs-5.6.36-2.el7.x86_64
mysql-community-client-5.6.36-2.el7.x86_64


yum remove mysql-community mysql-community-server mysql-community-libs mysql-community-common mysql-community-release

你可能感兴趣的:(入门2:mysql数据库模块的配置)