018:Django商城部署和数据库读写分离
本章知識點
1、Centos下python3環(huán)境的部署
2、Python uwsgi
3、Python uwsgi+nginx部署
4、mysql主從備份介紹
5、Linux下的mysql安裝
6、基于mysql的Django讀寫分離
知識點講解
1、Centos下python3環(huán)境的部署
Yum源,就是我們的安裝源。
檢測yum是否完好
Yum list
當(dāng)遇到以下問題
Yum 進程沒有關(guān)閉
錯誤:1
Yum 源沒有配置
配置yum源:
https://www.linuxidc.com/Linux/2017-08/146364.htm
錯誤:2
Pid 進程id
Kill -9 pid 殺死進程
安裝python需要的依賴包
Yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make?
Unix系統(tǒng)默認安裝了python
Centos 6 安裝 python2.6版本
Centos 7 安裝 python2.7版本
我們需要的版本是python 3.6.2,在centos上進行python2和3并存
下載python 3.6.2的包
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz
補充:
協(xié)議:網(wǎng)絡(luò)上數(shù)據(jù)傳輸?shù)募s定規(guī)則
Tcp 面向連接的協(xié)議,有校驗的數(shù)據(jù)包
Udp 無連接
可容忍丟包率
http 常規(guī)的http
https 加密的http
ftp 文件傳輸
Ssh 遠程空開
Sftp 基于ssh的文件傳輸
Smtp 協(xié)議
解壓安裝包
tar -xvJf Python-3.6.2.tar.xz?
編譯安裝
./configure prefix=/usr/local/python3
Make && make install
配置軟連接(環(huán)境變量)
軟連接 --> 快捷方式
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
測試:
以后我們運行python3的腳本需要編程 python3 *.py
安裝python的擴展包
yum install python-devel
安裝python 開發(fā)包
Pymysql
Pillow
Django2.1.5
django-ckeditor
pip3 install pymysql && pip3 install pillow && pip3 install django2.1.5 &&pip3 install django-ckeditor
2、Python uwsgi
Python 的web項目,本身很難和Apache或者Nginx進行結(jié)合,為了讓python web項目更加方便的被部署,開發(fā)出了uwsgi。
安裝:
Pip安裝uwsgi
創(chuàng)建軟連接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
使用:
我們遷移我們的項目Qshop
復(fù)制我們的項目到虛擬機
cp -r /root/Desktop/Qshop/ /opt/Qshop
我們需要清除pycharm的配置文件,初始的導(dǎo)入文件
測試項目
python3 manage.py runserver 0.0.0.0:8000
注意:
Systemctl stop firewalld 關(guān)閉防火墻
用uwsgi啟動django
uwsgi --http 10.10.16.229:8000 --file Qshop/wsgi.py --static-map=/static=static
uwsgi 模塊名稱
–http 協(xié)議
10.10.16.229:8000 主機端口
–file uwsgi要使用的解析文件,一般django自己創(chuàng)建
–static 靜態(tài)目錄
上面我們采用的是命令的方式使用uwsgi啟動django,但是,不夠完美。我們可以構(gòu)造配置文件進行操作。
配置內(nèi)容如下
[uwsgi]
chdir=/opt/OurBlog #項目目錄
module=OurBlog.wsgi:application #指定項目的application
socket=/opt/script/uwsgi.sock #指定sock的文件路徑
workers=5 #進程個數(shù)
pidfile=/opt/script/uwsgi.pid
http= 192.168.2.69:8000 #指定IP端口
static-map=/static=/opt/OurBlog/static #指定靜態(tài)文件
uid=root #用戶
gid=root #組
master=true #啟用主進程
vacuum=true #自動移除unix Socket和pid文件當(dāng)服務(wù)停止的時候
enable-threads=true #啟用線程
thunder-lock=true #序列化接受的內(nèi)容,如果可能的話
harakiri=30 #設(shè)置自中斷時間
post-buffering=4096 #設(shè)置緩沖
daemonize=/opt/script/uwsgi.log #設(shè)置日志目錄
1、創(chuàng)建uwsgi文件的配置文件目錄和文件
2、編寫內(nèi)容
啟動uwsgi
授權(quán)
殺死進程重啟
2、Python uwsgi+nginx部署
wget -c https://nginx.org/download/nginx-1.12.2.tar.gz
解壓 tar -zxvf nginx-1.12.2.tar.gz
編譯安裝
./configure
make && make install
Nginx
/usr/local/nginx
創(chuàng)建軟連接
Nginx通訊uwsgi
修改Nginx的配置文件
備份配置文件
開始配置
http {
? ? include ? ? ? mime.types;
? ? default_type ?application/octet-stream;
? ? log_format ?main ?'$remote_addr - $remote_user [$time_local] "$request" '
? ? ? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
? ? ? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"'; 日志的格式
? ? #access_log ?logs/access.log ?main;
? ? sendfile ? ? ? ?on;
? ? #tcp_nopush ? ? on;
? ? #keepalive_timeout ?0;
? ? keepalive_timeout ?65;
? ? #gzip ?on;
server {
? ? ? ? listen ? ? ? 80;
? ? ? ? server_name ?Qshop; 服務(wù)的名稱
? ? ? ? charset utf-8; 編碼格式
? ? ? ? access_log ?logs/host.access.log ?main; 訪問日志
? ? ? ? gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; ?訪問內(nèi)容的類型
? ? ? ? error_log /var/log/nginx/error.log error; 錯誤日志,默認沒有,需要手動創(chuàng)建
? ? ? ? location / {
? ? ? ? ? ? ?include uwsgi_params; 加載uwsgi_params
? ? ? ? ? ? ?uwsgi_connect_timeout 30; 連接的超時時間 不要加冒號不要加冒號不要加冒號?
? ? ? ? ? ? ?uwsgi_pass unix:/opt/script/uwsgi.sock; uwsgi.sock通訊的文件地址
? ? ? ? }
? ? ? ? location = /static/{
? ? ? ? ? ? alias /opt/Qshop/static; 靜態(tài)文件的目錄
? ? ? ? ? ? index index.html index.htm;
? ? ? ? }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
殺死當(dāng)前狀態(tài)下的所有Nginx服務(wù)
Pkill -9 nginx
從新啟動
訪問網(wǎng)站:
商城數(shù)據(jù)庫讀寫分離:
知識點:
1、mysql主從備份介紹
2、Linux下的mysql安裝
3、基于mysql的Django讀寫分離
知識點講解
1、mysql主從備份介紹
備份:安全。
目的:Django網(wǎng)站數(shù)據(jù)庫讀寫分離設(shè)置。在我們工作當(dāng)中,發(fā)現(xiàn)有的數(shù)據(jù)表經(jīng)常查詢,有的表 經(jīng)常寫入。同時交給一個數(shù)據(jù)庫,效率不高。
2、Linux下的mysql安裝
2臺Centos7 虛擬機
Server 主Ip 10.10.16.229
Client 從Ip 10.10.16.141
關(guān)閉防火墻
測試ping
3、基于mysql的Django讀寫分離
在Linux安裝mariadb數(shù)據(jù)庫
Mysql本身是開源的,大家用的很開心,但是有一家數(shù)據(jù)庫大戶,oracle公司看上了mysql,覺得收購,并且答應(yīng)mysql我們依然開源。果然,后來收費了。Mysql的團隊,不忿,然后從新寫了mysql的妹妹:mariadb,mariadb和mysql相識度到99%。并且免費。所以,centos7系統(tǒng),我們使用mariadb。
Centos7 官方y(tǒng)um源包含mariadb的完整安裝,所以我們需要的只是一條安裝命令。
yum -y install mariadb mariadb-server 安裝mariadb
Systemctl start mariadb 啟動mariadb
給root添加密碼
安裝telnet,這個工具在mysql主從備份會用到
yum install telnet.x86_64 telnet-server.x86_64
為了后面的實驗,我們在主mysql創(chuàng)建一個小小的數(shù)據(jù)庫
主從配置
查看主數(shù)據(jù)庫
修改數(shù)據(jù)庫配置
/etc/my.cnf
備份配置文件
訪問配置文件
配置主服務(wù)器的配置文件
/etc/my.cnf
重啟mariadb
systemctl restart mariadb
進入數(shù)據(jù)庫進行授權(quán)
GRANT REPLICATION SLAVE ON . TO “root”@“10.10.16.%” IDENTIFIED BY’admin’
GRANT 授權(quán)
REPLICATION 權(quán)限
SLAVE 備份
ON 在。。之上
. 所有權(quán)限
TO 給,去
“root” 用戶
“10.10.16.%”16網(wǎng)段所有ip
IDENTIFIED BY 確認密碼
Admin 備份的密碼
在工作當(dāng)中,我們備份的時候,可以允許讀操作,不允許寫操作,我們需要在備份的時候?qū)?shù)據(jù)庫進行加鎖。
加鎖
FLUSH TABLES WITH READ LOCK;
然后備份數(shù)據(jù)庫
導(dǎo)入和導(dǎo)出都是基于數(shù)據(jù)庫外的,操作之前請退出數(shù)據(jù)庫
數(shù)據(jù)庫數(shù)據(jù)導(dǎo)出
mysqldump -uroot -p123 --all-databases > /root/db.sql
解鎖
數(shù)據(jù)庫數(shù)據(jù)導(dǎo)入
本章總結(jié)
mysql主從備份介紹
Linux下的mysql安裝
基于mysql的Django讀寫分離
————————————————
版權(quán)聲明:本文為CSDN博主「考古學(xué)家lx」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_43582101/article/details/86625140
總結(jié)
以上是生活随笔為你收集整理的018:Django商城部署和数据库读写分离的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工行信用卡一年多了老是提不了额度怎么办
- 下一篇: Django中实现MySQL主从同步实现