Linux運(yùn)維-day50-綜合架構(gòu)-LNMP數(shù)據(jù)庫存儲(chǔ)遷移與負(fù)載均衡

https://www.processon.com/view/link/5d004e07e4b0cbb88a599f6a

一、LNMP數(shù)據(jù)庫遷移

環(huán)境準(zhǔn)備:
web01(外網(wǎng):10.0.0.7;內(nèi)網(wǎng):172.16.1.7)
db01(外網(wǎng):10.0.0.51擒抛;內(nèi)網(wǎng):172.16.1.51)

將web01上的數(shù)據(jù)庫遷移到db01上

1>首先在db01裝好MySQL數(shù)據(jù)庫:yum install -y mariadb.service
2>啟動(dòng)MySQL服務(wù)隙弛,并設(shè)置開機(jī)自啟動(dòng)
?啟動(dòng)服務(wù):systemctl start mariadb.service
?設(shè)置開機(jī)自啟動(dòng):systemctl enable mariadb.service

1.1 在web01上備份數(shù)據(jù)庫拂到,并打包

mysqldump -uroot -p -A >all.sql
gzip all.sql

[root@web01 ~]# mysqldump -uroot -p -A >/root/all.sql
Enter password: 
[root@web01 ~]# 

[root@web01 ~]# gzip all.sql 
[root@web01 ~]# ll
total 43272
-rw-r--r--  1 root   root        147756 Jun 11 22:02 all.sql.gz

1.2 將web01上備份的數(shù)據(jù)庫隨送到db01上

scp all.sql.gz 172.16.1.51:/root/

[root@web01 ~]# scp all.sql.gz  172.16.1.51:/root
The authenticity of host '172.16.1.51 (172.16.1.51)' can't be established.
ECDSA key fingerprint is SHA256:08kKtoy49Ynk6MZjwZJyQ7cg3znEnhKrb7AUia9Sqls.
ECDSA key fingerprint is MD5:de:85:81:80:9b:dc:ed:43:74:89:07:25:fc:e2:dc:b0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.51' (ECDSA) to the list of known hosts.
root@172.16.1.51's password: 
all.sql.gz                                                                                          100%  144KB  42.8MB/s   00:00    
[root@web01 ~]#

1.3 在db01還原數(shù)據(jù)庫

gzip -d all.sql.gz
mysql -uroot -p <all.sql

[root@db01 ~]# gzip -d all.sql.gz 
[root@db01 ~]# ll
total 544
-rw-r--r--  1 root root 552754 Jun 11 22:03 all.sql
-rw-------. 1 root root   1565 May 26 15:37 anaconda-ks.cfg
[root@db01 ~]# mysql -uroot -p </root/all.sql
Enter password: 
[END] 2019/6/12 0:12:50

1.4 檢查還原之后的數(shù)據(jù)庫

