ITPub博客

首页 > 数据库 > MySQL > 数据库在线迁移的设想

数据库在线迁移的设想

原创 MySQL 作者:壹頁書 时间:2015-11-11 21:49:59 0 删除 编辑
在分布式数据库的环境下,应该经常有数据库迁移的事情.

一个基于库的数据库在线迁移.
先做一个mysqldump到目标机,
因为这个过程很长,
完成之后,不断追这段时间的binlog

然后,停止应用服务器,应用最后一段binlog
最后手工检查数据一致性.

这个只是相关的想法,脚本未经过测试.不能用于生产环境.

  1. #/bin/bash
  2. source ~/.bashrc
  3. sourcehost='127.0.0.1'
  4. sourceport=3306
  5. sourceuser='root'
  6. sourcepwd='root'
  7. targethost='192.168.16.114'
  8. targetport=3306
  9. targetuser='xx'
  10. targetpwd='xx'
  11. binlogdir='/data'
  12. db='probe'

  13. #设置master_host
  14. mysql -u$targetuser -p$targetpwd -h$targethost -P$targetport -e "stop slave;reset slave all;change master to master_host='127.0.0.1';";

  15. #初次同步
  16. mysqldump -u$sourceuser -p$sourcepwd --master-data --single-transaction --databases $db | mysql -u$targetuser -p$targetpwd -h$targethost -P$targetport

  17. #获取源数据库的binlog信息
  18. sourcebinloginfo=$(mysql -u$targetuser -p$targetpwd -h$targethost -P$targetport -Be "show slave status\G" | grep 'Master_Log_File\|Read_Master_Log_Pos' | grep -v Relay | awk '{print $2}')
  19. Master_Log_File=$(echo $sourcebinloginfo | awk -F " " '{print $1}')
  20. Read_Master_Log_Pos=$(echo $sourcebinloginfo | awk -F " " '{print $2}')

  21. echo "Master_Log_File:"$Master_Log_File
  22. echo "Read_Master_Log_Pos:"$Read_Master_Log_Pos

  1. function getSourceBinlog()
  2. {
  3.     binloginfo=$(mysql -u$sourceuser -p$sourcepwd -e "flush tables with read lock;show master status\Gunlock tables" | grep "File\|Position" | awk '{print $2}');
  4.     echo $binloginfo;
  5. }

  1. function applyBinlog()
  2. {
  3.     info=$(getSourceBinlog);
  4.     echo "info:"$info
  5.     endfile=$(echo $info | awk -F " " '{print $1}')
  6.     endpos=$(echo $info | awk -F " " '{print $2}')
  7.     while [ "$endfile" != "$startfile" ]
  8.     do
  9.         echo "Apply Binlog:"$startfile","$Read_Master_Log_Pos
  10.         mysqlbinlog $binlogdir"/"$startfile --database $db --start-position=$Read_Master_Log_Pos | mysql -u$targetuser -p$targetpwd -h$targethost -P$targetport
  11.         startfile=$(echo $startfile | awk -F "." '{printf("%s.%06d",$1,$2+1)}')
  12.         startpos=0
  13.     done
  14.     mysqlbinlog $binlogdir"/"$startfile --database $db --stop-position=$endpos | mysql -u$targetuser -p$targetpwd -h$targethost -P$targetport
  15.     startfile=$endfile
  16.     startpos=$endpos
  17. }
  18. startfile=$Master_Log_File
  19. startpos=$Read_Master_Log_Pos
  20. applyBinlog;
  21. applyBinlog;
  22. applyBinlog;
  23. applyBinlog;
  24. echo "确认停止中间件(Y/N)"
  25. read sure
  26. while [ "Y" != "$sure" ]
  27. do
  28.     echo "确认停止中间件(Y/N)"
  29.     read sure;
  30. done
  31. applyBinlog;


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1829484/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6340032