Mysql,主从数据双向同步的搭建步骤

来源:互联网 发布:php 处理ios图片旋转 编辑:程序博客网 时间:2024/05/16 09:51


要求:
    2台计算机数据同步,使用MySQL数据库。


操作环境:
    Windows XP, MySQL版本:mysql-5.5.21-win32.msi

    主机(master):
    IP:10.0.1.174
    MySQL 安装路径:E:\MySQL

    从机(slave):
    IP:10.0.1.74
    MySQL 安装路径:C:\MySQL

 

一、搭建步骤


1) 主机的my.ini(在E:\MySQL目录下),在mysqld配置项中加入以下设置:     
    [mysqld]     
    server-id=1     #数据库ID号, 为1时表示为Master         
    log-bin=mysql-bin  #启用二进制日志     
    binlog-do-db=test  #需要同步的数据库名 (允许多个,多行抒写)

    # 双向备份时,增加的配置
    replicate-do-db=test #需要备份的数据库  (允许多个,多行抒写)


2) 从机的my.ini(在C:\MySQL目录下),在mysqld配置项中加入以下设置:     
    [mysqld]     
    server-id=2         #如果需要增加Slave库,则此id往后顺延
    replicate-do-db=test   #需要备份的数据库  (允许多个,多行抒写)

    # 双向备份时,增加的配置
    log-bin=mysql-bin  #启用二进制日志
    binlog-do-db=test  #需要同步的数据库名 (允许多个,多行抒写)

    据网上消息介绍,Mysql版本从5.1.7以后开始就不支持“master-host”类似的参数;


3) 重启主从机的MySQL服务。


4) 登录10.0.1.174主数据库,执行如下命令:

mysql> show master status\G;
*************************** 1. row ***************************
                         File: mysql-bin.000001
                     Position: 107
                 Binlog_Do_DB: test
             Binlog_Ignore_DB:
1 row in set (0.03 sec)

5) 登录10.0.1.74从数据库,执行如下命令:

mysql> show master status\G;
*************************** 1. row ***************************
                         File: mysql-bin.000001
                     Position: 107
                 Binlog_Do_DB: test
             Binlog_Ignore_DB:
1 row in set (0.03 sec)


6) 登录10.0.1.74从数据库,执行如下命令;
   (master_log_file对应主数据库显示的File,master_log_pos对应主数据库显示的Position)

mysql> change master to master_host='10.0.1.174', master_user='test', master_password='test', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos=107;

mysql> slave start;

mysql> show  slave status\G; 执行完这个命令会显示:
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 10.0.1.174
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: pc-zx-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          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: 107
              Relay_Log_Space: 107
              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: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
1 row in set (0.00 sec)

    从上面的状态可以看到,此时还无法从主库同步。

7) 登录10.0.1.174主数据库,执行如下命令;
   (master_log_file对应从数据库显示的File,master_log_pos对应从数据库显示的Position)

mysql> change master to master_host='10.0.1.74', master_user='back', master_password='back', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos=107;

mysql> slave start;

mysql> show  slave status\G; 执行完这个命令会显示:
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 10.0.1.74
                  Master_User: back
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: pc-zx-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          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: 107
              Relay_Log_Space: 107
              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: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
1 row in set (0.00 sec)

    从上面的状态可以看到,此时还无法从主库同步。


8) 在10.0.1.174主数据库,执行如下命令,授权给从数据库,同步相关数据:

mysql> grant replication slave on *.* to 'test'@'10.0.1.74' identified by 'test';


9) 在10.0.1.74从数据库,执行如下命令,授权给主数据库,同步相关数据:

mysql> grant replication slave on *.* to 'back'@'10.0.1.174' identified by 'back';


10) 在10.0.1.74从数据库,查看状态:

mysql> show  slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.1.174
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: pc-zx-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          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: 107
              Relay_Log_Space: 412
              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.01 sec)


11) 在10.0.1.174主数据库,查看状态:

mysql> show  slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.1.74
                  Master_User: back
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: pc-zx-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          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: 107
              Relay_Log_Space: 412
              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: 2
1 row in set (0.01 sec)

其中:
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes
说明主从机的同步配置OK。

现在可以在主数据库和从数据库都插入数据进行测试,是否双向同步......

 

原创粉丝点击