Linux运维-day50-综合架构-LNMP数据库存储迁移与负载均衡

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

16955089-6a77b7f01b2e7b26.png

一、LNMP数据库迁移

环境准备:
web01(外网:10.0.0.7;内网:172.16.1.7)
db01(外网:10.0.0.51;内网:172.16.1.51)

将web01上的数据库迁移到db01上

1>首先在db01装好MySQL数据库:yum install -y mariadb.service
2>启动MySQL服务,并设置开机自启动
 启动服务:systemctl start mariadb.service
 设置开机自启动:systemctl enable mariadb.service

1.1 在web01上备份数据库,并打包

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上备份的数据库随送到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还原数据库

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 检查还原之后的数据库
[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修改网站站点目录config文件中连接数据库的IP地址及账号信息
[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' );  \\将这里改为远程数据库服务器的IP地址
……

重启web01上的php-fpm服务:systemctl restart php-fpm.service
停止web01上的MySQL服务:systemctl restart php-fpm.service

测试数据库迁移的是否正常,用浏览器部署的网站,在后台添加一条数据,然后去db01服务器上查看是否有数据

二、网站上传目录迁移

环境准备:

nfs01服务器(内网:172.16.1.31;外网:10.0.0.31)
web01服务器(内网:172.16.1.7;外网:10.0.0.7)

2.1 在nfs01上配置共享服务

1>安装rpcbind和nfs

yum install -y rpcbind nfs-utils

2>启动rpcbind和nfs服务,并设置开机自启动

启动服务:systemctl start rpcbind nfs
开机自启动:systemctl enable rpcbind nfs

3>在nfs配置文件中进行配置

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

4>因web01服务器nginx服务目前使用的用户是nginx,为了两个服务器使用的用户保持一致,在nfs01上创建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>根据nfs的配置文件创建共享目录,并授权

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

6>重启rpcbind和nfs服务

[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服务

yum install -y nfs-utils

2>启动nfs服务,并设置开机自启动

启动服务:systemctl start nfs
开机自启动:systemctl enable nfs

3>保证web01服务器与nfs01服务器上的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>给站点目录重新修改所有者和所有属组

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

5>将网站的上传存储目录进行挂载,并开机自动挂载

如果网站的上传存储目录里在挂载之前已经有上传的文件,为了保证数据不丢失,挂载之前先进行备份,因为nfs挂载会将之前的数据覆盖掉,备份好之后再进行挂载,挂载之后再将文件还原

[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/

②如果上传存储目录还没有文件,直接挂载即可
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]# 

③设置开机自动挂载

[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 ~]# 

到这里数据库和存储目录就都分别迁移到db01服务器和nfs01服务器上,然后去浏览器测试就可以了
排错流程

三、nginx负载均衡

让后端服务器,保持每台服务器工作(负载)平均

3.1 实现

硬件设备:F5,A10,Redware
开源软件:Nginx、Haproxy、Lvs

3.2 开源软件负载均衡的区别:※※
命名不同
    负载均衡:用户请求的转发(Lvs)
    反向代理:代替用户去找,在发给用户(类似中介)(Nginx、Haproxy)
功能不同
    Lvs:工作在四层负载均衡
        传输层   tcp/udp
        最多进行端口转发
    Nginx、Haproxy:工作在4层和7层负载均衡
        传输层和应用层
        进行http协议 uri转发
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值