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

 2022-10-27    509  

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

Mater: 192.168.3.133 port:3307 doner 捐赠者

Slave: 192.168.3.134 port:3307 recipient 接受者

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

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

[root@mgr2bin]#cd/zcloud/db/abcMgr/abcMgr02/mysql/bin
[root@mgr2bin]#./mysqld--initialize--user=mysql
[root@mgr2bin]#./mysqld_safe--user=mysql&
[1]19556
[root@mgr2bin]#2020-03-12T01:32:26.503048Zmysqld_safeLoggingto‘/rock/mysqldata/error.log’.
2020-03-12T01:32:26.536292Zmysqld_safeStartingmysqlddaemonwithdatabasesfrom/rock/mysqldata
[root@mgr2bin]#
[root@mgr2bin]#mysql-uroot-p-P3307
Enterpassword:
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis8
Serverversion:8.0.19
Copyright©2000,2019,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type‘help;’or‘\h’forhelp.Type‘\c’toclearthecurrentinputstatement.
root@localhost:(none)09:33:06>alteruseruser()identifiedby‘root1234’;
QueryOK,0rowsaffected(0.00sec)
root@localhost:(none)09:33:26>flushprivileges;
QueryOK,0rowsaffected(0.00sec)
root@localhost:(none)09:33:31>exit
Bye

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

–创建用户

root@localhost:(none)10:07:05>createuser‘donor_user’@‘192.168.3.134’identifiedby‘password’;
QueryOK,0rowsaffected(0.00sec)
root@localhost:(none)10:07:07>grantbackup_adminon.to‘donor_user’@‘192.168.3.134’;
QueryOK,0rowsaffected(0.01sec)

–安装clone插件

root@localhost:(none)10:23:16>installpluginclonesoname‘mysql_clone.so’;
QueryOK,0rowsaffected(0.01sec)

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

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

mysql>createuser‘recipient_user’@‘192.168.3.134’identifiedby‘password’;

–安装clone插件

mysql>grantclone_adminon.to‘recipient_user’@‘192.168.3.134’;

–设置参数clone_valid_donor_list

root@localhost:(none)03:28:40>setglobalclone_valid_donor_list=‘192.168.3.133:3307’;
QueryOK,0rowsaffected(0.00sec)

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

[root@mgr3bin]#mysql-urecipient_user-ppassword-P3307-h192.168.3.134
recipient_user@192.168.3.134:(none)03:39:46>cloneinstancefrom‘donor_user’@‘192.168.3.133’:3307identifiedby‘password’;
QueryOK,0rowsaffected(2.34sec)
recipient_user@192.168.3.134:(none)03:39:57>Restartingmysqld…
2020-03-12T07:40:01.285267Zmysqld_safeNumberofprocessesrunningnow:0
2020-03-12T07:40:01.290169Zmysqld_safemysqldrestarted

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

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

–查看clone进度和状态

root@localhost:(none)03:34:49>SELECT*FROMperformance_schema.clone_progress;
±-----±----------±----------±---------------------------±---------------------------±--------±----------±----------±----------±-----------±--------------+
|ID|STAGE|STATE|BEGIN_TIME|END_TIME|THREADS|ESTIMATE|DATA|NETWORK|DATA_SPEED|NETWORK_SPEED|
±-----±----------±----------±---------------------------±---------------------------±--------±----------±----------±----------±-----------±--------------+
|1|DROPDATA|Completed|2020-03-1215:29:15.385694|2020-03-1215:29:15.634609|1|0|0|0|0|0|
|1|FILECOPY|Completed|2020-03-1215:29:15.634765|2020-03-1215:29:17.452961|1|465800520|465800520|465833356|0|0|
|1|PAGECOPY|Completed|2020-03-1215:29:17.453144|2020-03-1215:29:17.554224|1|0|0|99|0|0|
|1|REDOCOPY|Completed|2020-03-1215:29:17.554413|2020-03-1215:29:17.654430|1|2560|2560|3031|0|0|
|1|FILESYNC|Completed|2020-03-1215:29:17.654596|2020-03-1215:29:17.730172|1|0|0|0|0|0|
|1|RESTART|Completed|2020-03-1215:29:17.730172|2020-03-1215:29:22.160372|0|0|0|0|0|0|
|1|RECOVERY|Completed|2020-03-1215:29:22.160372|2020-03-1215:29:22.478889|0|0|0|0|0|0|
±-----±----------±----------±---------------------------±---------------------------±--------±----------±----------±----------±-----------±--------------+
7rowsinset(0.00sec)
root@localhost:(none)03:34:52>SELECT*FROMperformance_schema.clone_status\G
***************************1.row***************************
ID:1
PID:0
STATE:Completed
BEGIN_TIME:2020-03-1215:29:15.385
END_TIME:2020-03-1215:29:22.479
SOURCE:192.168.3.133:3307
DESTINATION:LOCALINSTANCE
ERROR_NO:0
ERROR_MESSAGE:
BINLOG_FILE:mysql-bin.000002
BINLOG_POSITION:421
GTID_EXECUTED:3e75bf2f-6401-11ea-8995-000c29db65a6:1
1rowinset(0.00sec)

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

root@localhost:(none)04:12:23>createuserrepl@‘192.168.3.%’identifiedby‘repl’;
QueryOK,0rowsaffected(0.00sec)
root@localhost:(none)04:13:03>grantallon.torepl@‘192.168.3.%’;
QueryOK,0rowsaffected(0.00sec)

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

root@localhost:(none)04:16:09>changemastertomaster_host=‘192.168.3.133’,master_port=3307,master_user=‘repl’,master_password=‘repl’,master_auto_position=1;
QueryOK,0rowsaffected,2warnings(0.00sec)
root@localhost:(none)04:17:32>startslave;
QueryOK,0rowsaffected(0.00sec)
root@localhost:(none)04:17:35>showslavestatus\G
***************************1.row***************************
Slave_IO_State:Waitingformastertosendevent
Master_Host:192.168.3.133
Master_User:repl
Master_Port:3307
Connect_Retry:60
Master_Log_File:mysql-bin.000002
Read_Master_Log_Pos:1193
Relay_Log_File:mgr3-relay-bin.000002
Relay_Log_Pos:904
Relay_Master_Log_File:mysql-bin.000002
Slave_IO_Running:Yes
Slave_SQL_Running:Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:0
Last_Error:
Skip_Counter:0
Exec_Master_Log_Pos:1193
Relay_Log_Space:1103
Until_Condition:None
Until_Log_File:
Until_Log_Pos:0
Master_SSL_Allowed:No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:0
Master_SSL_Verify_Server_Cert:No
Last_IO_Errno:0
Last_IO_Error:
Last_SQL_Errno:0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:207
Master_UUID:3e75bf2f-6401-11ea-8995-000c29db65a6
Master_Info_File:mysql.slave_master_info
SQL_Delay:0
SQL_Remaining_Delay:NULL
Slave_SQL_Running_State:Slavehasreadallrelaylog;waitingformoreupdates
Master_Retry_Count:86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:3e75bf2f-6401-11ea-8995-000c29db65a6:3-4
Executed_Gtid_Set:3e75bf2f-6401-11ea-8995-000c29db65a6:1-4
Auto_Position:1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key:0
Network_Namespace:
1rowinset(0.00sec)

至此远程从库通过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引擎

  •  标签:  
  • MySQL
  •  

原文链接:https://77isp.com/post/9976.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。