[root@db01 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 211
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lianxi             |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
6 rows in set (0.00 sec)
MariaDB [(none)]> use wordpress;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [wordpress]> show tables;
+-----------------------+
| Tables_in_wordpress   |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
12 rows in set (0.00 sec)
MariaDB [wordpress]> 

1.5 去web01修改網(wǎng)站站點(diǎn)目錄config文件中連接數(shù)據(jù)庫的IP地址及賬號(hào)信息

[root@web01 /usr/share/nginx/html/blog]# vim wp-config.php
<?php
……
define( 'DB_NAME', 'wordpress' );

/** MySQL database username */
define( 'DB_USER', 'wordpress' );

/** MySQL database password */
define( 'DB_PASSWORD', '123456' );

/** MySQL hostname */
define( 'DB_HOST', '172.16.1.51' );  \\將這里改為遠(yuǎn)程數(shù)據(jù)庫服務(wù)器的IP地址
……

重啟web01上的php-fpm服務(wù):systemctl restart php-fpm.service
停止web01上的MySQL服務(wù):systemctl restart php-fpm.service

測試數(shù)據(jù)庫遷移的是否正常归榕,用瀏覽器部署的網(wǎng)站叁征,在后臺(tái)添加一條數(shù)據(jù)纳账,然后去db01服務(wù)器上查看是否有數(shù)據(jù)

二、網(wǎng)站上傳目錄遷移

環(huán)境準(zhǔn)備:

nfs01服務(wù)器(內(nèi)網(wǎng):172.16.1.31捺疼;外網(wǎng):10.0.0.31)
web01服務(wù)器(內(nèi)網(wǎng):172.16.1.7疏虫;外網(wǎng):10.0.0.7)

2.1 在nfs01上配置共享服務(wù)

1>安裝rpcbind和nfs

yum install -y rpcbind nfs-utils

2>啟動(dòng)rpcbind和nfs服務(wù),并設(shè)置開機(jī)自啟動(dòng)

啟動(dòng)服務(wù):systemctl start rpcbind nfs
開機(jī)自啟動(dòng):systemctl enable rpcbind nfs

3>在nfs配置文件中進(jìn)行配置

[root@nfs01 ~]# vim /etc/exports
/webdata  172.16.1.0/24(rw,sync,all_squash,anonuid=2222,anongid=2222)

4>因web01服務(wù)器nginx服務(wù)目前使用的用戶是nginx帅涂,為了兩個(gè)服務(wù)器使用的用戶保持一致议薪,在nfs01上創(chuàng)建nginx用戶,UID和GID目前指定為2222媳友。

useradd nginx -u 2222 -M -s /sbin/nologin

[root@nfs01 ~]# useradd nginx -u 2222  -M -s /sbin/nologin
Creating mailbox file: File exists
[root@nfs01 ~]# id nginx
uid=2222(nginx) gid=2222(nginx) groups=2222(nginx)
[root@nfs01 ~]# 

5>根據(jù)nfs的配置文件創(chuàng)建共享目錄斯议,并授權(quán)

[root@nfs01 ~]# mkdir /webdata
[root@nfs01 ~]# chown nginx.nginx /webdata/

6>重啟rpcbind和nfs服務(wù)

[root@nfs01 ~]# systemctl restart rpcbind nfs
[root@nfs01 ~]# showmount -e
Export list for nfs01:
/webdata 172.16.1.0/24
[root@nfs01 ~]# vim /etc/exports

2.2 在web01上掛載共享目錄

1>安裝nfs服務(wù)

yum install -y nfs-utils

2>啟動(dòng)nfs服務(wù),并設(shè)置開機(jī)自啟動(dòng)

啟動(dòng)服務(wù):systemctl start nfs
開機(jī)自啟動(dòng):systemctl enable nfs

3>保證web01服務(wù)器與nfs01服務(wù)器上的nginx用戶的UID和GID一樣醇锚,如果不一樣哼御,將web01上的nginx刪除,然后在重新添加

[root@web01 ~]# id nginx
uid=998(nginx) gid=996(nginx) groups=996(nginx)
[root@web01 ~]# userdel nginx
userdel: user nginx is currently used by process 7259
[root@web01 ~]# systemctl stop nginx php-fpm.service
[root@web01 ~]# userdel nginx
[root@web01 ~]# useradd nginx -u 2222 -M -s /sbin/nologin
[root@web01 ~]# id nginx
uid=2222(nginx) gid=2222(nginx) groups=2222(nginx)

4>給站點(diǎn)目錄重新修改所有者和所有屬組

[root@web01 ~]# chown -R nginx.nginx /usr/share/nginx/html/blog

5>將網(wǎng)站的上傳存儲(chǔ)目錄進(jìn)行掛載焊唬,并開機(jī)自動(dòng)掛載

如果網(wǎng)站的上傳存儲(chǔ)目錄里在掛載之前已經(jīng)有上傳的文件恋昼,為了保證數(shù)據(jù)不丟失,掛載之前先進(jìn)行備份赶促,因?yàn)閚fs掛載會(huì)將之前的數(shù)據(jù)覆蓋掉液肌,備份好之后再進(jìn)行掛載,掛載之后再將文件還原

[root@web01 /usr/share/nginx/html/blog]# mv  wp-content/uploads  /tmp/
[root@web01 /usr/share/nginx/html/blog]# ll /tmp/
total 12
drwxr-xr-x  3 nginx nginx   16 Jun 12 10:52 2019

[root@web01 /usr/share/nginx/html/blog]# mkdir -p  wp-content/uploads
[root@web01 /usr/share/nginx/html/blog]# chown -R  nginx.nginx  wp-content/uploads
[root@web01 ~]# mount -t nfs 172.16.1.31:/webdata /usr/share/nginx/html/blog/wp-content/uploads/
[root@web01 ~]# mv /tmp/upload/*  /usr/share/nginx/html/blog/wp-content/uploads/

