搭建Django鏈接MySQL流程(python2版)
1.1、yum安裝(確定有正確的yum源 https://mirrors.tuna.tsinghua.edu.cn/)
yum install mariadb*
systemctl enable mariadb
添加mysql的root密碼
use mysql set password=PASSWORD(‘password‘) where User=‘root‘;
設置其他主機登錄的權限:
mysql>GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ WITH GRANT OPTION //賦予任何主機訪問數據的權限
mysql>FLUSH PRIVILEGES //修改生效
1.2、rpm包安裝(步驟轉自我朋友王康寧的筆記https://www.kancloud.cn/ningjing_home/ceph/462330) 卸載系統自動安裝的mariadb
yum -y remove MariaDB*
安裝mysql的rpm包,需按照以下順序進行安裝。
rpm -ivh mysql-community-common-5.7.17-1 .el7.x86_64.rpm rpm -ivh mysql-community-libs-5.7.17-1.el7.x86_64.rpm rpm -ivh mysql-community-client-5.7.17-1.el7.x86_64.rpm rpm -ivh mysql-community-server-5.7.17-1.el7.x86_64.rpm rpm -ivh mysql-community-devel-5.7.17-1.el7.x86_64.rpm rpm -ivh mysql-community-embedded-5.7.17-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm rpm -ivh mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
初始化mysql數據庫,獲取登錄密碼
mysqld --initialize --user=mysql
若提示目錄下有文件,直接將/var/lib/mysql/ 文件夾下的文件刪除幹凈再次執行。
完成後 進入日誌文件查找初始化生成的密碼vi /var/log/mysqld.log
啟動mysql systemctl start mysqld
修改初始密碼
登錄mysql
mysql -u root -p
輸入日誌中的密碼
第一次更改密碼前所有的操作都是禁止的,依次執行: set password=password(‘123456‘);flush privileges; quit;
或者通過命令行 mysqladmin -u root -p"5NUV1wX<;pj=" password 123456
重啟mysql: systemctl restart mysqld
查看配置文件: cat /etc/my.cnf
1.3 源碼安裝
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
安裝啟動:
gunzip mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.17-linux-glibc2.5-x86_64.tar /usr/local/
.創建運行MySQL的用戶和組(名字可以任意)
Shell> groupadd mysql
Shell> useradd mysql -g mysql
切換到mysql目錄
Shell > mkdir data
修改解壓包的權限
Shell> chown –R mysql .
Shell> chgrp –R mysql .
安裝MySQL
Shell> bin/mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data ##初始化數據目錄
shell> cp -f support-files/my-default.cnf /etc/my.cnf ##將默認配置文件復制到指定目錄
shell> bin/mysqld_safe --datadir=/usr/local/mysql/data --user=mysql & ##啟動MySQL服務
shell> cp support-files/mysql.server /etc/init.d/mysql.server ##將MySQL加入到服務自啟動
Shell> /etc/init.d/mysql.server start ##通過服務啟動MySQL
MySQL配置文件my.ini中,在[mysqld]下添加skip-grant-tables,關閉mysql服務並重新開啟; 輸入命令:mysql -u root -p 回車後,要求輸入密碼,直接回車跳過,則直接進入MySQL。 這樣設置之後,可以直接雙擊D:\MySQL5.7.10\bin下的mysql.exe啟動,而不用輸入命令。 update mysql.user set password=PASSWORD(‘password‘) where User=‘root‘;
2、安裝python的相關模塊(註意順序)
2.1、安裝python-devel,mysql-devel(其中含有mysql_config模塊)
2.2、在pypi.org下載MySQL-python-1.2.5.zip包。
2.3、源碼安裝MySQL-python,python setup.py install
[root@VirtualBox-web MySQL-python-1.2.5]# python -c "import MySQLdb"
/usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg/_mysql.pyc, but /root/MySQL-python-1.2.5 is being added to sys.path
因為是源碼安裝會有兩個MySQL_python-1.2.5-py2.7-linux-x86_64.egg,檢查版本一致的話,手動將root下的MySQL_python-1.2.5-py2.7-linux-x86_64.egg刪除。
3、修改項目的鏈接mysql。
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.mysql‘,
‘NAME‘: ‘lagou‘,
‘USER‘: ‘root‘,
‘PASSWORD‘: ‘password‘,
‘HOST‘: ‘127.0.0.1‘,
‘PORT‘: ‘3306‘,
}
}
4、測試Django對mysql的連通性。
4.1、django.db.utils.OperationalError: (2003, "Can‘t connect to MySQL server on ‘192.168.56.104‘ (113)"),
Django鏈接不到外網,修改項目setting.py的 ALLOWED_HOSTS = []成ALLOWED_HOSTS = [‘*‘,]
4.2、django.db.utils.OperationalError: (1130, "Host ‘192.168.56.105‘ is not allowed to connect to this MariaDB server")
mysql>GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ WITH GRANT OPTION //賦予任何主機訪問數據的權限
mysql>FLUSH PRIVILEGES //修改生效
4.3、 python manage.py migrate,自動創建web系統使用的表
4.4、通過Django創建指定表huiFangFirstTable和huiFangSecondTable。 4.5、查看創建的sql 4.6創建表成功來自為知筆記(Wiz)
搭建Django鏈接MySQL流程(python2版)