一篇搞懂MySQL 8.0 Clone技术在线搭建主从复制全过程

数据库 MySQL
MySQL从8.0.17开始新增了克隆Clone技术,可以在线进行MySQL的本地克隆或远程克隆,从此搭建从库可以不再需要备份工具来实现了,本文分享Clone技术在线搭建主从复制全过程,希望对大家有帮助。

本文将通过Clone来实现一个远程从库的搭建:环境如下

Mater: 192.168.3.133 port:3307 doner 捐赠者

Slave: 192.168.3.134 port:3307 recipient 接受者

第一步:两台机器MySQL分别进行初始化安装

进入MySQL软件目录进行初始化安装并修改密码:(MySQL8.0.19 下载解压步骤省略) 

  1. [root@mgr2 bin]#cd /zcloud/db/abcMgr/abcMgr02/mysql/bin  
  2. [root@mgr2 bin]# ./mysqld --initialize --user=mysql  
  3. [root@mgr2 bin]# ./mysqld_safe --user=mysql &  
  4. [1] 19556  
  5. [root@mgr2 bin]# 2020-03-12T01:32:26.503048Z mysqld_safe Logging to ‘/rock/mysqldata/error.log’.  
  6. 2020-03-12T01:32:26.536292Z mysqld_safe Starting mysqld daemon with databases from /rock/mysqldata  
  7. [root@mgr2 bin]#  
  8. [root@mgr2 bin]# mysql -uroot -p -P3307  
  9. Enter password:  
  10. Welcome to the MySQL monitor. Commands end with ; or \g.  
  11. Your MySQL connection id is 8  
  12. Server version: 8.0.19  
  13. Copyright © 2000, 2019, Oracle and/or its affiliates. All rights reserved.  
  14. Oracle is a registered trademark of Oracle Corporation and/or its  
  15. affiliates. Other names may be trademarks of their respective  
  16. owners.  
  17. Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.  
  18. root@localhost:(none) 09:33:06 >alter user user() identified by ‘root1234’;  
  19. Query OK, 0 rows affected (0.00 sec) 
  20. root@localhost:(none) 09:33:26 >flush privileges;  
  21. Query OK, 0 rows affected (0.00 sec)  
  22. root@localhost:(none) 09:33:31 >exit  
  23. Bye 

第二步:doner节点192.168.3.133相关操作

–创建用户 

  1. root@localhost:(none) 10:07:05 >create user ‘donor_user’@‘192.168.3.134’ identified by ‘password’;  
  2. Query OK, 0 rows affected (0.00 sec)  
  3. root@localhost:(none) 10:07:07 >grant backup_admin on . to ‘donor_user’@‘192.168.3.134’;  
  4. Query OK, 0 rows affected (0.01 sec) 

–安装clone插件 

  1. root@localhost:(none) 10:23:16 >install plugin clone soname ‘mysql_clone.so’;  
  2. Query OK, 0 rows affected (0.01 sec) 

第三步:recipient节点192.168.3.134相关操作

–创建用户(也可以不创建用户,用root直接操作) 

  1. mysql> create user ‘recipient_user’@‘192.168.3.134’ identified by ‘password’; 

–安装clone插件 

  1. mysql> grant clone_admin on . to ‘recipient_user’@‘192.168.3.134’; 

–设置参数clone_valid_donor_list 

  1. root@localhost:(none) 03:28:40 >set global clone_valid_donor_list=‘192.168.3.133:3307’;  
  2. Query OK, 0 rows affected (0.00 sec) 

–换成recipient_user’@‘192.168.3.134’ 用户登陆,执行clone语句(这里其实可以用本地root用户直接进行登录操作) 

  1. [root@mgr3 bin]# mysql -urecipient_user -ppassword -P3307 -h192.168.3.134  
  2. recipient_user@192.168.3.134:(none) 03:39:46 >clone instance from ‘donor_user’@‘192.168.3.133’:3307 identified by ‘password’;  
  3. Query OK, 0 rows affected (2.34 sec)  
  4. recipient_user@192.168.3.134:(none) 03:39:57 >Restarting mysqld…  
  5. 2020-03-12T07:40:01.285267Z mysqld_safe Number of processes running now: 0  
  6. 2020-03-12T07:40:01.290169Z mysqld_safe mysqld restarted 

