avatar

13.LNPM数据库拆分

LNPM数据库拆分

第1章 环境搭建准备

主机名 IP地址 安装服务
web01 172.16.1.7 nginx+php
db01 172.16.1.51 mysql

第2章 数据库拆分详细步骤

2.1 web01上备份数据库并拷贝到db01上

备份 web01 上的数据库

1
[root@web01 ~]# mysqldump -uroot -p'oldboy' -A --single-transaction > /tmp/web01.sql

将 web01 上备份的数据库拷贝至 db01 服务器上

1
[root@web01 ~]# scp /tmp/web01.sql 172.16.1.51:/tmp

2.2 db01服务器恢复数据库

安装数据库,并启动

1
2
3
4
[root@db01 ~]# yum install mariadb mariadb-server -y
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb
[root@db01 ~]# mysqladmin password 'oldboy'

将web01.sql导入到db01的数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@db01 ~]# mysql -uroot -p'oldboy' < /tmp/web01.sql

[root@db01-51 /tmp]# mysql -uroot -p'oldboy' -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
| zh |
+--------------------+

2.3 db01数据库进行授权

在新数据库上授权, 允许所有网段, 通过 all 账户连接并操作该数据库
授权所有权限 grant all privileges
授权所有库所有表 .
将授权赋予给哪个用户,这个用户只能通过哪个网段过来(%所有) ‘all’@’%’
授权该用户登录的密码 identified by

1
2
3
4
[root@db01-51 ~]# mysql -uroot -p'oldboy'
MariaDB [(none)]> grant all privileges on *.* to 'all'@'%' identified by 'oldboy';
> flush privileges;
>exit;

2.4 测试使用IP地址能不能登陆

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@web01 ~]# mysql -uall -poldboy -h 172.16.1.51
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
| zh |
+--------------------+
6 rows in set (0.01 sec)

2.5 web01修改PHP代码连接到新数据库

修改wordpress

1
2
3
4
5
6
7
[root@web01 ~]# cd /code/wordpress/ 
[root@web01 /code/wordpress]# find . -type f|xargs grep oldboy #查找密码所在文件
./wp-config.php:define('DB_PASSWORD', 'oldboy');
[root@web01 /code/wordpress]# vim wp-config.php
define('DB_USER', 'all');
define('DB_PASSWORD', 'oldboy');
define('DB_HOST', '172.16.1.51');

修改wecenter

1
2
3
4
5
6
7
[root@web01 ~]# cd /code/zh/
[root@web01 /code/zh]# find . -type f|xargs grep oldboy
./system/config/database.php: 'password' => 'oldboy',
[root@web01 /code/zh]# vim system/config/database.php
'host' => '172.16.1.51',
'username' => 'all',
'password' => 'oldboy',

2.6 停止web01上的数据库,测试网页是否正常

1
[root@web01 /code/zh]# systemctl stop mariadb.service

此时如果打开网页没有问题则表明数据库拆分完成

第3章 拓展WEB服务器

3.1 为什么要拓展多台web节点

单台 web 服务器能抗住的访问量是有限的,配置多台 web 服务器能提升更高的访问速度

3.2 拓展多台web解决了什么问题

1.单台 web 节点如果故障,会导致业务 down 机
2.多台 web 节点能保证业务的持续稳定,扩展性高
3.多台 web 节点能有效的提升用户访问网站的速度

3.3 多台web服务器架构组成

3.4 多台web服务器思路

1.可以使用ansible批量部署多台web服务器
2.配置内网私有yum仓库
3 按照web01的步骤安装好nginx和php,然后远程拷贝代码到新机器

3.5 部署步骤

3.5.1 web02 创建用户名密码

1
2
[root@web02-8 ~]# groupadd www -g 666
[root@web02-8 ~]# useradd www -s /sbin/nologin -M -u 666 -g 666

2.5.2 web02安装Nginx+PHP

