前言
之前搭建的LAMP或LNMP都是在一台虚拟机上的,随着学习的深入,把Mysql数据库单独分离至独立的Mysql虚拟机上有利于分担服务器的压力,以下的mysql数据库迁移实验是在用LAMPW成功搭建博客后隔了一段时间后进行的。这里我就以LAMPW为例,这里我的站点目录为:/var/html/blog,域名为:blog.lg.com
其实LNMPW也是一样的,原理相同。下面的链接是LAMP的搭建过程以供参考
https://blog.csdn.net/zisefeizhu/article/details/80218087
实验环境准备
新克隆一台虚拟机,操作系统为Centos6.7,内核版本为2.6.32-573.el6.x86_64
主机名为web01_db01,IP为外网 10.0.0.6,内网172.16.1.6
CentOS release 6.7 (Final)
[root@web01_db01 ~]# uname -r
2.6.32-573.el6.x86_64
[root@web01_data ~]# hostname web01_db01
[root@web01_data ~]# bash
[root@web01_db01 ~]# vim /etc/sysconfig/network
HOSTNAME=web01_db01
[root@web01_db01 ~]# hostname -I
10.0.0.6 172.16.1.6
搭建数据库
[root@web01_db01 tmp]# mkdir /home/lk/tools -p
[root@web01_db01 tmp]# cd /home/lk/tools/
[root@web01_db0 tools]# wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.54-linux2.6-x86_64.tar.gz
# 获取mysql-5.5.54的二进制包
[root@web01_db0 tools]# wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.54-linux2.6-x86_64.tar.gz.md5
# 下载mysql-5.5.54的md5文件,校验是否有误
[root@web01_db0 tools]# cat mysql-5.5.54-linux2.6-x86_64.tar.gz.md5
cfdc6bbd88e58c6207ffa290bad531b1 mysql-5.5.54-linux2.6-x86_64.tar.gz
[root@web01_db0 tools]# md5sum -c mysql-5.5.54-linux2.6-x86_64.tar.gz.md5
mysql-5.5.54-linux2.6-x86_64.tar.gz: 确定
[root@web01_db01 tools]# useradd mysql -s /sbin/nologin -M
[root@web01_db01 tools]# tar -xf mysql-5.5.54-linux2.6-x86_64.tar.gz
[root@web01_db01 tools]# mv mysql-5.5.54-linux2.6-x86_64 /application/mysql-5.5.54
mv: 无法将"mysql-5.5.54-linux2.6-x86_64" 移动至"/application/mysql-5.5.54": 没有那个文件或目录
[root@web01_db01 tools]# mkdir /application
[root@web01_db01 tools]# mv mysql-5.5.54-linux2.6-x86_64 /application/mysql-5.5.54
[root@web01_db01 tools]# ln -s /application/mysql-5.5.54/ /application/mysql
[root@web01_db01 tools]# ls /application/mysql
[root@web01_db01 tools]# cd /application/mysql/
[root@web01_db01 mysql]# chown -R mysql.mysql /application/mysql/
[root@web01_db01 mysql]# ./scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data/ --user=mysql
180315 15:18:46 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
180315 15:18:46 [Note] /application/mysql/bin/mysqld (mysqld 5.5.54) starting as process 2283 ...
OK
Filling help tables...
180315 15:18:46 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
180315 15:18:46 [Note] /application/mysql/bin/mysqld (mysqld 5.5.54) starting as process 2290 ...
OK
[root@web01_db01 mysql]# ll -d /tmp/
drwxrwxrwt. 3 root root 4096 3月 15 15:18 /tmp/
[root@web01_db01 mysql]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.8 web01 www.lg.com lg.com blog.lg.com
172.16.1.6 web01_db01
[root@web01_db01 mysql]# cd bin/
[root@web01_db01 bin]# sed 's#/usr/local/#/application/#g' mysqld_safe |grep application
[root@web01_db01 bin]# sed -i 's#/usr/local/#/application/#g' mysqld_safe
[root@web01_db01 bin]# cd ..
[root@web01_db01 mysql]# cp support-files/my-small.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
[root@web01_db01 mysql]# /application/mysql/bin/mysqld_safe --user=mysql &
[1] 2309
[root@web01_db01 mysql]# Logging to '/application/mysql/data/web01_db01.err'.
180616 18:24:39 mysqld_safe Starting mysqld daemon with databases from /application/mysql/data
[root@web01_db01 mysql]# mkdir /var/lib/mysql
[root@web01_db01 mysql]# /application/mysql/bin/mysqld_safe --user=mysql &
[2] 2553
[root@web01_db01 mysql]# 180616 18:25:08 mysqld_safe Logging to '/application/mysql/data/web01_db01.er
180616 18:25:08 mysqld_safe A mysqld process already exists
[2]+ Exit 1 /application/mysql/bin/mysqld_safe --user=mysql
[root@web01_db01 mysql]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 2532 mysql 10u IPv4 19149 0t0 TCP *:mysql (LISTEN)
[root@web01_db01 mysql]# /application/mysql/bin/mysql
mysql> exit
Bye
[root@web01_db01 mysql]# echo "PATH="/application/mysql/bin:$PATH"" >>/etc/profile
[root@web01_db01 mysql]# source /etc/profile
[root@web01_db01 mysql]# tail -1 /etc/profile
PATH=/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@web01_db01 mysql]# echo $PATH
/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@web01_db01 mysql]# sed 's#/usr/local/#/application/#g' support-files/mysql.server |grep applicat
[root@web01_db01 mysql]# sed -i 's#/usr/local/#/application/#g' support-files/mysql.server
[root@web01_db01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@web01_db01 mysql]# chmod +x /etc/init.d/mysqld
[root@web01_db01 mysql]# /etc/init.d/mysqld stop
Shutting down MySQL.180616 18:26:35 mysqld_safe mysqld from pid file /application/mysql/data/web01_db0
SUCCESS!
[1]+ Done /application/mysql/bin/mysqld_safe --user=mysql
[root@web01_db01 mysql]# lsof -i :3306
[root@web01_db01 mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[root@web01_db01 mysql]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 3042 mysql 10u IPv4 19928 0t0 TCP *:mysql (LISTEN)
[root@web01_db01 mysql]# chkconfig --add mysqld
[root@web01_db01 mysql]# chkconfig mysqld on
[root@web01_db01 mysql]# chkconfig --list mysqld
mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@web01_db01 mysql]# tail -10 /application/mysql/data/web01_db01.err
[root@web01_db01 mysql]# cd /tmp/
##下面两行是在LAMP服务器上进行的操作
[root@web01 tools]# mysqldump -uroot -p123456 wordpress -B |gzip>bak.sql.gz
[root@web01 tools]# scp bak.sql.gz root@10.0.0.6:/tmp
[root@web01_db01 tmp]# gzip -d bak.sql.gz
[root@web01_db01 tmp]# mysql -uroot -p123456 </tmp/bak.sql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@web01_db01 tmp]# mysqladmin -u root password '123456'
[root@web01_db01 tmp]# mysql -uroot -p123456 </tmp/bak.sql
[root@web01_db01 tmp]# mysql -uroot -p123456 -e "show databases like 'wordpress'"
+----------------------+
| Database (wordpress) |
+----------------------+
| wordpress |
+----------------------+
[root@web01_db01 tmp]# mysql -uroot -p123456
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> exit
Bye
下面请关闭LAMP上的Mysql服务,以进行验证数据库是否迁移成功
[root@web01 tools]# chkconfig mysqld off
[root@web01 tools]# /etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS!
注:上述英文意思:建立数据库连接时出错。为什么会出错呢?数据库导入显示是成功。这时候我们应该想到的原因是:是不是Wordpress博客是需要数据库支持的,那么一定存在有关数据库的控制配置文件。
[root@web01 ~]# vim /var/html/blog/wp-config.php
31 /** MySQL主机 */ 把这个配置文件中的主机名改成db01(172.16.1.6)
32 define('DB_HOST', '172.16.1.6');
再次进行测试
最后一句:进行数据库的分离 不仅仅有利于分担LAMP服务器的压力,更有利于数据的安全性!