mysql双主复制

两台服务器分别为:192.168.1.11,192.168.1.13 (此处也能够是两个外网ip,即不在同一机房内的两台机器也能够)
好如今开始进行配置,方便起见下面两台机器同时操做,你们不要看混乱哦!混乱的地方我会用服务器IP标识出来!
(1)首先确认两台机器上的mysql版本,版本最好一致,mysql升级后,bin-log会有更改。
即便两台机器上的mysql版本不一致也须要Slave的版本高于Master,而咱们如今是双主同步,必定要一致哦!
(2)在192.168.1.13上首先创建同步所用的账号:python

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.1.11' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.13 sec)
mysql> flush privileges

在192.168.1.11 一样创建同步账号:mysql

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.1.13' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.13 sec)
mysql> flush privileges

(3)修改192.168.1.13数据库的配置文件my.cnf,开启BINLOG,并设置server-id的值,修改以后必须重启Mysql服务。sql

[mysqld]
log-bin=mysql-bin 
#网络惟一标识,建议是ip的最后一痊
server-id=13
#须要同步的数据库
binlog-do-db = mydb
binlog-ignore-db=mysql

修改192.168.1.11数据库的配置文件my.cnf数据库

[mysqld]
log-bin=mysql-bin 
#网络惟一标识,建议是ip的最后一痊
server-id=11
#须要同步的数据库
binlog-do-db = mydb
binlog-ignore-db=mysql

(4)分别得到服务器当前二进制日志名和偏移量,这个操做的目的是为了在从数据库启动后,从这个点开始进行数据的恢复
192.168.1.13的:服务器

mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000006
Position: 1249
Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

 192.168.1.11的:网络

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

(5)中止对主库的更新操做,并将192.168.1.13数据库文件导出一份,而且导入到192.168.1.11中(这里随意了,若是新安装的数据库就不须要)ui

#首先添加一个读锁保证数据库的一致性
mysql> flush tables with read lock;
mysql> quit;
mysqldump -uroot -p -P 3306 mydb > /usr/local/mysql/mydb.sql

#而后将test.sql传到Slave机器上,而且导入。这里要求从库必须是启动的。
mysql> -uroot -p --default-character-set=utf8 mydb < mydb.sql

#最后恢复Master机器的读锁
mysql> unlock tables

(6)配置从服务器
在192.168.1.11上执行:日志

mysql>change master to master_host='192.168.1.13',master_user='slave',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=1249; //注意不要断开
start slave;#启动slave进程

在192.168.1.13上执行:code

mysql>change master to master_host='192.168.1.11',master_user='slave',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=338;
start slave;#启动slave进程

(7)分别执行show salve status验证主从配置是否生效server

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.157.13
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 834
               Relay_Log_File: lvstest-relay-bin.000004
                Relay_Log_Pos: 980
        Relay_Master_Log_File: mysql-bin.000008
             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: 834
              Relay_Log_Space: 1284
              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)

主要看这两项:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

都显示如上,说明配置成功。

以上是主主的配制,若是须要主人的话,将192.168.1.11上的这些操做去掉就好了。

相关文章
相关标签/搜索