当前位置:主页 > 查看内容

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

发布时间:2021-05-13 00:00| 位朋友查看

简介:本文将通过Clone来实现一个远程从库的搭建:环境如下 Mater: 192.168.3.133 port:3307 doner 捐赠者 Slave: 192.168.3.134 port:3307 recipient 接受者 第一步:两台机器MySQL分别进行初始化安装 进入MySQL软件目录进行初始化安装并修改密码:(MySQL8.0.19……

本文将通过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引擎 

本文转载自网络,原文链接:https://mp.weixin.qq.com/s/htjBObM5J9il5N0TQqp_DA
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:MongoDB复制集集群原理详解及部署 下一篇:没有了

推荐图文


随机推荐