MySQL数据库如何克隆(带脚本)
1、捐赠者和接受者INSTALL PLUGIN clone SONAME 'mysql_clone.so';
2、创建用户及授权
捐赠者:
[*]创建克隆所需用户:
CREATE USER `clone_user`@`192.168.1.%` IDENTIFIED by 'clone_user';
GRANT BACKUP_ADMIN ON *.* TO `clone_user`@`192.168.1.%` # BACKUP_ADMIN是MySQL8.0 才有的备份锁的权限接受者:
[*]创建执行克隆权限的用户:
CREATE USER clone_user@'192.168.1.%' IDENTIFIED by 'clone_user';
GRANT CLONE_ADMIN ON *.* TO 'clone_user'@'192.168.1.%'; CLONE_ADMIN权限 = BACKUP_ADMIN权限 + SHUTDOWN权限。
SHUTDOWN权限允许用户shutdown和restart mysqld。
授权不同是因为,接受者需要restart mysqld。
3、接受者设置捐赠者列表清单
SET GLOBAL clone_valid_donor_list = '192.168.1.11:3306';
4、接受者执行
CLONE INSTANCE FROM clone_user@'192.168.1.11':3306 IDENTIFIED BY 'clone_user';注意:
ERROR 3870 (HY000): Clone Donor plugin validate_password is not active in Recipient.https://dev.mysql.com/doc/refman/5.6/en/validate-password-installation.html
5、查看进度
SELECT STAGE, STATE, END_TIME FROM performance_schema.clone_progress;
6、在接受者查询捐赠款的日志信息
SELECT BINLOG_FILE, BINLOG_POSITION FROM performance_schema.clone_status;
7、查询进度的另一条SQL
select
stage,
state,
cast(begin_time as DATETIME) as "START TIME",
cast(end_time as DATETIME) as "FINISH TIME",
lpad(sys.format_time(power(10,12) * (unix_timestamp(end_time) - unix_timestamp(begin_time))), 10, ' ') as DURATION,
lpad(concat(format(round(estimate/1024/1024,0), 0), "MB"), 16, ' ') as "Estimate",
case when begin_time is NULL then LPAD('%0', 7, ' ')
when estimate > 0 then
lpad(concat(round(data*100/estimate, 0), "%"), 7, ' ')
when end_time is NULL then lpad('0%', 7, ' ')
else lpad('100%', 7, ' ')
end as "Done(%)"
from performance_schema.clone_progress;
8、 修改主从关系
CHANGE MASTER TO MASTER_HOST='10.0.14.141', MASTER_PORT=61106 ,MASTER_USER='repl',MASTER_PASSWORD='xxxxxxxx',MASTER_AUTO_POSITION = 1;
9、clone脚本
有一步重新初始化的操作,记得修改对应的目录
#!/usr/bin/env bash
CLONE_ADMIN_USER="clone_user@'192.168.x.%'"
CLONE_VALID_DONOR_LIST="192.168.x.x"
MYSQL_PORT=3306
ROOT_PASSWORD="123456"
# 仅支持GTID模式
# GTID_MODE=1
#删除旧文件
functiondel_old_file() {
systemctl stop mysqld && rm -rf /data/logs/* && rm -rf /data1/data* && rm -rf /data/data/binlog/* && rm -rf /data/data/relaylog/*
}
function start_mysqld() {
systemctl start mysqld
OLDPASSWORD=`grep 'temporary password' /data/logs//mysqld.log | awk '{printf $NF}'`
SETPASSWDTXT="set global validate_password.policy='LOW';alter user root@localhost identified by '${ROOT_PASSWORD}';"
mysql -h localhost -P${MYSQL_PORT} -uroot -p${OLDPASSWORD} -e "${SETPASSWDTXT}" --connect-expired-password
}
function install_plugin() {
INSTALL_PLUGIN_SQL="INSTALL PLUGIN clone SONAME 'mysql_clone.so'"
mysql -h localhost -P${MYSQL_PORT} -uroot -p${ROOT_PASSWORD} -e "${INSTALL_PLUGIN_SQL}" --connect-expired-password
}
function set_clone_user() {
SET_CLONE_USER_SQL="set global validate_password.policy='LOW';CREATE USER IF NOT EXISTS ${CLONE_ADMIN_USER} IDENTIFIED by 'clone_user';GRANT BACKUP_ADMIN,CLONE_ADMIN ON *.* TO ${CLONE_ADMIN_USER};"
mysql -h localhost -P${MYSQL_PORT} -uroot -p${ROOT_PASSWORD} -e "${SET_CLONE_USER_SQL}" --connect-expired-password
}
function begin_clone() {
CLONE_SQL="SET GLOBAL clone_valid_donor_list = '${CLONE_VALID_DONOR_LIST}:${MYSQL_PORT}';CLONE INSTANCE FROM clone_user@'${CLONE_VALID_DONOR_LIST}':${MYSQL_PORT} IDENTIFIED BY 'clone_user';"
mysql -h localhost -P${MYSQL_PORT} -uroot -p${ROOT_PASSWORD} -e "${CLONE_SQL}" --connect-expired-password && echo "CLONE ENDS ... "
}
function change_master() {
CHANGE_MASTER_SQL="STOP SLAVE;CHANGE MASTER TO MASTER_HOST='${CLONE_VALID_DONOR_LIST}', MASTER_PORT=${MYSQL_PORT} ,MASTER_USER='repl',MASTER_PASSWORD='repl20150602',MASTER_AUTO_POSITION = 1;START SLAVE;"
mysql -h localhost -P${MYSQL_PORT} -uroot -p${ROOT_PASSWORD} -e "${CHANGE_MASTER_SQL}" --connect-expired-password
}
function execute_all() {
del_old_file
start_mysqld
install_plugin
set_clone_user
begin_clone
change_master
}
function usage() {
echo "mysql-clone {-h|-d|-s|-u|-c|-m|-a}"
echo "del_old_file(-d) -- stop mysqld and delete old mysql-datafiles"
echo "start_mysqld(-s) -- start mysqld and set newpassword"
echo "install_plugin(-i) -- install clone plgin"
echo "set_clone_user(-u) -- set clone user"
echo "begin_clone(-c) -- set clone donor and clone"
echo "change_master(-m) -- change master"
echo "execute_all(-a) -- execute all"
}
case "$1" in
'-d')
del_old_file
;;
'-s')
start_mysqld
;;
'-i')
install_plugin
;;
'-u')
set_clone_user
;;
'-c')
begin_clone
;;
'-m')
change_master
;;
'-a')
execute_all
;;
*)
usage
esac
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
来源:https://www.jb51.net/database/3188623j0.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]