Home > Database > Mysql Tutorial > body text

将Uhost上的MySQL迁移到UDB

WBOY
Release: 2016-06-07 14:55:49
Original
1093 people have browsed it

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

一 应用场景描述

最近我们部署在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=.

  不导出指定表,如果有多个表不需要导出,需要使用多个--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;

--
Copy after login

这样如果设置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>
Copy after login


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>
Copy after login
mysql> reset slave all;
Query OK, 0 rows affected (0.02 sec)

mysql> show slave status\G
Empty set (0.00 sec)
Copy after login


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



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!