Python 3.6.1+Django 1.11+MySQL 5.7.18 開發環境搭建(mac os)(二)
前言
這段時間在做一個基於B/S的資訊管理平臺,在迪總的安排下,我和文濤同學負責後端的開發。在專案開始就確定了Python+Django+MySQL環境,在幾次試驗以後確定了版本,採用Python 3.6.1+Django 1.11+MySQL 5.7.18 作為開發環境。本文主要總結該環境搭建的步驟和經驗,主要參考了自強學堂 和廖雪峰的官方網站的教程。
接上一篇的內容,繼續總結 MySQL 5.7.18 的安裝 和 Django 專案與 MySQL 的連線與測試。
MySQL 5.7.18 的安裝
下載
安裝
安裝過程是傻瓜式的,需要記住的是隨機生成的密碼(這三張圖是借的):
安裝完成後啟動MySQL:
配置環境變數
開啟終端,輸入mysql
顯示:
MacBook-Air:~ ww$ mysql
-bash: mysql: command not found
說明沒有找到mysql
,可以去MySQL預設的安裝路徑/usr/local/mysql/bin/
找一下mysql
,確定有的話就開始改環境變數:
$ vim ~/.bash_profile
在 .bash_profile
檔案中加入export PATH=${PATH}:/usr/local/mysql/bin
,然後儲存退出,在終端執行:
$ source ~/.bash_profile
讓改變生效。此時執行mysql
和mysqladmin
:
MacBook-Air:~ ww$ mysql
ERROR 1045 (28000): Access denied for user 'ww'@'localhost' (using password: NO)
MacBook-Air:~ ww$ mysqladmin
mysqladmin Ver 8.42 Distrib 5.7.18, for macos10.12 on x86_64
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
...
發現命令已經可以使用,第一個命令沒有密碼被拒絕。
改密碼
我們直接改root
賬戶的密碼,這裡列了三種方法:
$ mysqladmin -u root -p password 123456 #執行後要輸入舊密碼,還有會被提示不安全
登陸MySQL
mysql> set password for 使用者名稱@localhost=password('新密碼')
登陸MySQL
mysql> use mysql;
mysql> update user set password=password('新密碼') where user='root' and host='localhost';
mysql> flush privileges;
現在可以用新密碼123456
來登入MySQL了:
$ mysql -u root -p
Enter password: #輸入密碼,它不會顯示出來
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 188
Server version: 5.7.18 MySQL Community Server (GPL)
...
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
登陸成功。簡單的SQL語句可以參考自強學堂SQL教程。
Django 專案與 MySQL 的連線與測試
在上一篇中我們已經建立了工程Jerry_site
:
- Jerry_site #專案目錄
- Jerry_site
- __init__.py
- __pycache__
- settings.py
- urls.py
- wsgi.py
- demo #app目錄
- __init__.py
- migrations
- models.py
- tests.py
- views.py
- manage.py
連線
開啟專案目錄中的settings.py
,注意以下程式碼段:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
可以看出,現在用的是sqlite3
這個資料庫,為了連線MySQL將程式碼改為:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Jerry_DB', #資料庫名
'USER': 'root', #使用者名稱
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
給專案虛環境安裝pymysql
元件,注意要進到虛環境venv
,是給這個虛環境安裝:
(venv) MacBook-Air:Jerry_site ww$ pip install pymysql
Collecting pymysql
Using cached PyMySQL-0.7.11-py2.py3-none-any.whl
Installing collected packages: pymysql
Successfully installed pymysql-0.7.11
開啟Jerry_site/Jerry_site/__init__.py
,寫入以下程式碼:
import pymysql
pymysql.install_as_MySQLdb()
現在我們還沒有要連線的資料庫Jerry_DB
,所以在MySQL中建立一下:
mysql> create database Jerry_db;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| Jerry_db |
| PROJECT_DB |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
7 rows in set (0.03 sec)
可以看到我們已經有了資料庫Jerry_DB
。
測試
進入到專案目錄Jerry_site/
下,執行:
(venv) MacBook-Air:Jerry_site jlwang$ python manage.py dishell
...
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 191
Server version: 5.7.18 MySQL Community Server (GPL)
...
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
看到這個提示,我們順利進入到資料庫命令列,其實已經連線成功,但是我們任然用從其他部落格看到的方法來測試一下,任然是在專案目錄Jerry_site/
下,執行:
(venv) MacBook-Air:Jerry_site ww$ python manage.py shell
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.db import connection #在專案終端環境執行以下兩句
>>> cursor = connection.cursor()
>>>
結果沒有報錯,連線成功。
小結
本篇主要總結了MySQL 5.7.18的安裝和環境變數配置,還有MySQL與Django 1.11的連線與測試。