至此,远程数据的克隆已经完成了。

通过查询两张表来监控一下克隆的进度和结果状态:

–查看clone进度和状态 

  1. root@localhost:(none) 03:34:49 >SELECT * FROM performance_schema.clone_progress;  
  2. ±-----±----------±----------±---------------------------±---------------------------±--------±----------±----------±----------±-----------±--------------+ 
  3. | ID | STAGE | STATE | BEGIN_TIME | END_TIME | THREADS | ESTIMATE | DATA | NETWORK | DATA_SPEED | NETWORK_SPEED |  
  4. ±-----±----------±----------±---------------------------±---------------------------±--------±----------±----------±----------±-----------±--------------+ 
  5. | 1 | DROP DATA | Completed | 2020-03-12 15:29:15.385694 | 2020-03-12 15:29:15.634609 | 1 | 0 | 0 | 0 | 0 | 0 |  
  6. | 1 | FILE COPY | Completed | 2020-03-12 15:29:15.634765 | 2020-03-12 15:29:17.452961 | 1 | 465800520 | 465800520 | 465833356 | 0 | 0 |  
  7. | 1 | PAGE COPY | Completed | 2020-03-12 15:29:17.453144 | 2020-03-12 15:29:17.554224 | 1 | 0 | 0 | 99 | 0 | 0 |  
  8. | 1 | REDO COPY | Completed | 2020-03-12 15:29:17.554413 | 2020-03-12 15:29:17.654430 | 1 | 2560 | 2560 | 3031 | 0 | 0 |  
  9. | 1 | FILE SYNC | Completed | 2020-03-12 15:29:17.654596 | 2020-03-12 15:29:17.730172 | 1 | 0 | 0 | 0 | 0 | 0 |  
  10. | 1 | RESTART | Completed | 2020-03-12 15:29:17.730172 | 2020-03-12 15:29:22.160372 | 0 | 0 | 0 | 0 | 0 | 0 |  
  11. | 1 | RECOVERY | Completed | 2020-03-12 15:29:22.160372 | 2020-03-12 15:29:22.478889 | 0 | 0 | 0 | 0 | 0 | 0 |  
  12. ±-----±----------±----------±---------------------------±---------------------------±--------±----------±----------±----------±-----------±--------------+  
  13. 7 rows in set (0.00 sec)  
  14. root@localhost:(none) 03:34:52 >SELECT * FROM performance_schema.clone_status\G  
  15. *************************** 1. row ***************************  
  16. ID: 1  
  17. PID: 0  
  18. STATE: Completed  
  19. BEGIN_TIME: 2020-03-12 15:29:15.385  
  20. END_TIME: 2020-03-12 15:29:22.479  
  21. SOURCE: 192.168.3.133:3307  
  22. DESTINATION: LOCAL INSTANCE  
  23. ERROR_NO: 0  
  24. ERROR_MESSAGE:  
  25. BINLOG_FILE: mysql-bin.000002  
  26. BINLOG_POSITION: 421  
  27. GTID_EXECUTED: 3e75bf2f-6401-11ea-8995-000c29db65a6:1  
  28. 1 row in set (0.00 sec) 

–在主库133上建立复制账号: 

  1. root@localhost:(none) 04:12:23 >create user repl@‘192.168.3.%’ identified by ‘repl’;  
  2. Query OK, 0 rows affected (0.00 sec)  
  3. root@localhost:(none) 04:13:03 >grant all on . to repl@‘192.168.3.%’; 
  4. Query OK, 0 rows affected (0.00 sec) 

