Mysql双主复制


实验环境mysql_1  172.16.114.15

           mysql_2  172.16.114.16

 

一、在两台机创建授权用户:


GRANT REPLICATION SLAVE ON *.* TO 'loyu'@'%' IDENTIFIED BY '123456';


二、编辑mysql配置文档


[root@mysql-1 ~]# vi /etc/my.cnf


添加如下代码:

server-id=1      

log-bin=mysql-bin

log-slave-updates(缺少之后,双主创建失败)


[root@mysql-2 ~]# vi /etc/my.cnf


server-id=2      

log-bin=mysql-bin

log-slave-updates(缺少之后,双主创建失败)


三、重启mysql服务


/etc/init.d/mysqld restart


四、在两台机上开启slave复制


mysql> flush tables;

mysql> show master status\G;

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

            File: mysql-bin.000002

        Position: 106

    Binlog_Do_DB:

Binlog_Ignore_DB:

1 row in set (0.00 sec)

 

ERROR:

No query specified

 

mysql> slave stop

    -> ;

Query OK, 0 rows affected(0.00 sec)

 

mysql>  CHANGE MASTER TOMASTER_HOST='172.16.114.16',MASTER_PORT=3306, MASTER_USER='loyu', MASTER_PASSWORD='123456',  MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=106;  

# 第二台机只需修改     MASTER_HOST='172.16.114.15

Query OK, 0 rows affected(0.03 sec)

 

mysql> start slave;

Query OK, 0 rows affected(0.00 sec)

 

mysql> show slave status\G;

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

               Slave_IO_State: Waiting formaster to send event

                  Master_Host: 172.16.114.16

                  Master_User: loyu

                  Master_Port: 3306

                Connect_Retry: 10

              Master_Log_File: mysql-bin.000002

          Read_Master_Log_Pos: 106

               Relay_Log_File:mysqld-relay-bin.000002

                Relay_Log_Pos: 251

        Relay_Master_Log_File: mysql-bin.000002

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

              Relay_Log_Space: 407

              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:

1 row in set (0.00 sec)

 

ERROR:

No query specified


五、测试


在第一台机上创建数据库

mysql> create databaseliuyuqing;

Query OK, 1 row affected(0.01 sec)

 

mysql> use liuyuqing;

Database changed

mysql> create tableloyu(number int);

Query OK, 0 rows affected(0.02 sec)

 

mysql> insert into loyuvalues(1011);

Query OK, 1 row affected (0.00 sec)


在第二台机器上查看复制情况

 Mysql双主复制_ Mysql 双主复制

 

 

在第二台机上创建数据库

 Mysql双主复制_ Mysql 双主复制_02

 

在第一台机上查看数据库复制情况

 Mysql双主复制_ Mysql 双主复制_03