1、安装MySQL8.0.20二进制安装及插件安装
下载 wget -P /data/soft https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz 解压并创建软链接 tar Jxf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz -C /home/mysql/ cd /usr/local/ ln -sv /home/mysql/mysql-8.0.20-linux-glibc2.12-x86_64 mysql8 创建MySQL数据目录
mkdir /home/mysql/mysql3307/ mkdir /home/mysql/mysql3307/undolog mkdir /home/mysql/mysql3307/binlog mkdir /home/mysql/mysql3307/data 创建MySQL配置文件 cd /home/mysql/mysql3307/ 配置文件是使用叶金荣老师的在线生成配置文件 # cat my3307.cnf # ## my.cnf for MySQL 5.7/8.0 ## autoor: yejr(yejinrong@zhishutang.com, http://imysql.com, QQ: 4700963) ## ## 叶金荣(yejr) ## 靠谱优质的在线培训品牌知数堂培训(http://zhishuedu.com)联合创始人 ## 新浪微博: @叶金荣, 微信公众:老叶茶馆(imysql_wx) ## QQ群: 650149401 ## 注意:个别建议可能需要根据实际情况作调整,请自行判断或联系我,本人不对这些建议结果负相应责任 ## 本配置文件主要适用于MySQL 5.7/8.0版本 # [client] port = 3307 socket = /home/mysql/mysql3307/mysql.sock [mysql] prompt="u@mysqldb R:m:s [d]> " no-auto-rehash [mysqld] user = mysql port = 3307 basedir = /usr/local/mysql8 datadir = /home/mysql/mysql3307/data socket = /home/mysql/mysql3307/mysql.sock pid-file = mysqldb.pid character-set-server = utf8mb4 skip_name_resolve = 1 #要加入这个参数否则会报错MySQL8 ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' default_authentication_plugin = mysql_native_password #若你的MySQL数据库主要运行在境外,请务必根据实际情况调整本参数 default_time_zone = "+8:00" open_files_limit = 65535 back_log = 1024 max_connections = 512 max_connect_errors = 1000000 table_open_cache = 1024 table_definition_cache = 1024 table_open_cache_instances = 64 thread_stack = 512K external-locking = FALSE max_allowed_packet = 32M sort_buffer_size = 4M join_buffer_size = 4M thread_cache_size = 768 interactive_timeout = 600 wait_timeout = 600 tmp_table_size = 32M max_heap_table_size = 32M slow_query_log = 1 log_timestamps = SYSTEM slow_query_log_file = /home/mysql/mysql3307/slow.log log-error = /home/mysql/mysql3307/error.log long_query_time = 0.1 log_queries_not_using_indexes =1 log_throttle_queries_not_using_indexes = 60 min_examined_row_limit = 100 log_slow_admin_statements = 1 log_slow_slave_statements = 1 server-id = 3307 log-bin = /home/mysql/mysql3307/binlog/mysql-bin sync_binlog = 1 binlog_cache_size = 4M max_binlog_cache_size = 2G max_binlog_size = 1G #注意:MySQL 8.0开始,binlog_expire_logs_seconds选项也存在的话,会忽略expire_logs_days选项 expire_logs_days = 7 master_info_repository = TABLE relay_log_info_repository = TABLE gtid_mode = on enforce_gtid_consistency = 1 log_slave_updates slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN' binlog_format = row binlog_checksum = 1 relay_log_recovery = 1 relay-log-purge = 1 key_buffer_size = 32M read_buffer_size = 8M read_rnd_buffer_size = 4M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 lock_wait_timeout = 3600 explicit_defaults_for_timestamp = 1 innodb_thread_concurrency = 0 #innodb_sync_spin_loops = 100 #innodb_spin_wait_delay = 30 transaction_isolation = REPEATABLE-READ #innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 2048M innodb_buffer_pool_instances = 4 innodb_buffer_pool_load_at_startup = 1 innodb_buffer_pool_dump_at_shutdown = 1 innodb_data_file_path = ibdata1:1G:autoextend innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 32M innodb_log_file_size = 2G innodb_log_files_in_group = 3 innodb_max_undo_log_size = 4G innodb_undo_directory = /home/mysql/mysql3307/undolog innodb_undo_tablespaces = 95 # 根据您的服务器IOPS能力适当调整 # 一般配普通SSD盘的话,可以调整到 10000 - 20000 # 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000 innodb_io_capacity = 4000 innodb_io_capacity_max = 8000 innodb_flush_sync = 0 innodb_flush_neighbors = 0 innodb_write_io_threads = 8 innodb_read_io_threads = 8 innodb_purge_threads = 4 innodb_page_cleaners = 4 innodb_open_files = 65535 innodb_max_dirty_pages_pct = 50 innodb_flush_method = O_DIRECT innodb_lru_scan_depth = 4000 innodb_checksum_algorithm = crc32 innodb_lock_wait_timeout = 10 innodb_rollback_on_timeout = 1 innodb_print_all_deadlocks = 1 innodb_file_per_table = 1 innodb_online_alter_log_max_size = 4G innodb_stats_on_metadata = 0 innodb_undo_log_truncate = 1 #注意:MySQL 8.0.16开始删除该选项 #internal_tmp_disk_storage_engine = InnoDB # some var for MySQL 8 log_error_verbosity = 3 innodb_print_ddl_logs = 1 binlog_expire_logs_seconds = 604800 #innodb_dedicated_server = 0 innodb_status_file = 1 #注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log-error文件增长较快 innodb_status_output = 0 innodb_status_output_locks = 1 innodb_sort_buffer_size = 67108864 innodb_autoinc_lock_mode = 1 #performance_schema performance_schema = 1 #performance_schema_instrument = '%memory%=on' performance_schema_instrument = '%lock%=on' #innodb monitor innodb_monitor_enable="module_innodb" innodb_monitor_enable="module_server" innodb_monitor_enable="module_dml" innodb_monitor_enable="module_ddl" innodb_monitor_enable="module_trx" innodb_monitor_enable="module_os" innodb_monitor_enable="module_purge" innodb_monitor_enable="module_log" innodb_monitor_enable="module_lock" innodb_monitor_enable="module_buffer" innodb_monitor_enable="module_index" innodb_monitor_enable="module_ibuf_system" innodb_monitor_enable="module_buffer_page" innodb_monitor_enable="module_adaptive_hash" # 初始化时以下俩个参数要注释否则无法初始化 #plugin-load-add=mysql_clone.so #clone=FORCE_PLUS_PERMANENT [mysqldump] quick max_allowed_packet = 32M
# 创建MySQL用户
groupadd mysql
useradd -g mysql -s /sbin/nologin -d /usr/local/mysql -MN mysql
# 修改MySQL目录所有者
chown mysql. /home/mysql/ -R
# MySQL初始化 /usr/local/mysql8/bin/mysqld --defaults-file=/home/mysql/mysql3307/my3307.cnf --initialize # 查看初始化登录密码 grep -w 'root@localhost' error.log |awk '{print $NF}' # 取消配置文件mysql3307.cnf的注释 plugin-load-add=mysql_clone.so clone=FORCE_PLUS_PERMANENT 说明:如果克隆插件对你们很重要,可以设置clone=FORCE_PLUS_PERMANENT或clone=FORCE。作用是:如果插件未成功初始化,就会强制mysqld启动失败。
启动MySQL /usr/local/mysql8/bin/mysqld --defaults-file=/home/mysql/mysql3307/my3307.cnf & # ss -tunpl|grep 3307 tcp 0 128 :::3307 :::* users:(("mysqld",72861,37)) 登录修改MySQL密码 mysql -uroot -p -S /home/mysql/mysql3307/mysql.sock 输入VNXqVbFu=1sk alter user user() identified by '密码'; mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.20 | +-----------+ 1 row in set (0.00 sec) #因为配置文件已经写了插件所以启动时自动加载,就无需安装了 mysql> INSTALL PLUGIN clone SONAME 'mysql_clone.so'; ERROR 1125 (HY000): Function 'clone' already exists # 查看clone插件是否启动 mysql> SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME = 'clone'; +-------------+---------------+ | PLUGIN_NAME | PLUGIN_STATUS | +-------------+---------------+ | clone | ACTIVE | +-------------+---------------+ 1 row in set (0.00 sec)
2、克隆本地数据
克隆需要的权限 需要有备份锁的权限。备份锁是 MySQL 8.0 的新特性之一,比5.7版本的flush table with read lock要轻量。 mysql> create user clone_user@'127.0.0.1' identified by 'mysql123'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT BACKUP_ADMIN ON *.* TO clone_user@'127.0.0.1'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) BACKUP_ADMIN是MySQL8.0 才有的备份锁的权限 #执行本地克隆 mysql -uclone_user -pmysql123 -P3307 -h'127.0.0.1' mysql> CLONE LOCAL DATA DIRECTORY = '/home/mysql/mydata_clone';
注意:例子中需要 MySQL 的运行用户拥有/home/mysql/mydata_clone目录的rwx权限,要求 clone_dir 目录不存在
否则会报错
mysql> CLONE LOCAL DATA DIRECTORY = '/home/mysql/mydata_clone'; ERROR 1006 (HY000): Can't create database '/home/mysql/mydata_clone/' (errno: 13 - Permission denied) mysql> CLONE LOCAL DATA DIRECTORY = '/home/mysql/mydata_clone'; ERROR 1007 (HY000): Can't create database '/home/mysql/mydata_clone'; database exists mysql> CLONE LOCAL DATA DIRECTORY = '/home/mysql/mydata_clone';
本地克隆的步骤如下
-
DROP DATA
-
FILE COPY
-
PAGE COPY
-
REDO COPY
-
FILE SYNC
观察方法如下
mysql> SELECT STAGE, STATE, END_TIME FROM performance_schema.clone_progress; +-----------+-------------+----------------------------+ | STAGE | STATE | END_TIME | +-----------+-------------+----------------------------+ | DROP DATA | Completed | 2020-10-10 09:34:22.696656 | | FILE COPY | Completed | 2020-10-10 09:34:31.265343 | | PAGE COPY | Completed | 2020-10-10 09:34:31.586777 | | REDO COPY | Completed | 2020-10-10 09:34:31.788930 | | FILE SYNC | Completed | 2020-10-10 09:34:42.458395 | | RESTART | Not Started | NULL | | RECOVERY | Not Started | NULL | +-----------+-------------+----------------------------+ 7 rows in set (0.01 sec)
查看克隆文件
mysql> system ls -lh /home/mysql/mydata_clone; 总用量 5.1G drwxr-x--- 2 mysql mysql 4.0K 10月 10 09:34 #clone -rw-r----- 1 mysql mysql 5.5K 10月 10 09:34 ib_buffer_pool -rw-r----- 1 mysql mysql 1.0G 10月 10 09:34 ibdata1 -rw-r----- 1 mysql mysql 2.0G 10月 10 09:34 ib_logfile0 -rw-r----- 1 mysql mysql 2.0G 10月 10 09:34 ib_logfile1 drwxr-x--- 2 mysql mysql 4.0K 10月 10 09:34 mysql -rw-r----- 1 mysql mysql 24M 10月 10 09:34 mysql.ibd drwxr-x--- 2 mysql mysql 4.0K 10月 10 09:34 sys -rw-r----- 1 mysql mysql 10M 10月 10 09:34 undo_001 -rw-r----- 1 mysql mysql 10M 10月 10 09:34 undo_002
在上述操作语句中,"/path/to/clone_dir"是将数据克隆到本地目录的绝对路径。
该路径中,"clone_dir"目录不能事先存在(事先存在会报错),但路径前缀"/path/to/"必须事先存在。
另外,MySQL Server必须具有在文件系统中创建目录所需的写权限。
注意:本地克隆操作不支持克隆位于数据目录外部的用户创建的表或表空间。
尝试克隆此类表或表空间会导致报错:
ERROR 1086 (HY000): File '/path/to/tablespace_name.ibd' already exists.。
克隆操作时如果指定了一个与数据源表空间相同路径时会导致冲突,因此被禁止
重要提示:
当执行克隆操作时,所有用户创建的InnoDB表和表空间,InnoDB系统表空间,redo log和undo log表空间都将被克隆到指定目录下
注意:克隆操作只会克隆数据文件,除了系统变量datadir之外,
如果系统变量innodb_data_home_dir、innodb_data_file_path、innodb_log_group_home_dir、innodb_undo_directory单独指定了不同于datadir指定的路径,则也会被执行克隆,
系统变量socket、pid-file、tmpdir、log-error、slow_query_log_file、log-bin、relay-log指定路径下的文件不会被克隆
如果需要,可以在克隆操作完成后使用克隆的数据目录启动一个新的MySQL Server,例如:
#其中clone_dir是克隆操作完成之后的数据副本目录
shell> mysqld_safe --datadir=clone_dir
#示例
先将第二个MySQL Server的配置文件设置好,不能与同一个主机中其他MySQL Server的配置文件存在路径冲突,也不能存在端口冲突,然后,使用mysqld_safe启动第二MySQL Server,使用--datadir指定克隆的数据副本目录。
由于原先的实例开启了innodb_undo_directory
存放undolog的日志,然而指定目录/data/mysql/mydata_clone
克隆完成后,undolog日志存放了/data/mysql/mydata_clone
这个下面,所以clonemy8.cnf配置文件要重新指定innodb_undo_directory=/data/mysql/mydata_clone,才能基于clone副本数据正常启动新的实例
# pwd /home/mysql/mydata_clone 查看配置文件 # egrep '3308|mydata_clone' my3308.cnf port = 3308 socket = /home/mysql/mydata_clone/mysql.sock port = 3308 datadir = /home/mysql/mydata_clone socket = /home/mysql/mydata_clone/mysql.sock slow_query_log_file = /home/mysql/mydata_clone/slow.log log-error = /home/mysql/mydata_clone/error.log server-id = 3308 log-bin = /home/mysql/mydata_clone/binlog/mysql-bin innodb_undo_directory = /home/mysql/mydata_clone 创建目录并修改权限 mkdir binlog chown mysql. /home/mysql/mydata_clone/ -R 启动mysql /usr/local/mysql8/bin/mysqld --defaults-file=/home/mysql/mydata_clone/my3308.cnf & mysql> select * from performance_schema.clone_status; +------+------+-----------+-------------------------+-------------------------+----------------+---------------------------+----------+---------------+-------------+-----------------+---------------+ | ID | PID | STATE | BEGIN_TIME | END_TIME | SOURCE | DESTINATION | ERROR_NO | ERROR_MESSAGE | BINLOG_FILE | BINLOG_POSITION | GTID_EXECUTED | +------+------+-----------+-------------------------+-------------------------+----------------+---------------------------+----------+---------------+-------------+-----------------+---------------+ | 1 | 12 | Completed | 2020-10-10 09:34:22.694 | 2020-10-10 09:34:42.459 | LOCAL INSTANCE | /home/mysql/mydata_clone/ | 0 | | | 0 | | +------+------+-----------+-------------------------+-------------------------+----------------+---------------------------+----------+---------------+-------------+-----------------+---------------+ 1 row in set (0.00 sec) mysql> select * from performance_schema.clone_progress; +------+-----------+-------------+----------------------------+----------------------------+---------+------------+------------+---------+------------+---------------+ | ID | STAGE | STATE | BEGIN_TIME | END_TIME | THREADS | ESTIMATE | DATA | NETWORK | DATA_SPEED | NETWORK_SPEED | +------+-----------+-------------+----------------------------+----------------------------+---------+------------+------------+---------+------------+---------------+ | 1 | DROP DATA | Completed | 2020-10-10 09:34:22.694327 | 2020-10-10 09:34:22.696656 | 1 | 0 | 0 | 0 | 0 | 0 | | 1 | FILE COPY | Completed | 2020-10-10 09:34:22.696814 | 2020-10-10 09:34:31.265343 | 2 | 1119999445 | 1119999445 | 0 | 0 | 0 | | 1 | PAGE COPY | Completed | 2020-10-10 09:34:31.265535 | 2020-10-10 09:34:31.586777 | 2 | 0 | 0 | 0 | 0 | 0 | | 1 | REDO COPY | Completed | 2020-10-10 09:34:31.586968 | 2020-10-10 09:34:31.788930 | 2 | 7680 | 7680 | 0 | 0 | 0 | | 1 | FILE SYNC | Completed | 2020-10-10 09:34:31.789090 | 2020-10-10 09:34:42.458395 | 2 | 0 | 0 | 0 | 0 | 0 | | 1 | RESTART | Not Started | NULL | NULL | 0 | 0 | 0 | 0 | 0 | 0 | | 1 | RECOVERY | Not Started | NULL | NULL | 0 | 0 | 0 | 0 | 0 | 0 | +------+-----------+-------------+----------------------------+----------------------------+---------+------------+------------+---------+------------+---------------+ 7 rows in set (0.00 sec)
总结:个人感觉不太喜欢本地克隆的数据,因为只克隆了data目录下的相关数据,不会克隆原实例的全部目录结构,不好管理和维护。
3、克隆远程数据
环境:
10.72.23.45 master实例
10.72.23.44 新的实例
安装过程可以参考“三、安装克隆插件”,此处不再赘述。
确保俩台机器都启动了clone插件
mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.20 | +-----------+ 1 row in set (0.00 sec) mysql> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec) mysql> SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME = 'clone'; +-------------+---------------+ | PLUGIN_NAME | PLUGIN_STATUS | +-------------+---------------+ | clone | ACTIVE | +-------------+---------------+ 1 row in set (0.01 sec)
账号授权
开始操作演示:
10.72.23.45 donor机器发送端操作:
用管理用户登录到发送方 donor MySQL Server中,创建一个克隆用户并赋予BACKUP_ADMIN权限: root@localhost [(none)]> create user donor_clone_user@'10.72.23.%' identified by 'mysql123'; Query OK, 0 rows affected (0.02 sec) root@localhost [(none)]> grant backup_admin on *.* to donor_clone_user@'10.72.23.%'; Query OK, 0 rows affected (0.02 sec) 在donor MySQL Server中安装克隆插件: root@localhost [(none)]>INSTALL PLUGIN clone SONAME 'mysql_clone.so'; ERROR 1125 (HY000): Function 'clone' already exists
10.72.23.44 接受方机器操作:
使用管理用户登录到接收方MySQL Server,创建一个克隆用户并赋予CLONE_ADMIN权限: root@localhost [(none)]>create user recipient_clone_user@'10.72.23.%' identified by 'mysql123'; root@localhost [(none)]>grant clone_admin on *.* to recipient_clone_user@'10.72.23.%'; 在接收方MySQL Server中安装克隆插件: root@localhost [(none)]>INSTALL PLUGIN clone SONAME 'mysql_clone.so'; ERROR 1125 (HY000): Function 'clone' already exists 将donor MySQL Server的主机地址添加到接收方MySQL Server的clone_valid_donor_list变量中: root@localhost [(none)]> SET GLOBAL clone_valid_donor_list = '10.72.23.45:3307'; 以在donor MySQL Server中创建的克隆用户,登录到接收方MySQL Server中,执行如下克隆语句:(指定克隆目录操作) root@localhost [(none)]>CLONE INSTANCE FROM 'donor_clone_user'@'10.72.23.45':3307 IDENTIFIED BY 'mysql123' DATA DIRECTORY = '/home/mysql/3307/clone_data'; Query OK, 0 rows affected (24.72 sec) 查看克隆状态: use performance_schema; mysql> select * from clone_statusG; *************************** 1. row *************************** ID: 1 PID: 9 STATE: Completed BEGIN_TIME: 2020-10-10 11:00:01.116 END_TIME: 2020-10-10 11:01:04.177 SOURCE: 10.72.23.45:3307 DESTINATION: /home/mysql/3307/clone_data/ ERROR_NO: 0 ERROR_MESSAGE: BINLOG_FILE: BINLOG_POSITION: 0 GTID_EXECUTED: 1 row in set (0.00 sec) ERROR: No query specified mysql> select * from clone_progress; +------+-----------+-------------+----------------------------+----------------------------+---------+------------+------------+------------+------------+---------------+ | ID | STAGE | STATE | BEGIN_TIME | END_TIME | THREADS | ESTIMATE | DATA | NETWORK | DATA_SPEED | NETWORK_SPEED | +------+-----------+-------------+----------------------------+----------------------------+---------+------------+------------+------------+------------+---------------+ | 1 | DROP DATA | Completed | 2020-10-10 11:00:01.236066 | 2020-10-10 11:00:01.337572 | 1 | 0 | 0 | 0 | 0 | 0 | | 1 | FILE COPY | Completed | 2020-10-10 11:00:01.337831 | 2020-10-10 11:00:15.023297 | 4 | 1119999445 | 1119999445 | 1120067369 | 0 | 0 | | 1 | PAGE COPY | Completed | 2020-10-10 11:00:15.023590 | 2020-10-10 11:00:15.434913 | 4 | 0 | 0 | 393 | 0 | 0 | | 1 | REDO COPY | Completed | 2020-10-10 11:00:15.435333 | 2020-10-10 11:00:15.735725 | 4 | 6144 | 6144 | 6835 | 0 | 0 | | 1 | FILE SYNC | Completed | 2020-10-10 11:00:15.735979 | 2020-10-10 11:01:04.176982 | 4 | 0 | 0 | 0 | 0 | 0 | | 1 | RESTART | Not Started | NULL | NULL | 0 | 0 | 0 | 0 | 0 | 0 | | 1 | RECOVERY | Not Started | NULL | NULL | 0 | 0 | 0 | 0 | 0 | 0 | +------+-----------+-------------+----------------------------+----------------------------+---------+------------+------------+------------+------------+---------------+ 7 rows in set (0.00 sec)
克隆到指定目录说明:
默认情况下,远程克隆操作会删除接收方数据目录中的数据,并用克隆的数据替换它。
通过克隆到指定目录,可以避免接收方数据目录中的现有数据被删除,也不会执行重启MySQL Server的操作。
将数据克隆到指定目录的过程与不指定目录的克隆远程数据过程相同,但有一点区别,前者的克隆语句必须包含DATA DIRECTORY [=] 'clone_dir'子句。
例如:CLONE
INSTANCE FROM 'donor_clone_user'@'192.168.1.105':3306 IDENTIFIED BY
'clone_test66' DATA DIRECTORY = '/data/mysql/3307/clone_data';
其中,DATA DIRECTORY子句需要指定一个绝对路径,且该目录不能事先存在,MySQL Server必须具有创建目录所需的写访问权限
克隆到指定目录时,在克隆数据完成之后,不会自动重新启动接收方MySQL Server。
如果你想使用指定目录启动MySQL Server,你必须手动修改一些文件的目录调整之后再执行启动,或者直接使用新的my.cnf配置文件,
在启动时将--datadir选项指定到克隆数据所在的目录,例如:mysqld_safe --datadir=/data/mysql/3307/clone_data
由于配置文件制定了undo文件存放目录,所以也要修改下:
[root@mysql-redis182 ~]# grep -w 'clone_data' /data/mysql/3307/my8.cnf datadir = /home/mysql/3307/clone_data innodb_undo_directory = /home/mysql/3307/clone_data
记得修改server-id与master不一样
# cat my3307.cnf |grep server-id
server-id = 443307
sed -i 's#/home/mysql/mysql3307#/home/mysql/3307/clone_data#g' my3307.cnf
mkdir binlog
chown mysql. binlog/ -R
然后启动mysql服务:
/usr/local/mysql8/bin/mysqld --defaults-file=/home/mysql/3307/clone_data/my3307.cnf & 查看45的数据已经克隆过来 # /usr/local/mysql8/bin/mysql -uroot -p'mysql123' -S /home/mysql/3307/clone_data/mysql.sock mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.01 sec)
启动克隆版的MySQL,查看接受放的二进制日志位置和gtid信息:
克隆操作结束之后,可在执行克隆操作的MySQL Server上执行如下查询语句,以检查发送给接收方的二进制日志位置:
mysql> SELECT BINLOG_FILE, BINLOG_POSITION FROM performance_schema.clone_status; +------------------+-----------------+ | BINLOG_FILE | BINLOG_POSITION | +------------------+-----------------+ | mysql-bin.000002 | 2234 | +------------------+-----------------+ 1 row in set (0.00 sec)
在克隆操作结束之后,可在执行克隆操作的MySQL Server上执行如下查询,以检查传输给接收方的GTID SET:
mysql> SELECT @@GLOBAL.GTID_EXECUTED; +------------------------------------------+ | @@GLOBAL.GTID_EXECUTED | +------------------------------------------+ | 8fb02055-0a08-11eb-a98b-ac162dbda4a4:1-9 | +------------------------------------------+ 1 row in set (0.00 sec)
提示:当在192.168.1.182机器接受端未指定克隆目录操作,会清空当前192.168.1.182mysql实例数据目录下的数据,然后把克隆的数据复制过来,并且会自动重启mysql服务,由于undolog日志路径变化导致重启失败
登录到接收方192.168.1.182MySQL Server中,执行如下克隆语句:(未指定克隆目录操作)
root@localhost [(none)]> CLONE INSTANCE FROM 'donor_clone_user'@'192.168.1.105':3306 IDENTIFIED BY 'clone_test66' ;
ERROR 3707 (HY000): Restart server failed (mysqld is not managed by supervisor process).
4、利用远程克隆的数据进行复制拓扑扩展应用介绍:
当在发送方上设置master_info_repository=TABLE和relay_log_info_repository=TABLE时(这是MySQL 8.0的默认设置),如果发送方是一个从库角色,那么从库的状态日志会被保存在表中,这些表在克隆操作期间会从发送方当做数据复制到接收方。
从库的状态日志中保存了与复制相关的配置设置,这些设置可用于在克隆操作成功之后正确地自动恢复复制
(如果配置文件中没有设置skip_slave_start参数,则在克隆操作完成之后会自动启动复制线程,另外,如果发送方不是从库而是主库,那么从库的状态日志表中并不存在复制配置信息,因此不适用,恢复复制过程中需要手动执行CHANGE
MASTER语句进行配置)。
在MySQL 8.0.17和8.0.18中,只有mysql.slave_master_info表才会被复制到接收方(主库信息日志)
从MySQL 8.0.19开始,mysql.slave_relay_log_info(中继日志信息日志)和mysql.slave_worker_info(从库worker线程日志)也会被复制到接收方
PS:有关上述三张表中每个表包含的内容列表及其含义,请参阅:https://dev.mysql.com/doc/refman/8.0/en/slave-logs-status.html。
注意,如果在发送方上设置了master_info_repository=FILE和relay_log_info_repository=FILE(这不是MySQL
8.0的默认设置,并且是不推荐的),则不会克隆从库的状态日志,只有在发送方设置master_info_repository=TABLE和relay_log_info_repository=TABLE时才会克隆从库的状态日志信息。
要在复制拓扑中使用克隆,请按照以下步骤执行:
对于用于组复制的新组成员,请先按照链接:https://dev.mysql.com/doc/refman/8.0/en/group-replication-adding-instances.html 中的说明为组复制配置好MySQL Server环境,且按照链接:https://dev.mysql.com/doc/refman/8.0/en/group-replication-cloning.html的说明设置好克隆功能所需的前提条件。
然后,在joiner成员上执行START GROUP_REPLICATION语句时,克隆操作由组复制自动管理,因此不需要手动执行joiner成员加入组的操作,也不需要在joiner成员上执行任何进一步的设置步骤。
对于主从复制拓扑中的从库,首先在从库(接收方)中手动执行远程克隆操作语句,将数据从donor MySQL Server克隆到接收方。
在复制拓扑中,发送方必须是主库或从库。如果发送方是主库的,则后续需要手动执行CHANGE MASTER语句来配置复制,如果发送方是从库的,则不需要手动执行复制配置操作,复制能够通过克隆数据进行自动恢复复制
(配置文件中指定了skip_slave_start参数的情况除外)。有关克隆语句的详细信息,请参见"3、克隆远程数据"
克隆操作成功完成后,如果要在接收方MySQL Server上使用与发送方相同的复制通道,请验证其中哪些设置或配置可以在主从复制拓扑中自动恢复,哪些需要手动设置才能恢复。
-
对于基于GTID的复制,如果在接收方配置了gtid_mode=ON,并且在发送方设置了gtid_mode=ON、ON_PERMISSIVE或 OFF_PERMISSIVE值,则接收方的gtid_executed系统变量中的GTID SET会作为接收方的GTID SET。
如果接收方的数据是从拓扑中已经存在的一个从库中克隆出来的,则在启用了GTID自动定位(由CHANGE MASTER TO语句上的MASTER_AUTO_POSITION选项指定)的接收方上的复制通道可以在自动重启MySQL Server之后自动恢复,不需要执行任何手动设置(如果需要修改复制通道,则自行调整,这里不做赘述) -
对于基于二进制日志文件位置的复制,如果接收方使用的是MySQL 8.0.17或8.0.18,则来自发送方的二进制日志位置不应用于接收方,仅记录在performance_schema.clone_status表中。
因此,在自动重启实例之后,必须在接收方中手动为复制通道设置使用基于二进制日志文件位置的复制,以便恢复复制。
在这种情况下,需要确保在重启实例时启用skip_slave_start来避免复制自动启动,因为此时并未设置二进制日志位置,自动启动复制将尝试从头开始复制。 - 对于基于二进制日志文件位置的复制,如果接收方使用的是MySQL 8.0.19或以上版本,则来自发送方的二进制日志位置将应用于接收方。
接收方上使用基于二进制日志文件位置的复制,将会在自动重启复制通道之前,使用中继日志信息自动尝试执行中继日志的恢复过程。
注意:对于单线程的从库(slave_parallel_workers设置为0),在没有任何其他意外发生的情况下,复制通道无需进一步设置即可成功恢复复制。
对于多线程的从库(slave_parallel_workers大于0),中继日志恢复可能会失败,因为它通常不能自动完成。在这种情况下,会发出一条错误消息,您必须手动设置通道
环境: 10.72.23.45 master 10.72.23.44 slave #在master 44创建复制账号 create user rep@'10.72.23.44' identified by 'mysql123'; grant replication slave on *.* to rep@'10.72.23.44'; # 在slave 45上执行复制语句 CHANGE MASTER TO MASTER_HOST='10.72.23.45',MASTER_PORT=3307,MASTER_USER='rep',MASTER_PASSWORD='mysql123',MASTER_AUTO_POSITION = 1; mysql> start slave; Query OK, 0 rows affected (0.02 sec) mysql> show slave statusG; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.72.23.45 Master_User: rep Master_Port: 3307 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 3487 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 1671 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: 3487 Relay_Log_Space: 1884 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: 3307 Master_UUID: 8fb02055-0a08-11eb-a98b-ac162dbda4a4 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: 8fb02055-0a08-11eb-a98b-ac162dbda4a4:10-12 Executed_Gtid_Set: 8fb02055-0a08-11eb-a98b-ac162dbda4a4:1-12 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: 1 row in set (0.00 sec) ERROR: No query specified 在45上创建数据库test1 mysql> create database test1; Query OK, 1 row affected (0.00 sec) 在44上查看 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | | test1 | +--------------------+ 6 rows in set (0.01 sec)
5、远程克隆数据注意事项:
执行CLONE INSTANCE在非donor实例上执行。
不指定DATA DIRECTORY,将先清空本地数据,再做克隆拷贝,并自动重启MySQL实例(建议mysqld_safe启动)。
若指定DATA DIRECTORY,本地磁盘空间需要更多的空间(克隆数据+本地历史数据),不会自动重启MySQL实例
donor 和 recipient的MySQL版本要一致,并且至少8.0.17或者更高的版本。
donor 和 recipient的操作系统不能跨平台。
donor 和 recipient需要具有相同的字符集和排序规则。
donor和 recipient需要设置相同的 innodb_page_size and innodb_data_file_path
如果克隆了加密或者页压缩的数据,donor 和 recipient需要保持一样的文件系统块大小。
克隆命令将以1MB的包大小传输,所以donor 和 recipient的 max_allowed_packet至少要设置2MB。
6、其他注意事项
克隆操作期间不允许DDL(包括TRUNCATE TABLE)。
一次只能克隆一个MySQL实例。不支持在单个克隆操作中克隆多个MySQL实例。
远程克隆操作(在CLONE INSTANCE语句中指定Donor的MySQL服务器实例的端口号时)不支持mysqlx_port指定的X协议端口。
clone插件不支持MySQL配置参数的克隆。
clone插件不支持二进制日志的克隆。
克隆插件仅克隆存储在InnoDB中的数据。其他存储引擎数据未克隆。存储在任何数据库(包括sys模式)中的MyISAM和CSV表都被克隆为空表
鸣谢:本博文是在学些了马蜂窝高级DBA张充和爱可生高级DBA罗小波分享在知书堂微信公众号的关于MySQL8.0的clone plugin插件的文章。结合自己的理解总结于此。特别感谢二位的分享。同时我也分享出来,希望能帮助到更多的爱好MySQL伙伴,一起交流,一起成长。
转自
最详细的clone plugin 介绍-10931853-51CTO博客 https://blog.51cto.com/wujianwei/2493941?source=dra
参考
(2条消息) 技术分享 | 实战 MySQL 8.0.17 Clone Plugin_ActionTech的博客-CSDN博客 https://blog.csdn.net/actiontech/article/details/98054877
MySQL clone plugin(克隆插件) - 简书 https://www.jianshu.com/p/ed7e0796a3d8