一、拆分数据库,转至db01服务器:
1、web01服务器操作如下:
1、导出mysql数据库(由于是root用户,所以不需要密码,直接回车即可):
[root@web01 ~]# mysqldump -uroot -p -A |gzip >/root/all.sql.gz
Enter password:
2、把数据库推送给db01服务器:
[root@web01 ~]# scp all.sql.gz root@10.0.0.51:/root
2、db01数据库服务器操作如下( 在db01上安装数据库,然后倒入数据库):
1、在db01上安装mariadb:
yum install mariadb mariadb-server -y
2、查看mysql的端口和进程:
[root@db01 ~]# ss -lntup|grep 3306
tcp LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=7362,fd=13))
3、查看进程:
[root@db01 ~]# ps -ef |grep mysql
mysql 7178 1 0 08:26 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql 7362 7178 0 08:26 ? 00:00:02 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 8576 7639 0 09:25 pts/0 00:00:00 grep --color=auto mysql
4、检查现有数据库状态:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | db01 |
| root | db01 |
| | localhost |
| root | localhost |
+------+-----------+
6 rows in set (0.00 sec)
MariaDB [(none)]> Bye
5、对数据库文件进行解压:
[root@db01 ~]# ll all.sql.gz
-rw-r--r-- 1 root root 276731 Jun 12 09:16 all.sql.gz
[root@db01 ~]# gzip -d all.sql.gz
6、把web01数据库倒入db01里面(数据库更新后一定要更新库):
[root@db01 ~]#mysql -uroot -p < /root/all.sql
[root@mysql-15 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
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)]> flush privileges; //数据库倒入后重启数据库,然后就可以登陆进数据库,否则失败
Query OK, 0 rows affected (0.01 sec)
[root@db01 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
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 |
| mysql |
| performance_schema |
| test |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> select user,host from mysql.user;
+-----------+------------+
| user | host |
+-----------+------------+
| root | 127.0.0.1 |
| wordpress | 172.16.1.% |
| root | ::1 |
| root | localhost |
| wordpress | localhost |
| root | web01 |
+-----------+------------+
6 rows in set (0.00 sec)
MariaDB [(none)]>
7、导入数据库后,查看本地是否能够连接数据库:
[root@db01 ~]# mysql -uwordpress -p123456 -h 172.16.1.51
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
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)]>
二、web01代码修改(修改文件里调用数据库文件),关闭web01数据库,查看远程是否可以连接数据库:
2.1 web01代码修改(修改文件里调用数据库文件):
[root@web01 /usr/share/nginx/html/blog]# grep -i db_host wp-config.php
define( 'DB_HOST', '172.16.1.51' );
备注:文件内容是localhost的时候,网页登录域名显示无法找到数据库(无法正常连接);文件内容改成数据库ip后就可以远程连接到数据库,网页可以正常连接。用这个办法可以验证数据库分离是否成功
2.2、 关闭web01数据库
[root@web01 /usr/share/nginx/html/blog]# systemctl stop mariadb.service
2.3、 查看远程是否可以连接数据库:
[root@web01 /usr/share/nginx/html/blog]# mysql -uwordpress -p123456 -h 172.16.1.51
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
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)]> Bye
三、在nfs下面进行共享目录环境搭建:
[root@nfs01 ~]# cat /etc/exports
/webdata 172.16.1.0/24(rw,all_squash,anonuid=2222,anongid=2222)
[root@nfs01 ~] useradd -u2222 -s /sbin/nologin -M nginx =//创建nginx虚拟用户
[root@nfs01 ~]# id nginx
uid=2222(nginx) gid=2222(nginx) groups=2222(nginx)
[root@nfs01 ~] mkdir -p /webdata =//创建目录
[root@nfs01 ~] chown nginx.nginx /webdata =//对目录进行更改nginx用户
[root@nfs01 ~]# systemctl restart rpcbind
[root@nfs01 ~]# systemctl restart nfs
[root@nfs01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/webdata 172.16.1.0/24
四、在web01下面进行共享目录挂载:
3.1 先把uploads目录下的所有用户上传文件进行备份(因为这个目录进行nfs服务挂载的时候会默认与nfs服务器下的共享目录文件保持一致)
[root@web01 /usr/share/nginx/html/blog]# mv wp-content/uploads/ /tmp/
3.2、 创建uploads目录(因为值钱把这个目录已经移动到了tmp下)
[root@web01 /usr/share/nginx/html/blog]# mkdir -p /usr/share/nginx/html/blog/wp-content/uploads/
3.3、进行挂载:
[root@web01 /usr/share/nginx/html/blog]# mount -t nfs 172.16.1.31:/webdata /usr/share/nginx/html/blog/wp-content/uploads/
3.4、查询是否挂载成功:
[root@web01 /usr/share/nginx/html/blog]# df -h
df: ‘/upload’: Stale file handle
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 99G 2.1G 97G 3% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 197M 105M 93M 54% /boot
tmpfs 98M 0 98M 0% /run/user/0
172.16.1.31:/webdata 99G 1.7G 98G 2% /usr/share/nginx/html/blog/wp-content/uploads
3.5、把之前uploads目录下的用户文件,重新放回新创建的uploads目录下:
[root@web01 /usr/share/nginx/html/blog]# mv /tmp/uploads/* wp-content/uploads/
mv: failed to preserve ownership for ‘wp-content/uploads/2019/06/461273802.jpg’: Operation not permitted
mv: failed to preserve ownership for ‘wp-content/uploads/2019/06/461273802-150x150.jpg’: Operation not permitted
mv: failed to preserve ownership for ‘wp-content/uploads/2019/06/461273802-219x300.jpg’: Operation not permitted
mv: failed to preserve ownership for ‘wp-content/uploads/2019/06/1499239092.png’: Operation not permitted
mv: failed to preserve ownership for ‘wp-content/uploads/2019/06/1499239092-150x55.png’: Operation not permitted
mv: failed to preserve ownership for ‘wp-content/uploads/2019/06/linux学院-桌面背景.jpg’: Operation not permitted
mv: failed to preserve ownership for ‘wp-content/uploads/2019/06/linux学院-桌面背景-150x150.jpg’: Operation not permitted
mv: failed to preserve ownership for ‘wp-content/uploads/2019/06/linux学院-桌面背景-300x188.jpg’: Operation not permitted
mv: failed to preserve ownership for ‘wp-content/uploads/2019/06/linux学院-桌面背景-768x480.jpg’: Operation not permitted
mv: failed to preserve ownership for ‘wp-content/uploads/2019/06/linux学院-桌面背景-1024x640.jpg’: Operation not permitted
mv: failed to preserve ownership for ‘wp-content/uploads/2019/06’: Operation not permitted
mv: failed to preserve ownership for ‘wp-content/uploads/2019’: Operation not permitted
[root@web01 /usr/share/nginx/html/blog]# ll wp-content/uploads/
total 0
drwxr-xr-x 3 nginx nginx 16 Jun 10 12:12 2019