LNMP环境中的数据库迁移为独立的服务器

环境: centos 6.5 ip:192.168.0.118  nginx、php、mysql

         centos 6.5 ip:192.168.0.117 mysql

现在我们需要把数据库迁移到192.168.0.117机器上:

首先我们需要在118机器上备份数据库:

mysqldump 最常用的备份工具:

逻辑备份:小于50G的数据量,4-6个小时ok

原理:将数据库的数据以逻辑的sql语句的方式导出 

物理备份:

  1. scp  /application/mysql  拷贝到独立数据库上就可以
  2. xtrabackup 开源的物理备份工具

下面我们以常用的逻辑来备份:

  • -A 备份所有库
  • -B 备份多个库,并添加use 库名:create database 库等的功能
  • -X 锁表,备份会影响读写,尽量晚上执行。
  • |gzip 压缩效率
  • .sql.gz 表示sql语句数据,.gz是压缩包。
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bqh                |
| jyw                |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
7 rows in set (0.06 sec)

mysql> quit
[root@bqh-118 mysql]# mysqldump -uroot -p123456 -A -B -X|gzip>/opt/bak_$(date +%F).sql.gz
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
[root@bqh-118 mysql]# ll /opt/
总用量 348
-rw-r--r-- 1 root root 354461 6月  30 12:56 bak_2019-06-30.sql.gz
[root@bqh-118 mysql]# rm -rf /opt/bak_2019-06-30.sql.gz 
[root@bqh-118 mysql]# mysqldump -uroot -p123456 -A -B -X --events|gzip>/opt/bak_$(date +%F).sql.gz  #备份全库
[root@bqh-118 mysql]# ll /opt/
总用量 348
-rw-r--r-- 1 root root 354463 6月  30 13:00 bak_2019-06-30.sql.gz
[root@bqh-118 mysql]# mysqldump -uroot -p123456  -B -X wordpress|gzip>/opt/bak_wordpress.sql.gz #只备份指定的库
[root@bqh-118 mysql]# ll /opt/
总用量 544
-rw-r--r-- 1 root root 354463 6月  30 13:00 bak_2019-06-30.sql.gz
-rw-r--r-- 1 root root 199211 6月  30 13:01 bak_wordpress.sql.gz

注:Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.

解决方法:mysqldump备份时加参数 --events

将备份的库scp到117机器上去:

[root@bqh-118 mysql]# scp -rp -P22 /opt/bak_wordpress.sql.gz root@192.168.0.117:/opt/
root@192.168.0.117's password: 
bak_wordpress.sql.gz                                              100%  195KB 194.5KB/s   00:00    
[root@bqh-118 mysql]# 

我们上117机器/opt/下查看是否推送过来了:

现在我们在117机器上恢复数据:

[root@bqh-117 opt]# ll
总用量 188
-rw-r--r--  1 root root 185857 6月  30 23:38 bak_wordpress.sql.gz
drwxr-xr-x. 2 root root   4096 11月 22 2013 rh
[root@bqh-117 opt]# gunzip bak_wordpress.sql.gz 
[root@bqh-117 opt]# ll
总用量 756
-rw-r--r--  1 root root 766279 6月  30 23:38 bak_wordpress.sql
drwxr-xr-x. 2 root root   4096 11月 22 2013 rh
[root@bqh-117 opt]# less bak_wordpress.sql 
[root@bqh-117 opt]# mysql -uroot -p123456 <bak_wordpress.sql

我们进入数据库查看是否导入了:

[root@bqh-117 opt]# mysql -uroot -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.5.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

mysql> use wordpress;
Database changed
mysql> show tables;
+-----------------------+
| Tables_in_wordpress   |
+-----------------------+
| bh_commentmeta        |
| bh_comments           |
| bh_links              |
| bh_options            |
| bh_postmeta           |
| bh_posts              |
| bh_term_relationships |
| bh_term_taxonomy      |
| bh_terms              |
| bh_usermeta           |
| bh_users              |
+-----------------------+
11 rows in set (0.00 sec)

mysql> 

我们现在打开web博客试试:

原因是我们没做授权和配置wp-config.php,现在我们要做117机器上给予授权:对于web来讲,数据授权:增删改查即可。

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | bqh-117   |
| root | bqh-117   |
|      | localhost |
| root | localhost |
+------+-----------+
6 rows in set (0.01 sec)

mysql> grant select,insert,update,delete on wordpress.* to wordpress@'192.168.0.%' identified by '123456';
Query OK, 0 rows affected (0.02 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 | 192.168.0.% |
| root      | ::1         |
|           | bqh-117     |
| root      | bqh-117     |
|           | localhost   |
| root      | localhost   |
+-----------+-------------+
7 rows in set (0.00 sec)

mysql> 

授权完后,我们需要修改wp-config.php配置文件:修改php连接文件

/** MySQL主机 */

define('DB_HOST', 'localhost');

localhost更改为”远端数据库ip或者域名”   #建议用域名

然后我们做hosts地址解析:

现在打开浏览器www.test.com

ok!

posted @ 2019-07-01 21:46  南清风  阅读(329)  评论(0编辑  收藏  举报