翼度科技»论坛 编程开发 mysql 查看内容

MySQL数据库如何克隆(带脚本)

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
1、捐赠者和接受者
  1. INSTALL PLUGIN clone SONAME 'mysql_clone.so';
复制代码
2、创建用户及授权

捐赠者:

  • 创建克隆所需用户:
  1. CREATE USER `clone_user`@`192.168.1.%` IDENTIFIED by 'clone_user';
  2. GRANT BACKUP_ADMIN ON *.* TO `clone_user`@`192.168.1.%`   # BACKUP_ADMIN是MySQL8.0 才有的备份锁的权限
复制代码
接受者:

  • 创建执行克隆权限的用户:
  1. CREATE USER clone_user@'192.168.1.%' IDENTIFIED by 'clone_user';
  2. GRANT CLONE_ADMIN ON *.* TO 'clone_user'@'192.168.1.%';
复制代码
CLONE_ADMIN权限 = BACKUP_ADMIN权限 + SHUTDOWN权限。
SHUTDOWN权限允许用户shutdown和restart mysqld。
授权不同是因为,接受者需要restart mysqld。

3、接受者设置捐赠者列表清单
  1. SET GLOBAL clone_valid_donor_list = '192.168.1.11:3306';
复制代码
4、接受者执行
  1. CLONE INSTANCE FROM clone_user@'192.168.1.11':3306 IDENTIFIED BY 'clone_user';
复制代码
注意:
  1. 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、查看进度
  1. SELECT STAGE, STATE, END_TIME FROM performance_schema.clone_progress;
复制代码
6、在接受者查询捐赠款的日志信息
  1. SELECT BINLOG_FILE, BINLOG_POSITION FROM performance_schema.clone_status;
复制代码
7、查询进度的另一条SQL
  1. select
  2. stage,
  3. state,
  4. cast(begin_time as DATETIME) as "START TIME",
  5. cast(end_time as DATETIME) as "FINISH TIME",
  6. lpad(sys.format_time(power(10,12) * (unix_timestamp(end_time) - unix_timestamp(begin_time))), 10, ' ') as DURATION,
  7. lpad(concat(format(round(estimate/1024/1024,0), 0), "MB"), 16, ' ') as "Estimate",
  8. case when begin_time is NULL then LPAD('%0', 7, ' ')
  9. when estimate > 0 then
  10. lpad(concat(round(data*100/estimate, 0), "%"), 7, ' ')
  11. when end_time is NULL then lpad('0%', 7, ' ')
  12. else lpad('100%', 7, ' ')
  13. end as "Done(%)"
  14. from performance_schema.clone_progress;
复制代码
8、 修改主从关系
  1. CHANGE MASTER TO MASTER_HOST='10.0.14.141', MASTER_PORT=61106 ,MASTER_USER='repl',MASTER_PASSWORD='xxxxxxxx',MASTER_AUTO_POSITION = 1;
复制代码
9、clone脚本

有一步重新初始化的操作,记得修改对应的目录
  1. #!/usr/bin/env bash

  2. CLONE_ADMIN_USER="clone_user@'192.168.x.%'"
  3. CLONE_VALID_DONOR_LIST="192.168.x.x"
  4. MYSQL_PORT=3306
  5. ROOT_PASSWORD="123456"

  6. # 仅支持GTID模式
  7. # GTID_MODE=1
  8. #删除旧文件
  9. function  del_old_file() {
  10.     systemctl stop mysqld && rm -rf /data/logs/* && rm -rf /data1/data* && rm -rf /data/data/binlog/* && rm -rf /data/data/relaylog/*
  11. }

  12. function start_mysqld() {
  13.     systemctl start mysqld
  14.     OLDPASSWORD=`grep 'temporary password' /data/logs//mysqld.log | awk '{printf $NF}'`
  15.     SETPASSWDTXT="set global validate_password.policy='LOW';alter user root@localhost identified by '${ROOT_PASSWORD}';"
  16.     mysql -h localhost -P${MYSQL_PORT} -uroot -p${OLDPASSWORD} -e "${SETPASSWDTXT}" --connect-expired-password
  17. }

  18. function install_plugin() {
  19.     INSTALL_PLUGIN_SQL="INSTALL PLUGIN clone SONAME 'mysql_clone.so'"
  20.     mysql -h localhost -P${MYSQL_PORT} -uroot -p${ROOT_PASSWORD} -e "${INSTALL_PLUGIN_SQL}" --connect-expired-password
  21. }
  22. function set_clone_user() {
  23.     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};"
  24.     mysql -h localhost -P${MYSQL_PORT} -uroot -p${ROOT_PASSWORD} -e "${SET_CLONE_USER_SQL}" --connect-expired-password
  25. }

  26. function begin_clone() {
  27.     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';"
  28.     mysql -h localhost -P${MYSQL_PORT} -uroot -p${ROOT_PASSWORD} -e "${CLONE_SQL}" --connect-expired-password && echo "CLONE ENDS ... "
  29. }
  30. function change_master() {
  31.     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;"
  32.     mysql -h localhost -P${MYSQL_PORT} -uroot -p${ROOT_PASSWORD} -e "${CHANGE_MASTER_SQL}" --connect-expired-password
  33. }

  34. function execute_all() {
  35.     del_old_file
  36.     start_mysqld
  37.     install_plugin
  38.     set_clone_user
  39.     begin_clone
  40.     change_master
  41. }
  42. function usage() {
  43.     echo "mysql-clone {-h|-d|-s|-u|-c|-m|-a}"
  44.     echo "del_old_file(-d)               -- stop mysqld and delete old mysql-datafiles"
  45.     echo "start_mysqld(-s)               -- start mysqld and set newpassword"
  46.     echo "install_plugin(-i)               -- install clone plgin"
  47.     echo "set_clone_user(-u)             -- set clone user"
  48.     echo "begin_clone(-c)                -- set clone donor and clone"
  49.     echo "change_master(-m)                -- change master"
  50.     echo "execute_all(-a)                -- execute all"
  51. }

  52. case "$1" in
  53.     '-d')
  54.         del_old_file
  55.         ;;
  56.     '-s')
  57.         start_mysqld
  58.         ;;
  59.     '-i')
  60.         install_plugin
  61.         ;;
  62.     '-u')
  63.         set_clone_user
  64.         ;;
  65.     '-c')
  66.         begin_clone
  67.         ;;
  68.     '-m')
  69.         change_master
  70.         ;;
  71.     '-a')
  72.         execute_all
  73.         ;;
  74.     *)
  75.         usage
  76. esac
复制代码
总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

来源:https://www.jb51.net/database/3188623j0.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
来自手机

举报 回复 使用道具