zoukankan      html  css  js  c++  java
  • MySQL异步复制-加强版

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

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

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

    Query OK, 0 rows affected (0.18 sec)

    说明:如果此账号仅用于复制,那么有replicate slave权限足够了,但是如果要在本地查看从库信息,还需要replicate client权限。

    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并设置server_id,注意server_id必须不能一样,server_id可以使用ip后8位+端口号标示:

    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参数必须唯一:

    Log_bin=mysql-bin

    Relay_log=mysql-relay-bin

    Log_slave_updates=1

    Read_only=1

    server_id=2

    参数说明:

    Log_bin=mysql-bin:建议主从库配置一样的名字。

    Log_slave_updates=1:决定是否将从主库接受到的更新写入从库自己的binlog中。将这个值设置为1,方便以后将这个从库升级为主库,根据需要配置一个从库,业方便恢复操作。

    Read_only=1:从库只有super权限的用户可以写入操作。

    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,端口以及开始进行复制的日志文件和位置等,具体如下:

    change master to

    master_host='xxx.xxx.xxx.xxx',

    master_port=3306,

    master_user='replic_user',

    master_password='password',

    master_log_file='log file name',

    master_log_pos=position;

    本次试验脚本如下:

    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文件后重启数据库即可解决。

  • 相关阅读:
    Linux文件和目录的权限
    nginx的默认web目录
    SSH-keygen rsa 密钥对根据私钥生成公钥
    对ajax的理解
    开关式复选框的操作
    关于弹框和原页面的切换问题
    单选框和复选框中value值得获取
    php中明明写了类函数,却报致命错误找不到类
    python函数
    python关键字
  • 原文地址:https://www.cnblogs.com/wcwen1990/p/6956550.html
Copyright © 2011-2022 走看看