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ā)