可以直接从web01上拷贝yum源到本机yum目录

1
2
3
4
[root@web02-8 ~]# scp -rp root@172.16.1.7:/etc/yum.repos.d/* /etc/yum.repos.d/
[root@web02-8 ~]# scp -rp root@172.16.1.7:/etc/pki/rpm-gpg/* /etc/pki/rpm-gpg/
[root@web02-8 ~]# yum -y install nginx
[root@web02-8 ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

3.2.3 拷贝web01的nginx和php配置文件到web02

1
2
[root@web02-8 ~]# scp -rp root@172.16.1.7:/etc/nginx /etc/
[root@web02-8 ~]# scp -rp root@172.16.1.7:/etc/php-fpm.d /etc/

3.2.4 拷贝代码目录到web02

1
2
[root@web01 ~]# tar zcf code.tar.gz /code
[root@web01 ~]# scp code.tar.gz root@172.16.1.8:/tmp

3.2.5 web02上将代码解压到相应目录

1
2
3
[root@web02-8 ~]# tar xf /tmp/code.tar.gz -C /
[root@web02-8 ~]# ll -d /code/
drwxr-xr-x 4 www www 112 Aug 1 17:02 /code/

3.2.6 web02上启动nginx和php-fpm并加入开机自启动

1
2
[root@web02-8 ~]# systemctl start nginx php-fpm.service 
[root@web02-8 ~]# systemctl enable nginx php-fpm.service

3.2.7 web访问测试

修改windows的hosts为web02的地址,然后浏览器访问测试

第4章 将静态资源挂载到共享存储

4.1 为什么要拆分静态资源到独立服务器

当后端的 web 节点出现多台时,会导致用户上传的图片、视频附件等内容仅上传至一台 web 服务器,那么其他的web 服务器则无法访问到该图片

4.2 新增一台nfs存储解决了什么问题

1.保证了多台 web 节点静态资源一致。
2.有效节省多台 web 节点的存储空间。
3.统一管理静态资源,便于后期推送至 CDN 进行静态资源加速

4.3 多台web节点架构环境准备

服务器 IP地址 安装服务
web01 172.16.1.7 Nginx+PHP
web02 172.16.1.8 Nginx+PHP
db01 172.16.1.51 MySQL
nfs 172.16.1.31 NFS

4.4 NFS服务器端配置

4.4.1安装配置NFS

1
2
3
4
[root@nfs ~]# yum -y install nfs-utils
[root@nfs ~]# cat /etc/exports
/data/blog 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/zh 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

4.4.2 创建共享目录并进行授权

1
2
[root@nfs ~]# mkdir /data/{blog,zh}
[root@nfs ~]# chown -R www.www /data/

4.4.3 启动nfs服务并加入开机自启

1
2
3
4
5
6
7
[root@nfs ~]# systemctl start nfs-server
[root@nfs ~]# systemctl enable rpcbind nfs-server
[root@nfs ~]# systemctl restart rpcbind nfs-server
[root@nfs ~]# showmount -e
Export list for nfs:
/data/zh 172.16.1.0/24
/data/blog 172.16.1.0/24

4.5 web01端操作步骤如下

4.5.1 web01安装nfs

1
2
3
4
5
6
7
[root@web01 ~]# yum -y install nfs-utils rpcbind
[root@web01 ~]# systemctl restart rpcbind
[root@web01 ~]# systemctl enable rpcbind nfs-server
[root@web01 ~]# showmount -e 172.16.1.31 #查看nfs共享信息
Export list for 172.16.1.31:
/data/zh 172.16.1.0/24
/data/blog 172.16.1.0/24

4.5.2 查找Wordpress 静态资源存放的位置

在wordpress的文章里添加一张图片,然后查看图片URL资源

或者

复制图片地址打开,查看静态资源存放路径

4.5.3 备份web01服务器上Wordpress 的静态资源

1
2
3
4
5
6
7
8
9
10
11
[root@web01 ~]# cd /code/wordpress/wp-content
[root@web01 /code/wordpress/wp-content]# cp -a uploads uploads.bak
[root@web01 /code/wordpress/wp-content]# ll
total 8
-rw-r--r-- 1 www www 28 Jan 9 2012 index.php
drwxr-xr-x 4 www www 4096 Feb 8 2018 languages
drwxr-xr-x 3 www www 55 Aug 2 10:59 plugins
drwxr-xr-x 5 www www 88 Aug 2 10:59 themes
drwxr-xr-x 2 www www 6 Aug 1 16:32 upgrade
drwxr-xr-x 3 www www 18 Aug 1 12:03 uploads
drwxr-xr-x 3 www www 18 Aug 1 12:03 uploads.bak

4.5.4 备份web01服务器上zh的静态资源

复制图片地址,找到静态资源存放路径

备份静态资源

1
2
[root@web01 ~]# cd /code/zh
[root@web01 /code/zh]# cp -a uploads uploads.bak

4.5.5 web01客户端执行挂载操作

1
2
3
4
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/blog/ /code/wordpress/wp-content/uploads
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/zh/ /code/zh/uploads
[root@web01 ~]# cp -rp /code/wordpress/wp-content/uploads.bak/* /code/wordpress/wp-content/uploads/
[root@web01 ~]# cp -rp /code/zh/uploads.bak/* /code/zh/uploads/

4.5.6 将挂载信息加入开机自启

1
2
3
4
[root@web01 ~]# tail -2 /etc/rc.local 
mount -t nfs 172.16.1.31:/data/blog/ /code/wordpress/wp-content/uploads
mount -t nfs 172.16.1.31:/data/zh/ /code/zh/uploads
[root@web01 ~]# chmod +x /etc/rc.d/rc.local

4.6 web02端操作与web01一样

安装配置NFS

1
2
3
4
5
6
7
[root@web02-8 ~]# yum -y install rpcbind nfs-utils
[root@web02-8 ~]# systemctl restart rpcbind
[root@web02-8 ~]# systemctl enable rpcbind nfs-server
[root@web02-8 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/zh 172.16.1.0/24
/data/blog 172.16.1.0/24

备份web02服务器上Wordpress 的静态资源

1
2
[root@web02-8 /code/wordpress/wp-content]# cp -a uploads uploads.bak
[root@web02-8 /code/zh]# cp -a uploads uploads.bak

web02客户端执行挂载操作

1
2
3
4
5
[root@web02-8 ~]# mount -t nfs 172.16.1.31:/data/blog/ /code/wordpress/wp-content/uploads
[root@web02-8 ~]# mount -t nfs 172.16.1.31:/data/zh/ /code/zh/uploads
[root@web02-8 ~]# df -h
172.16.1.31:/data/blog 39G 1.8G 38G 5% /code/wordpress/wp-content/uploads
172.16.1.31:/data/zh 39G 1.8G 38G 5% /code/zh/uploads

拷贝备份的静态资源数据到挂载点目录下

1
2
[root@web02-8 ~]# cp -rp /code/wordpress/wp-content/uploads.bak/* /code/wordpress/wp-content/uploads/
[root@web02-8 ~]# cp -rp /code/zh/uploads.bak/* /code/zh/uploads/

将挂载信息加入开机自启

1
2
3
4
[root@web02-8 ~]# tail -2 /etc/rc.local 
mount -t nfs 172.16.1.31:/data/blog/ /code/wordpress/wp-content/uploads
mount -t nfs 172.16.1.31:/data/zh/ /code/zh/uploads
[root@web02-8 ~]# chmod +x /etc/rc.d/rc.local
文章作者: Wu Fei
文章链接: http://linuxwf.com/2020/04/13/13-LNPM%E6%95%B0%E6%8D%AE%E5%BA%93%E6%8B%86%E5%88%86/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 WF's Blog
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论