前面搭建的nginx、PHP(FastCGI)和MySQL是在同一台主机上,接下来就把mysql分离到一台新的主机上,把一些静态资源放到我们的网络文件系统上。
一、迁移MySQL
1、新建一台linux虚拟机安装好mysql,我的配置的主机名db01
博主写的安装教程链接:https://blog.csdn.net/qq_39014511/article/details/90290699
2、导出lnmp中的wordpress数据库数据
[root@web01 tools]# mysqldump -uroot -p123456 wordpress -B |gzip>bak.sql.gz
3、把数据发送给新主机
[root@web01 tools]# scp bak.sql.gz root@10.0.0.51:/tmp
4、在db01主机上把数据导入数据库
[root@db01 tools]# cd /tmp/
[root@db01 tmp]# ls
bak.sql.gz mysql.sock
[root@db01 tmp]# gzip -d bak.sql.gz #-d 参数是删除原压缩文件
[root@db01 tmp]# ls
bak.sql mysql.sock
[root@db01 tmp]# mysqladmin -uroot password 123456 #创建密码
[root@db01 tmp]# mysql -uroot -p123456 </tmp/bak.sql #导入数据
[root@db01 tmp]# mysql -uroot -p123456 -e "show databases like 'wordpress';" #查看数据库
+----------------------+
| Database (wordpress) |
+----------------------+
| wordpress |
+----------------------+
[root@db01 tmp]# mysql -uroot -p123456 -e "use wordpress;show tables" #查看该库中的表
+------------------------+
| Tables_in_wordpress |
+------------------------+
| ysm_commentmeta |
| ysm_comments |
| ysm_links |
| ysm_options |
| ysm_postmeta |
| ysm_posts |
| ysm_term_relationships |
| ysm_term_taxonomy |
| ysm_termmeta |
| ysm_terms |
| ysm_usermeta |
| ysm_users |
+------------------------+
5、在db01上进行数据库授权,让web主机可以访问
授权代码命令:grant all on wordpress.* to wordpress@'172.16.1.%' identified by '123456';
我的新数据库主机的IP地址是172.16.1.51,这里172.16.1.%表示授权172.16.1这个网段所有主机的wordpress用户都有wordpress数据库的all权限
[root@db01 tmp]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.49 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all on wordpress.* to wordpress@'172.16.1.%' identified by '123456'; #授权
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; #刷新生效
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user; #查看用户和地址
+-----------+------------+
| user | host |
+-----------+------------+
| root | 127.0.0.1 |
| wordpress | 172.16.1.% |
| root | ::1 |
| | db01 |
| root | db01 |
| | localhost |
| root | localhost |
+-----------+------------+
7 rows in set (0.00 sec)
mysql>
6、停掉web主机上的mysql数据库
[root@web01 blog]# /etc/init.d/mysqld stop #关闭MySQL
[root@web01 blog]# chkconfig mysqld off #关闭其开机自启动,如果在/etc/rc.local也配置了,也去要注释掉
这时在访问我们先前配置本机数据库的网站,就会有如下提示。如果可以正常访问,可能是浏览器缓存的原因,注意清理缓存
7、在web主机上更改配置文件中数据库主机ip,改为新搭建的MySQL主机的IP
[root@web01 blog]# cd /application/nginx/html/blog/
[root@web01 blog]# vim wp-config.php
8、重新加载nginx,再访问自己搭建的blog
[root@web01 conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 conf]# /application/nginx/sbin/nginx -s reload
这时就可以正常访问了
写一篇新的文章发布,在去数据库查看数据是否增加
db01主机上查看数据
mysql> use wordpress;
mysql> select * from ysm_posts\G;
二、将blog的资源文件迁移到nfs网络文件系统
博主写的搭建nfs系统教程连接:https://blog.csdn.net/qq_39014511/article/details/89576931
资源文件指的是图片,音频,视频等这样的只要上传就基本不会改变的文件
查看一下web主机上我们自己的blog的资源文件,就是上篇中在wordpress写博客上传的图片文件
完整的目录是/application/nginx/html/blog/wp-content/uploads/
[root@web01 blog]# tree wp-content/uploads/
wp-content/uploads/
└── 2019
└── 04
├── ysm01-1024x963.jpg
├── ysm01-150x150.jpg
├── ysm01-300x282.jpg
├── ysm01-768x723.jpg
├── ysm01.jpg
├── ysm-150x150.jpg
├── ysm-225x300.jpg
└── ysm.jpg
2 directories, 8 files
多个主机访问一个共享目录,最理想的是建一个相同uid的用户,所以我们要在nfs服务端主机上建立uid相同的用户
查询到web主机的www用户信息,因为我的nginx的指定的进程用户是www
[root@web01 tools]# id www
uid=507(www) gid=507(www) 组=507(www)
查看web主机上rpc运行情况,一定要设为开机自启动
[root@web01 home]# /etc/init.d/rpcbind status
rpcbind (pid 1341) 正在运行...
1、nfs服务端准备工作
在nfs服务端主机指定uid添加www用户
[root@nfs01 ~]# useradd -u 507 www
[root@nfs01 ~]# id www
uid=507(www) gid=507(www) 组=507(www)
修改nfs的配置文件exports 如下,修改后重新加载nfs
并在共享目录/data下创建一个nfs-blog目录用于web主机挂载/application/nginx/html/blog/wp-content/uploads/
[root@nfs01 ~]# vim /etc/exports
#share /data by TQ for wordpress at 20190412
/data 172.16.1.0/24(rw,async,all_squash,anonuid=507,anongid=507)
~
[root@nfs01 ~]# /etc/init.d/nfs reload
[root@nfs01 /]# mkdir /data/nfs-blog
[root@nfs01 /]# chown -R www.www /data
2、在web01上挂载nfs
在挂载前 ,把/uploads/ 目录下的文件全部移动到其他目录,挂载完成再拷贝回来
挂载命令:mount -t nfs 172.16.1.31:/data/nfs-blog /application/nginx/html/blog/wp-content/uploads/
-t nfs 指定挂载文件系统类型,172.16.1.31是nfs服务端的IP地址
[root@web01 home]# mount -t nfs 172.16.1.31:/data/nfs-blog /application/nginx/html/blog/wp-content/uploads/
[root@web01 home]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 3.1G 15G 18% /
tmpfs 491M 12K 491M 1% /dev/shm
/dev/sda1 190M 35M 146M 19% /boot
172.16.1.31:/data 19G 1.8G 16G 10% /mnt
172.16.1.31:/data/nfs-blog
19G 1.8G 16G 10% /application/nginx-1.6.3/html/blog/wp-content/uploads
[root@web01 home]# umount /mnt #看到以前挂载了/mnt在nfs的/data目录上,就把他取消挂载了
[root@web01 home]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 3.1G 15G 18% /
tmpfs 491M 12K 491M 1% /dev/shm
/dev/sda1 190M 35M 146M 19% /boot
172.16.1.31:/data/nfs-blog
19G 1.8G 16G 10% /application/nginx-1.6.3/html/blog/wp-content/uploads
挂载完成,把原来wp-content/uploads/目录下的文件拷贝回去即可