一、实现本地克隆操作过程:
克隆需求:实现快速创建和源数据库服务一模一样的多实例服务程序;
克隆操作步骤01:加载克隆插件信息
# 进行克隆插件加载配置
mysql> INSTALL PLUGIN clone SONAME 'mysql_clone.so';
或者
[mysqld]
plugin-load-add=mysql_clone.so
clone=FORCE_PLUS_PERMANENT
# 查看克隆插件加载情况
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)
克隆操作步骤02:创建克隆专用用户
mysql> create user clone_user@'%' identified by 'password';
mysql> grant backup_admin on *.* to 'clone_user';
-- backup_admin权限是mysql 8.0才有的备份锁的权限;
克隆操作步骤03:进行本地克隆操作
[root@baimeidashu-01 ~]# mkdir -p /data/test
[root@baimeidashu-01 ~]# chown -R mysql.mysql /data/
[root@baimeidashu-01 ~]# mysql -uclone_user -ppassword
mysql> clone local data directory = '/data/test/clonedir';
-- 完成本地数据库目录的克隆操作,如果出现异常需要删除克隆目录,在进行重新克隆操作
克隆操作步骤04:观测本地克隆状态(另开窗口使用管理员用户查看)
mysql> select stage,state,end_time from performance_schema.clone_progress;
+---------------+----------------+-----------------------------------+
| stage | state | end_time |
+---------------+----------------+-----------------------------------+
| DROP DATA | Completed | 2022-11-28 01:11:17.715901 |
| FILE COPY | Completed | 2022-11-28 01:11:17.752819 |
| PAGE COPY | Completed | 2022-11-28 01:11:17.756830 |
| REDO COPY | Completed | 2022-11-28 01:11:17.757802 |
| FILE SYNC | Completed | 2022-11-28 01:11:17.912679 |
| RESTART | Not Started | NULL |
| RECOVERY | Not Started | NULL |
+---------------+----------------+-----------------------------------+
7 rows in set (0.00 sec)
-- 当克隆数据量比较大的时候,可以使用此SQL语句进行克隆状态查看
克隆操作步骤05:实现克隆日志观测
mysql> set global log_error_verbosity=3;
[root@baimeidashu-01 ~]# tail -f db01.err
clone local data directory = '/data/test/clonedir'
克隆操作步骤06:启动运行克隆实例
[root@baimeidashu-01 ~]# mysqld_safe --datadir=/data/test/clonedir --port=3333 --socket=/tmp/mysql3333.sock --user=mysql --mysqlx=off &
[root@baimeidashu-01 ~]# netstat -lntup|grep 3333
tcp6 0 0 :::3333 :::* LISTEN 52674/mysqld
# 核实查看克隆后数据库信息
[root@baimeidashu-01 ~]# mysql -uroot -p123456 -S[root@baimeidashu-01 ~]# mysql -uroot -p123456 -S
mysql> show databases;
二、实现远程克隆操作过程:
在进行实现远程克隆操作步骤之前,可以利用虚拟软件再次克隆出一台新的数据库8.0版本的服务器主机;
克隆操作步骤01:克隆操作环境准备
# 在克隆接收者主机上清理数据库服务环境:
[root@baimeidashu-02 ~]# pkill mysqld
[root@baimeidashu-02 ~]# rm -rf /data/3306/data/*
[root@baimeidashu-02 ~]# rm -rf /data/3306/binlog/*
[root@baimeidashu-02 ~]# rm -rf /data/3306/logs/*
# 在克隆接收者主机上进行实例初始化操作:
[root@baimeidashu-02 ~]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3306/data
[root@baimeidashu-02 ~]# vim /etc/my.cnf
[mysqld]
server_id=16
-- 修改克隆接收者主机上的server_id的配置信息
# 在克隆接收者主机上进行实例的运行操作:
[root@baimeidashu-02 ~]# /etc/init.d/mysqld start
克隆操作步骤02:加载克隆插件信息
# 进行克隆插件加载配置
mysql> INSTALL PLUGIN clone SONAME 'mysql_clone.so';
或者
[mysqld]
plugin-load-add=mysql_clone.so
clone=FORCE_PLUS_PERMANENT
-- 克隆插件信息需要在克隆主机的捐赠者和接受者上都进行安装
# 查看克隆插件加载情况
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)
-- 克隆插件信息需要在克隆主机的捐赠者和接受者上都进行安装后确认
克隆操作步骤03:创建克隆专用用户
# 在克隆捐赠者主机上进行授权(数据库01主机上设置)
mysql> create user test_jz@'%' identified by 'password';
mysql> grant backup_admin on *.* to test_jz@'%' ;
-- backup_admin权限是mysql 8.0才有的备份锁的权限;
# 在克隆接收者主机上进行授权(数据库02主机上设置)
mysql> create user test_js@'%' identified by 'password';
mysql> grant clone_admin on *.* to test_js@'%' ;
-- clone_admin权限是mysql 8.0才有的克隆同步数据的权限;
说明:可以在克隆捐赠者主机上和接收者主机上均创建两个用户信息,防止克隆同步数据后,接收者主机上不再含有接收用户信息;
克隆操作步骤04:进行远程克隆操作
# 在克隆接收者主机上进行设置
mysql> set global clone_valid_donor_list='192.168.30.101:3306';
-- 设置克隆同步数据的信任列表
# 在克隆接收者主机上进行克隆
[root@baimeidashu-02 ~]# mysql -utest_js -ppassword -h192.168.30.102 -P3306
mysql> clone instance from test_jz@'192.168.30.101':3306 identified by 'password';
-- 在接收者主机上实现远程克隆操作
克隆操作步骤05:观测本地克隆状态
mysql> select stage,state,end_time from performance_schema.clone_progress;
+---------------+----------------+-----------------------------------+
| stage | state | end_time |
+---------------+----------------+-----------------------------------+
| DROP DATA | Completed | 2022-11-29 00:15:34.002378 |
| FILE COPY | Completed | 2022-11-29 00:15:35.218397 |
| PAGE COPY | Completed | 2022-11-29 00:15:35.225659 |
| REDO COPY | Completed | 2022-11-29 00:15:35.229777 |
| FILE SYNC | Completed | 2022-11-29 00:15:35.773431 |
| RESTART | Completed | 2022-11-29 00:15:39.189607 |
| RECOVERY | Completed | 2022-11-29 00:15:39.978301 |
+---------------+----------------+-----------------------------------+
7 rows in set (0.00 sec)
-- 当克隆数据量比较大的时候,可以使用此SQL语句进行克隆状态查看,在克隆接收者主机上进行查看
mysql> show databases;
-- 此时克隆接收者主机上查看到的数据信息,与克隆捐赠者主机上查看到的数据信息一致,即远程克隆操作完成