mysqldump和xtrabackup两种方式进行非空库复制搭建

mysqldump和xtrabackup两种方式进行非空库复制搭建

1、mysqldump非空库复制搭建

  • 背景
    • 主库:IP:192.168.1.21;版本:5.7.18
    • 备库:IP:192.168.1.128;版本:5.6.36
  • 从库上先停掉复制
  
  
  1. root@localhost : (none) 01:57:45> stop slave;
  2. Query OK, 0 rows affected (0.02 sec)
  3. root@localhost : (none) 01:57:54> reset slave;
  4. Query OK, 0 rows affected (0.10 sec)
  5. root@localhost : (none) 01:57:58> show slave status\G;
  6. *************************** 1. row ***************************
  7. Slave_IO_State:
  8. Master_Host: 192.168.1.21
  9. Master_User: repl
  10. Master_Port: 3306
  11. Connect_Retry: 60
  12. Master_Log_File:
  13. Read_Master_Log_Pos: 4
  14. Relay_Log_File: mysql-relay-bin.000001
  15. Relay_Log_Pos: 4
  16. Relay_Master_Log_File:
  17. Slave_IO_Running: No
  18. Slave_SQL_Running: No
  19. Replicate_Do_DB:
  20. Replicate_Ignore_DB:
  21. Replicate_Do_Table:
  22. Replicate_Ignore_Table:
  23. Replicate_Wild_Do_Table:
  24. Replicate_Wild_Ignore_Table:
  25. Last_Errno: 0
  26. Last_Error:
  27. Skip_Counter: 0
  28. Exec_Master_Log_Pos: 0
  29. Relay_Log_Space: 143
  30. Until_Condition: None
  31. Until_Log_File:
  32. Until_Log_Pos: 0
  33. Master_SSL_Allowed: No
  34. Master_SSL_CA_File:
  35. Master_SSL_CA_Path:
  36. Master_SSL_Cert:
  37. Master_SSL_Cipher:
  38. Master_SSL_Key:
  39. Seconds_Behind_Master: NULL
  40. Master_SSL_Verify_Server_Cert: No
  41. Last_IO_Errno: 0
  42. Last_IO_Error:
  43. Last_SQL_Errno: 0
  44. Last_SQL_Error:
  45. Replicate_Ignore_Server_Ids:
  46. Master_Server_Id: 12001
  47. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  48. Master_Info_File: mysql.slave_master_info
  49. SQL_Delay: 0
  50. SQL_Remaining_Delay: NULL
  51. Slave_SQL_Running_State:
  52. Master_Retry_Count: 86400
  53. Master_Bind:
  54. Last_IO_Error_Timestamp:
  55. Last_SQL_Error_Timestamp:
  56. Master_SSL_Crl:
  57. Master_SSL_Crlpath:
  58. Retrieved_Gtid_Set:
  59. Executed_Gtid_Set:
  60. Auto_Position: 0
  61. 1 row in set (0.00 sec)
  62. root@localhost : (none) 01:58:00> reset slave all;
  63. Query OK, 0 rows affected (0.00 sec)
  64. root@localhost : (none) 01:58:17> show slave status;
  65. Empty set (0.00 sec)
  • 在主库上创建新的测试数据
  
  
  1. mysql> create database sbtest;
  2. Query OK, 1 row affected (0.02 sec)
  3. mysql> use sbtest;
  4. Database changed
  5. mysql> create table xiyouji(
  6. -> id int,
  7. -> role varchar(11) );
  8. Query OK, 0 rows affected (0.14 sec)
  9. mysql> insert into xiyouji values (1,'songwukong'),(2,'tangsanzang'),(3,'zhuwuneng'),(4,'shawujing');
  10. Query OK, 4 rows affected (0.02 sec)
  11. Records: 4 Duplicates: 0 Warnings: 0
  12. mysql> select * from xiyouji;
  13. +------+-------------+
  14. | id | role |
  15. +------+-------------+
  16. | 1 | songwukong |
  17. | 2 | tangsanzang |
  18. | 3 | zhuwuneng |
  19. | 4 | shawujing |
  20. +------+-------------+
  21. 4 rows in set (0.00 sec)
  • mysqldump把数据导出并导入到从库中
  
  
  1. [root@host-192-168-1-21 mysqldump]# mysqldump -uroot -p -S /usr/local/mysql/mysql.sock --single-transaction --master-data=2 --flush-logs --all-databases > ./master_slave.sql
  2. [root@host-192-168-1-21 mysqldump]# mysql -uroot -h192.168.1.128 < ./master_slave.sql
  3. ERROR 1726 (HY000) at line 232: Storage engine 'InnoDB' does not support system tables. [mysql.help_category]
  4. #原因:主库是5.7版本,从库是5.6版本。mysql.help_category这个系统表在5.7中有,但是5.6中没有。当然不止这一个表,sys整个库都是新增的。
  • 不备份系统库表,只备份业务表
  
  
  1. [root@host-192-168-1-21 mysqldump]# mysqldump -uroot -p -S /usr/local/mysql/mysql.sock --single-transaction --master-data=2 --flush-logs sbtest > ./master_slave.sql
  2. [root@host-192-168-1-21 mysqldump]# mysql -uroot -h192.168.1.128 < ./master_slave.sql
  3. ERROR 1046 (3D000) at line 28: No database selected
  4. #原因;这个报错原因就是之前在练习mysqldump备份时候遇到过的,原因如下:
  5. mysqldump -uroot -S /usr/local/mysql5711/mysql.sock -p --single-transaction --master-data=2 --flush-logs --databases sbtest > ./danku.sql
  6. mysqldump -uroot -S /usr/local/mysql5711/mysql.sock -p --single-transaction --master-data=2 --flush-logs sbtest > ./danku2.sql
  7. #区别:前者加了--databases参数,生成的sql文件中含有CREATE DATABASE语句;后者不加这个参数,直接跟单库名,生成的sql文件中没有CREATE DATABASE语句。
  • 重新导出sql文件
  
  
  1. [root@host-192-168-1-21 mysqldump]# mysqldump -uroot -p -S /usr/local/mysql/mysql.sock --single-transaction --master-data=2 --flush-logs --databases sbtest > ./master_slave.sql
  2. [root@host-192-168-1-21 mysqldump]# mysql -uroot -h192.168.1.128 < ./master_slave.sql
  3. [root@host-192-168-1-21 mysqldump]# cat master_slave.sql |grep -i 'change master'
  4. -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000025', MASTER_LOG_POS=154;
  5. mysql> show master status;
  6. +------------------+----------+--------------+------------------+-------------------+
  7. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  8. +------------------+----------+--------------+------------------+-------------------+
  9. | mysql-bin.000025 | 154 | | | |
  10. +------------------+----------+--------------+------------------+-------------------+
  11. 1 row in set (0.00 sec)
  • 在从库上查看数据是否已经导入成功
  
  
  1. root@localhost : mysql 02:28:24> use sbtest;
  2. Database changed
  3. root@localhost : sbtest 02:28:39> show tables;
  4. +------------------+
  5. | Tables_in_sbtest |
  6. +------------------+
  7. | xiyouji |
  8. +------------------+
  9. 1 row in set (0.00 sec)
  10. root@localhost : sbtest 02:28:42> select * from xiyouji;
  11. +------+-------------+
  12. | id | role |
  13. +------+-------------+
  14. | 1 | songwukong |
  15. | 2 | tangsanzang |
  16. | 3 | zhuwuneng |
  17. | 4 | shawujing |
  18. +------+-------------+
  19. 4 rows in set (0.00 sec)
  • 设置主从复制
  
  
  1. root@localhost : mysql 02:22:53> show slave status;
  2. Empty set (0.00 sec)
  3. root@localhost : mysql 02:26:54> CHANGE MASTER TO master_host='192.168.1.21',master_user='repl',master_password='repl',MASTER_LOG_FILE='mysql-bin.000025', MASTER_LOG_POS=154;
  4. Query OK, 0 rows affected, 2 warnings (0.16 sec)
  5. root@localhost : mysql 02:28:15> start slave;
  6. Query OK, 0 rows affected (0.02 sec)
  7. root@localhost : sbtest 02:30:28> show slave status\G;
  8. *************************** 1. row ***************************
  9. Slave_IO_State: Waiting for master to send event
  10. Master_Host: 192.168.1.21
  11. Master_User: repl
  12. Master_Port: 3306
  13. Connect_Retry: 60
  14. Master_Log_File: mysql-bin.000025
  15. Read_Master_Log_Pos: 154
  16. Relay_Log_File: mysql-relay-bin.000002
  17. Relay_Log_Pos: 286
  18. Relay_Master_Log_File: mysql-bin.000025
  19. Slave_IO_Running: Yes
  20. Slave_SQL_Running: Yes
  21. Replicate_Do_DB:
  22. Replicate_Ignore_DB:
  23. Replicate_Do_Table:
  24. Replicate_Ignore_Table:
  25. Replicate_Wild_Do_Table:
  26. Replicate_Wild_Ignore_Table:
  27. Last_Errno: 0
  28. Last_Error:
  29. Skip_Counter: 0
  30. Exec_Master_Log_Pos: 154
  31. Relay_Log_Space: 459
  32. Until_Condition: None
  33. Until_Log_File:
  34. Until_Log_Pos: 0
  35. Master_SSL_Allowed: No
  36. Master_SSL_CA_File:
  37. Master_SSL_CA_Path:
  38. Master_SSL_Cert:
  39. Master_SSL_Cipher:
  40. Master_SSL_Key:
  41. Seconds_Behind_Master: 0
  42. Master_SSL_Verify_Server_Cert: No
  43. Last_IO_Errno: 0
  44. Last_IO_Error:
  45. Last_SQL_Errno: 0
  46. Last_SQL_Error:
  47. Replicate_Ignore_Server_Ids:
  48. Master_Server_Id: 12001
  49. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  50. Master_Info_File: mysql.slave_master_info
  51. SQL_Delay: 0
  52. SQL_Remaining_Delay: NULL
  53. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  54. Master_Retry_Count: 86400
  55. Master_Bind:
  56. Last_IO_Error_Timestamp:
  57. Last_SQL_Error_Timestamp:
  58. Master_SSL_Crl:
  59. Master_SSL_Crlpath:
  60. Retrieved_Gtid_Set:
  61. Executed_Gtid_Set:
  62. Auto_Position: 0
  63. 1 row in set (0.00 sec)
  64. #主从复制同步成功
  • 验证
  
  
  1. #主库上新建表,并插入数据
  2. mysql> use sbtest;
  3. Database changed
  4. mysql> create table sanguoyanyi(
  5. -> id int,
  6. -> people varchar(13) );
  7. Query OK, 0 rows affected (0.09 sec)
  8. mysql> insert into sanguoyanyi values (1,'caocao'),(2,'guojia');
  9. Query OK, 2 rows affected (0.02 sec)
  10. Records: 2 Duplicates: 0 Warnings: 0
  11. mysql> select * from sanguoyanyi;
  12. +------+--------+
  13. | id | people |
  14. +------+--------+
  15. | 1 | caocao |
  16. | 2 | guojia |
  17. +------+--------+
  18. 2 rows in set (0.00 sec)
  
  
  1. #从库上查看数据是否同步成功:
  2. root@localhost : sbtest 02:30:32> show tables;
  3. +------------------+
  4. | Tables_in_sbtest |
  5. +------------------+
  6. | sanguoyanyi |
  7. | xiyouji |
  8. +------------------+
  9. 2 rows in set (0.00 sec)
  10. root@localhost : sbtest 02:34:24> select * from sanguoyanyi;
  11. +------+--------+
  12. | id | people |
  13. +------+--------+
  14. | 1 | caocao |
  15. | 2 | guojia |
  16. +------+--------+
  17. 2 rows in set (0.00 sec)