②如果上傳存儲(chǔ)目錄還沒有文件鸥滨,直接掛載即可
mount -t nfs 172.16.1.31:/webdata /usr/share/nginx/html/blog/wp-content/uploads/

[root@web01 /usr/share/nginx/html/blog]# mount -t nfs 172.16.1.31:/webdata /usr/share/nginx/html/blog/wp-content/uploads/
[root@web01 /usr/share/nginx/html/blog]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              19G  2.1G   17G  11% /
devtmpfs              980M     0  980M   0% /dev
tmpfs                 991M     0  991M   0% /dev/shm
tmpfs                 991M  9.6M  981M   1% /run
tmpfs                 991M     0  991M   0% /sys/fs/cgroup
/dev/sda1             197M  105M   93M  54% /boot
tmpfs                 199M     0  199M   0% /run/user/0
172.16.1.31:/webdata   19G  1.9G   17G  10% /usr/share/nginx/html/blog/wp-content/uploads
[root@web01 /usr/share/nginx/html/blog]# 

③設(shè)置開機(jī)自動(dòng)掛載

[root@web01 ~]# tail -1 /etc/fstab
172.16.1.31:/webdata     /usr/share/nginx/html/blog/wp-content/uploads/   nfs  defaults  0 0 
[root@web01 ~]# 

到這里數(shù)據(jù)庫和存儲(chǔ)目錄就都分別遷移到db01服務(wù)器和nfs01服務(wù)器上嗦哆,然后去瀏覽器測試就可以了
排錯(cuò)流程

三谤祖、nginx負(fù)載均衡

讓后端服務(wù)器,保持每臺(tái)服務(wù)器工作(負(fù)載)平均

3.1 實(shí)現(xiàn)

硬件設(shè)備:F5老速,A10粥喜,Redware
開源軟件:Nginx、Haproxy橘券、Lvs

3.2 開源軟件負(fù)載均衡的區(qū)別:※※

命名不同
    負(fù)載均衡:用戶請求的轉(zhuǎn)發(fā)(Lvs)
    反向代理:代替用戶去找额湘,在發(fā)給用戶(類似中介)(Nginx、Haproxy)
功能不同
    Lvs:工作在四層負(fù)載均衡
        傳輸層   tcp/udp
        最多進(jìn)行端口轉(zhuǎn)發(fā)
    Nginx旁舰、Haproxy:工作在4層和7層負(fù)載均衡
        傳輸層和應(yīng)用層
        進(jìn)行http協(xié)議 uri轉(zhuǎn)發(fā)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末锋华,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子箭窜,更是在濱河造成了極大的恐慌供置,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绽快,死亡現(xiàn)場離奇詭異,居然都是意外死亡紧阔,警方通過查閱死者的電腦和手機(jī)坊罢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來擅耽,“玉大人活孩,你說我怎么就攤上這事」猿穑” “怎么了憾儒?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長乃沙。 經(jīng)常有香客問我起趾,道長,這世上最難降的妖魔是什么警儒? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任训裆,我火速辦了婚禮,結(jié)果婚禮上蜀铲,老公的妹妹穿的比我還像新娘边琉。我一直安慰自己,他們只是感情好记劝,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布变姨。 她就那樣靜靜地躺著,像睡著了一般厌丑。 火紅的嫁衣襯著肌膚如雪定欧。 梳的紋絲不亂的頭發(fā)上渔呵,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天,我揣著相機(jī)與錄音忧额,去河邊找鬼厘肮。 笑死,一個(gè)胖子當(dāng)著我的面吹牛睦番,可吹牛的內(nèi)容都是我干的类茂。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼托嚣,長吁一口氣:“原來是場噩夢啊……” “哼巩检!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起示启,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤兢哭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后夫嗓,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體迟螺,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年舍咖,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了矩父。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,902評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡排霉,死狀恐怖窍株,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情攻柠,我是刑警寧澤球订,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站瑰钮,受9級(jí)特大地震影響冒滩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜飞涂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一旦部、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧较店,春花似錦士八、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春蝗茁,著一層夾襖步出監(jiān)牢的瞬間醋虏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工哮翘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留颈嚼,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓饭寺,卻偏偏與公主長得像阻课,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子艰匙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評論 2 354

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