MySQL异步复制

准备:主备库版本一致,正常安装软件。

1、主库上设置一个复制使用的账户:

mysql> grant replication slave on *.* to 'rep1'@'192.168.100.136' identified by 'dbking';

Query OK, 0 rows affected (0.18 sec)

mysql> select user,host,password from mysql.user where user='rep1';

+------+-----------------+-------------------------------------------+

| user | host            | password                                  |

+------+-----------------+-------------------------------------------+

| rep1 | 192.168.100.136 | *0E5B9DDCEF035D1E653099F4143C0F36061F7653 |

+------+-----------------+-------------------------------------------+

1 row in set (0.08 sec)

2、修改主库参数文件,开启binlog并设置slave id:

log_bin=/usr/local/mysql/data/dbking-bin.log

server_id=1

3、主数据库上设置读锁定有效:

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

4、然后得到主数据库上当前二进制日志文件及偏移量,为了在从库复制起点:

mysql> show master status;

+-------------------+----------+--------------+------------------+-------------------+

| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+-------------------+----------+--------------+------------------+-------------------+

| dbking-bin.000001 |      120 |              |                  |                   |

+-------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

5、主数据库目前已停止更新操作,现在要得到主数据库备份到从库上恢复:

[root@chavinking mysql]# tar -cvf data.dir data

6、解锁主库:

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

7、将主库备份恢复到从库:

[root@chavinking mysql]# mv data data1204

[root@chavinking mysql]# tar -xvf data.dir

8、修改从库my.cnf文件,设置server_id参数,server_id参数必须唯一:

server_id=2

9、从库使用--skip-slave-start启动数据库:

[root@chavinking mysql]# bin/mysqld_safe --skip-slave-start &

[1] 18389

[root@chavinking mysql]# 161202 08:50:16 mysqld_safe Logging to '/usr/local/mysql/data/chavinking.err'.

161202 08:50:16 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

10、对从库数据库服务器进行相应设置,制定复制使用账户,主库ip,端口以及开始进行复制的日志文件和位置等,具体如下:

wps58F2.tmp

本次试验脚本如下:

mysql> change master to

    -> master_host='192.168.80.133',

    -> master_user='rep1',

    -> master_password='dbking',

    -> master_port=3306,

    -> master_log_file='dbking-bin.000001',

    -> master_log_pos=120;

Query OK, 0 rows affected, 2 warnings (0.11 sec)

11、从库启动slave线程:

mysql> start slave;

Query OK, 0 rows affected (0.11 sec)

12、从库上查看配置信息:

mysql> show slave status G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.80.133

                  Master_User: rep1

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: dbking-bin.000004

          Read_Master_Log_Pos: 120

               Relay_Log_File: chavinking-relay-bin.000008

                Relay_Log_Pos: 284

        Relay_Master_Log_File: dbking-bin.000004

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: 120

              Relay_Log_Space: 510

              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

                  Master_UUID: 9b92b2a8-b7e0-11e6-81e4-000c29fa5a95

             Master_Info_File: /usr/local/software/mysql-5.6.24-linux-glibc2.5-x86_64/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

1 row in set (0.00 sec)

如果其中Slave_IO_Running: NO,并且日志文件报错:

2016-12-02 11:19:37 19637 [ERROR] Slave I/O: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Error_code: 1593

那么修改data目录下的auto.cnf文件后重启数据库即可解决。

原文地址:https://www.cnblogs.com/wcwen1990/p/6655329.html