阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

将Uhost上的MySQL迁移到UDB

121次阅读
没有评论

共计 4394 个字符,预计需要花费 11 分钟才能阅读完成。

一 应用场景描述
最近我们部署在 Ucloud 的一台云主机由于底层硬件故障,Ucloud 建议将这台云主机作动态迁移,但是由于这台服务器上部署有 MySQL 数据库,数据量太大,云主机迁移会很慢。所以我们需要先将这台云主机的 MySQL 迁移到 UDB, 然后切换程序里面的 MySQL 配置。

二 具体操作步骤
1. 将 Uhost 上的 MySQL 导出一份
mysqldump  -A –ignore-table=mysql.slow_log  –master-data=1 –single-transaction –quick -R –event -uroot -p > mysql_data20150203.sql

如果数据量太大,导出的时间会很长,可以使用 screen 开一个窗口,可以随时查看进度

-A 导出所有数据库
–ignore-table=<database>.<table>  不导出指定表, 如果有多个表不需要导出,需要使用多个 –ignore-table 参数

–master-data=[1|2]  如果设置 –master-data= 1 将会显示如下信息:
       

— Position to start replication or point-in-time recovery from

 
CHANGE MASTER TO MASTER_LOG_FILE=’mysql-bin.000003′, MASTER_LOG_POS=3018292;
 

这样如果设置 SLAVE 的时候就不需要再单独指定 MASTER_LOG_FILE 和 MASTER_LOG_POS
如何设置 –master-data=2,CHANGE MASTER TO 这一行将被注释掉,当设置 SLAVE 的同步需要按照这里的提示设置 MASTER_LOG_FILE 和 MASTER_LOG_POS

设置 –master-data 参数,配合 –single-transaction 参数可以不锁表。

–single-transaction 保证备份数据的一致性,目前支持 InnoDB

–quick Don’t buffer query, dump directly to stdout.
-R 导出函数和存储过程
–event Dump events

2. 导入数据到 UDB
如果数据量太大,导入的时间会很长,可以使用 screen 开一个窗口,可以随时查看进度

mysql -h10.4.21.160 -uroot -p
mysql> source mysql_data20150203.sql

这里导入数据的时候有几个小故障,由于 UDB 默认设置为 Master,开启了 binlog,所以导入数据的时候 UDB 的 binlog 增长太快,把 500G 磁盘写满后,导入失败。

可以设置导入的时候不写入 binlog

SET sql_log_bin=0

由于使用 mysqldump 导出的时候使用了 -A 参数,没有过滤掉 mysql 库,所以如果 Uhost 的 MySQL 里面没有设置除了 root 账号外具有合适权限登录的账号,那么在以下设置 Slave 的时候就会出错。
这种情况下,可以使用 –skip-grant-tables 参数重新启动 UDB,然后再重新设置 root 账号密码。

3. 设置 UDB 为 MySQL Slave, 从 Uhost 上的 MySQL 同步数据
CHANGE MASTER TO MASTER_HOST=’10.4.3.149′,MASTER_PORT=3306,MASTER_USER=’repl_user’,MASTER_PASSWORD=’xyzzy’,MASTER_LOG_FILE=’mysql-bin.000003′, MASTER_LOG_POS=3018292;

同步过程中,查看 Slave 的状态信息,主要观察以下几个参数
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0

在同步过程中,Seconds_Behind_Master 数值会越来越小,如果同步出错,会有报错信息显示

mysql> show slave status\G
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.4.3.149
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 925459151
              Relay_Log_File: mysql-relay.000012
                Relay_Log_Pos: 294731160
        Relay_Master_Log_File: mysql-bin.000005
            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: 925459151
              Relay_Log_Space: 294731312
              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: 1
1 row in set (0.00 sec)
 
mysql>

4. 停掉 Uhost 上的 MySQL
5. 更改程序里面的 MySQL 连接信息
6. 取消 UDB 的 Slave 设置
先使用 STOP SLAVE; 命令停掉 UDB 上的 SLAVE 设置
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> show slave status\G
*************************** 1. row ***************************
              Slave_IO_State: 
                  Master_Host: 10.4.3.149
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 351201559
              Relay_Log_File: mysql-relay.000016
                Relay_Log_Pos: 15191656
        Relay_Master_Log_File: mysql-bin.000006
            Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 351201559
              Relay_Log_Space: 15191808
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 2003
                Last_IO_Error: error reconnecting to master ‘repl_user@10.4.3.149:3306’ – retry-time: 60  retries: 86400
              Last_SQL_Errno: 0
              Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
            Master_Server_Id: 1
1 row in set (0.00 sec)
 
mysql>

12345 mysql> reset slave all;
Query OK, 0 rows affected (0.02 sec)
 
mysql> show slave status\G
Empty set (0.00 sec)

然后使用 RESET SLAVE ALL; 命令清楚 UDB 上的 SLAVE 设置。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计4394字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中