入門(mén)2:mysql數(shù)據(jù)庫(kù)模塊的配置

將項(xiàng)目名/項(xiàng)目名內(nèi)的文件夾中的setting.py文件中的數(shù)據(jù)庫(kù)配置修改為mysql

DATABASES = {
    'default': {
        #'ENGINE': 'django.db.backends.sqlite3',
         'ENGINE': 'django.db.backends.mysql',
         'NAME': 'mysql中對(duì)應(yīng)的數(shù)據(jù)庫(kù)名(自己在飛root用戶(hù)下創(chuàng)建)',
        #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
         'USER': '賬號(hào)名',
         'PASSWORD': '密碼',
         'HOST': '127.0.0.1',
         'PORT': '3306'
    }
}


PS: 因?yàn)槎鄠€(gè)app處于同一網(wǎng)站之下,使用同一種數(shù)據(jù)庫(kù),所以只需要在網(wǎng)站根目錄下進(jìn)行配置即可
1.安裝mysql:
yum install mysql -y
yum install mysql-server -y
yum install mysql-devel -y

但是在CentOS中mysql因?yàn)闈撛诘纳虡I(yè)原因被改為了mariadb,所以需要到官網(wǎng)下載
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');

不過(guò)推薦使用非管理員賬號(hào)做后臺(tái)管理....權(quán)限最好不要授予太多
Create  user  '用戶(hù)名'@'localhost'  identified  by  '密碼';
Grant   all  privileges(讀寫(xiě)權(quán)限)   on  數(shù)據(jù)庫(kù)名.表名(或者偷懶用*.*)   to   '用戶(hù)名'@'localhost' ;
flush privileges;

后來(lái)發(fā)現(xiàn)少裝了Mysql-Python,但是Mysql-Python不支持py3,
所以需要pip install pymysql
然后在/項(xiàng)目名/項(xiàng)目名/ _init_.py的文件中加入:
import pymysql
pymysql.install_as_MySQLdb()


python manage.py migrate

創(chuàng)建數(shù)據(jù)庫(kù)表項(xiàng):
image.png

然后就會(huì)在你配置的數(shù)據(jù)庫(kù)中初始化一系列的表
image.png

如果mysql配置出了點(diǎn)問(wèn)題想卸載mysql怎么辦?
rpm -qa |grep -i mysql
#查看安裝了哪些依賴(lài):
[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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容