2、xtrabackup非空库复制搭建

  • 背景
    • 主库:IP:192.168.1.21;版本:5.7.18
    • 备库:IP:192.168.1.128;版本:5.7.18 
      - 在128上新搭5.7.18版本的mysql。版本不同的mysql server用全库物理备份会出现各种问题。
  • 清空192.168.1.128从库
  
  
  1. root@localhost : sbtest 02:39:32> drop database sbtest;
  2. Query OK, 2 rows affected, 1 warning (0.06 sec)
  • 在从库上安装xtrabackup
  
  
  1. [root@host-192-168-1-128 program]# wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.7/binary/redhat/6/x86_64/percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm
  2. [root@host-192-168-1-128 program]# rpm -ivh percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm
  3. warning: percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
  4. error: Failed dependencies:
  5. libev.so.4()(64bit) is needed by percona-xtrabackup-24-2.4.7-1.el6.x86_64
  6. perl(DBD::mysql) is needed by percona-xtrabackup-24-2.4.7-1.el6.x86_64
  7. rsync is needed by percona-xtrabackup-24-2.4.7-1.el6.x86_64
  8. [root@host-192-168-1-128 program]# yum install rsync
  9. [root@host-192-168-1-128 program]# yum install perl-DBD-MySQL
  10. [root@host-192-168-1-128 program]# wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/libev-4.15-1.el6.rf.x86_64.rpm
  11. [root@host-192-168-1-128 program]# ls
  12. libev-4.15-1.el6.rf.x86_64.rpm percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm
  13. [root@host-192-168-1-128 program]# rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm
  14. warning: libev-4.15-1.el6.rf.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEY
  15. Preparing... ########################################### [100%]
  16. 1:libev ########################################### [100%]
  17. [root@host-192-168-1-128 program]# rpm -ivh percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm
  18. warning: percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
  19. Preparing... ########################################### [100%]
  20. 1:percona-xtrabackup-24 ########################################### [100%]
  • 主库上的业务库信息
  
  
  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | performance_schema |
  8. | sbtest |
  9. | sys |
  10. +--------------------+
  11. 5 rows in set (0.00 sec)
  12. mysql> use sbtest
  13. Database changed
  14. mysql> show tables;
  15. +------------------+
  16. | Tables_in_sbtest |
  17. +------------------+
  18. | sanguoyanyi |
  19. | xiyouji |
  20. +------------------+
  21. 2 rows in set (0.00 sec)
  22. mysql> select * from sanguoyanyi;
  23. +------+--------+
  24. | id | people |
  25. +------+--------+
  26. | 1 | caocao |
  27. | 2 | guojia |
  28. +------+--------+
  29. 2 rows in set (0.00 sec)
  30. mysql> select * from xiyouji;
  31. +------+-------------+
  32. | id | role |
  33. +------+-------------+
  34. | 1 | songwukong |
  35. | 2 | tangsanzang |
  36. | 3 | zhuwuneng |
  37. | 4 | shawujing |
  38. +------+-------------+
  39. 4 rows in set (0.00 sec)
  • 在主库上用innobackup备份数据并传输到从库服务器。注意:两台机器之间需要ssh免密钥登录
  
  
  1. innobackupex --defaults-file=/usr/local/mysql/my.cnf --user=root --password=123 --no-timestamp --stream='tar' /home/backup/xtrabackup/ |ssh root@192.168.1.128 "cat - > /home/backup/backup_`date +%y%m%d`.tar"
  • 将主库的配置文件拷贝到从库
  
  
  1. scp /usr/local/mysql/my.cnf root@192.168.1.128:/usr/local/mysql/
  2. #修改server-id
  • 停掉从库的mysql
  
  
  1. [root@host-192-168-1-128 backup]# service mysql stop
  2. Shutting down MySQL.. SUCCESS!
  • 解压数据包,应用日志并做数据恢复
  
  
  1. [root@host-192-168-1-128 backup]# ls
  2. backup_170517.tar
  3. [root@host-192-168-1-128 backup]# pwd
  4. /home/backup
  5. [root@host-192-168-1-128 backup]# tar xvf backup_170517.tar
  6. [root@host-192-168-1-128 backup]# ls
  7. backup_170517.tar ib_buffer_pool IBdata2 performance_schema sys xtrabackup_checkpoints xtrabackup_logfile
  8. backup-my.cnf IBdata1 mysql sbtest xtrabackup_binlog_info xtrabackup_info
  9. [root@host-192-168-1-128 backup]# innobackupex --apply-log --read-only /home/backup/
  10. [root@host-192-168-1-128 data]# innobackupex --defaults-file=/usr/local/mysql/my.cnf --copy-back /home/backup/
  11. 170517 04:47:15 completed OK!
  • 修改datadir属组
  
  
  1. [root@host-192-168-1-128 home]# chown -R mysql:mysql /home/data/
  • 启动mysql
  
  
  1. mysqld_safe --defaults-file=/usr/local/mysql/my.cnf --user=mysql
  2. ps -ef|grep mysql
  3. 确定是否运行
  • 查看数据,和主库一致
  • 设置主从复制
  
  
  1. [root@host-192-168-1-128 backup]# cat xtrabackup_binlog_info
  2. mysql-bin.000025 639
  3. root@localhost : mysql 09:22:53> CHANGE MASTER TO master_host='192.168.1.21',master_user='repl',master_password='repl',MASTER_LOG_FILE='mysql-bin.000025', MASTER_LOG_POS=639;
  4. root@localhost : sbtest 09:25:28> show slave status\G;
  5. *************************** 1. row ***************************
  6. Slave_IO_State: Waiting for master to send event
  7. Master_Host: 192.168.1.21
  8. Master_User: repl
  9. Master_Port: 3306
  10. Connect_Retry: 60
  11. Master_Log_File: mysql-bin.000025
  12. Read_Master_Log_Pos: 639
  13. Relay_Log_File: mysql-relay-bin.000001
  14. Relay_Log_Pos: 286
  15. Relay_Master_Log_File: mysql-bin.000025
  16. Slave_IO_Running: Yes
  17. Slave_SQL_Running: Yes
  18. Replicate_Do_DB:
  19. Replicate_Ignore_DB:
  20. Replicate_Do_Table:
  21. Replicate_Ignore_Table:
  22. Replicate_Wild_Do_Table:
  23. Replicate_Wild_Ignore_Table:
  24. Last_Errno: 0
  25. Last_Error:
  26. Skip_Counter: 0
  27. Exec_Master_Log_Pos: 639
  28. Relay_Log_Space: 459
  29. Until_Condition: None
  30. Until_Log_File:
  31. Until_Log_Pos: 0
  32. Master_SSL_Allowed: No
  33. Master_SSL_CA_File:
  34. Master_SSL_CA_Path:
  35. Master_SSL_Cert:
  36. Master_SSL_Cipher:
  37. Master_SSL_Key:
  38. Seconds_Behind_Master: 0
  39. Master_SSL_Verify_Server_Cert: No
  40. Last_IO_Errno: 0
  41. Last_IO_Error:
  42. Last_SQL_Errno: 0
  43. Last_SQL_Error:
  44. Replicate_Ignore_Server_Ids:
  45. Master_Server_Id: 12001
  46. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  47. Master_Info_File: mysql.slave_master_info
  48. SQL_Delay: 0
  49. SQL_Remaining_Delay: NULL
  50. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  51. Master_Retry_Count: 86400
  52. Master_Bind:
  53. Last_IO_Error_Timestamp:
  54. Last_SQL_Error_Timestamp:
  55. Master_SSL_Crl:
  56. Master_SSL_Crlpath:
  57. Retrieved_Gtid_Set:
  58. Executed_Gtid_Set:
  59. Auto_Position: 0
  60. 1 row in set (0.00 sec)
  61. #同步成功

猜你喜欢

转载自blog.csdn.net/wukong_666/article/details/72477678
今日推荐