–在从库134上进行复制步骤的完成: 

  1. root@localhost:(none) 04:16:09 >change master to master_host=‘192.168.3.133’,master_port=3307,master_user=‘repl’,master_password=‘repl’,master_auto_position=1
  2. Query OK, 0 rows affected, 2 warnings (0.00 sec)  
  3. root@localhost:(none) 04:17:32 >start slave;  
  4. Query OK, 0 rows affected (0.00 sec)  
  5. root@localhost:(none) 04:17:35 >show slave status\G  
  6. *************************** 1. row ***************************  
  7. Slave_IO_State: Waiting for master to send event  
  8. Master_Host: 192.168.3.133  
  9. Master_User: repl  
  10. Master_Port: 3307  
  11. Connect_Retry: 60  
  12. Master_Log_File: mysql-bin.000002  
  13. Read_Master_Log_Pos: 1193  
  14. Relay_Log_File: mgr3-relay-bin.000002  
  15. Relay_Log_Pos: 904  
  16. Relay_Master_Log_File: mysql-bin.000002  
  17. Slave_IO_Running: Yes  
  18. Slave_SQL_Running: Yes  
  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: 1193  
  29. Relay_Log_Space: 1103  
  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: 0  
  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: 207  
  47. Master_UUID: 3e75bf2f-6401-11ea-8995-000c29db65a6  
  48. Master_Info_File: mysql.slave_master_info  
  49. SQL_Delay: 0  
  50. SQL_Remaining_Delay: NULL  
  51. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates  
  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: 3e75bf2f-6401-11ea-8995-000c29db65a6:3-4  
  59. Executed_Gtid_Set: 3e75bf2f-6401-11ea-8995-000c29db65a6:1-4  
  60. Auto_Position: 1  
  61. Replicate_Rewrite_DB:  
  62. Channel_Name:  
  63. Master_TLS_Version:  
  64. Master_public_key_path:  
  65. Get_master_public_key: 0  
  66. Network_Namespace:  
  67. 1 row in set (0.00 sec) 

至此远程从库通过clone插件的方式搭建成功了,非常简单也非常快速,不需要mysqldump也不需要xtrabackup,在线搭建成功,非常快,以后用8.0可以考虑这种便捷的方式了。

最后将clone技术的 限制条件 列出:

  1.  版本大于等于8.0.17且不支持跨版本
  2.  两台机器具有相同的操作系统OS
  3.  两台MySQL实例具体相同的 innodb_page_size 和 innodb_data_file_path(ibdata文件名)
  4.  同一时刻仅仅允许有一个克隆任务存在
  5.  recipient 需要设置变量clone_valid_donor_list
  6.  max_allowed_packet 大于2M
  7.  doner的undo表空间文件名称不能重复
  8.  不会克隆my.cnf文件
  9.  不会克隆binlog
  10.  仅仅支持innodb引擎 

 

责任编辑:庞桂玉 来源: 数据库开发
相关推荐

2011-08-30 13:08:55

Mysql ProxyLua分离

2017-04-20 21:00:06

MySQLbinlog主从复制

2021-06-08 07:48:27

MySQL主从配置

2024-03-01 18:33:59

MySQL节点数据

2011-08-30 18:01:20

2011-02-22 10:46:02

Samba配置

2022-02-08 16:17:41

MySQL主从复制数据库

2023-02-27 07:33:14

MySQL数据库服务器

2015-07-08 09:57:59

Git服务器分步详解

2021-06-09 19:23:52

MySQLROLE管理

2009-12-08 17:56:16

WCF配置

2011-04-18 15:56:10

软件测试

2023-03-19 22:38:12

逻辑复制PostgreSQL

2011-09-06 15:38:20

QT安装

2011-01-21 17:51:52

2009-04-13 12:37:18

2023-03-19 11:53:27

2009-08-05 14:16:38

无线局域网设置无线路由无线网卡

2022-12-20 08:46:41

MySQL主从复制

2017-10-11 15:40:20

MySQL主从复制拓扑结构
点赞
收藏

51CTO技术栈公众号