MySQL 高可用:主主复制(双主复制)

 

###########################################################################################  
  
linux:  CentOS 5.6  
mysql:  5.6.22  
MASTER A : centos152(192.168.1.152)  
MASTER B : centos153(192.168.1.153)  


#主备都启用:log-bin 和 relay_log
#若主主双方都操作,最好设置auto-increment-offset 和 auto-increment-increment,以避免冲突。若只在其中一个库操作,可不需要设置
###########################################################################################  
#master A 主服务器配置:  
[root@centos152 ~]# vi /etc/my.cnf  
  
[mysqld]  
basedir =/usr/local/mysql  
datadir =/usr/local/mysql/data  
socket=/usr/local/mysql/mysql.sock  
port = 3306  
server_id = 1
log-bin= mysql-bin
relay_log=mysql-relay-bin
binlog_format = row   
binlog-ignore-db=mysql  
binlog-ignore-db=information_schema
#auto-increment-offset=1
#auto-increment-increment=2

###########################################################################################  
#master B 备服务器配置:  
[root@centos153 ~]# vi /etc/my.cnf  
  
[mysqld]  
basedir =/usr/local/mysql  
datadir =/usr/local/mysql/data  
socket=/usr/local/mysql/mysql.sock  
port=3306  
server_id=2  
log-bin= mysql-bin
relay_log=mysql-relay-bin
binlog_format = row  
binlog-ignore-db=mysql  
binlog-ignore-db=information_schema
#auto-increment-offset=2
#auto-increment-increment=2

###########################################################################################

#配置 my.cnf 后 master A 和 master B 都重启mysqld服务  
[root@centos152 ~]# service mysqld restart  
  
[root@centos153 ~]# service mysqld restart  


#master A :备份数据  
[root@centos152 ~]# mysqldump -uroot -p --single-transaction --events --triggers --routines --flush-logs --master-data=2 --databases test > /tmp/test.sql  


#master A :备份远程传输到服务器153 (master B)
[root@centos152 ~]# scp -r /tmp/test.sql root@192.168.1.153:/tmp   
  

#master B :还原 数据库 
[root@centos153 ~]# mysql -u root -p test< /tmp/test.sql


#master A 和 master B 添加账号 
mysql> grant replication slave on *.* to 'replication'@'192.168.1.%' identified by '123456';  
mysql> flush privileges;  

###########################################################################################

#查看备份时的日志位置  
[root@centos152 ~]# grep -i "change master" /tmp/test.sql  
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000015', MASTER_LOG_POS=120;  



#【注:如果是新实例,可以直接查看 show master status; 执行下面语句,不需要备份还原】
#master B 连接到 master A 
mysql> STOP SLAVE;  
mysql>     
CHANGE MASTER TO     
    MASTER_HOST='192.168.1.152',
    MASTER_PORT=3306,
    MASTER_USER='replication',     
    MASTER_PASSWORD='123456',     
    MASTER_LOG_FILE='mysql-bin.000015',     
    MASTER_LOG_POS=120;    
mysql> START SLAVE;  


#master B 查看同步信息:
mysql> show slave status \G;   

#若看到:Slave_IO_Running 和 Slave_SQL_Running 为 Yes ,则表示两个同步线程正常运行中。  
#此时 master A 可以正常同步到  master B 了。

#接下来设置 master B 同步到 master A

###########################################################################################

#master B (192.168.1.153)查看状态
mysql> show master status; 
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000010 |     8690 |              | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+


#master A (192.168.1.152)设置同步
mysql> STOP SLAVE;  
mysql> 
CHANGE MASTER TO     
    MASTER_HOST='192.168.1.153',
    MASTER_PORT=3306,
    MASTER_USER='replication',     
    MASTER_PASSWORD='123456',     
    MASTER_LOG_FILE='mysql-bin.000010',     
    MASTER_LOG_POS=8690; 
mysql> START SLAVE; 


#master A 查看同步信息
mysql> show slave status \G;  

#若看到:Slave_IO_Running 和 Slave_SQL_Running 为 Yes ,则表示两个同步线程正常运行中。  

###########################################################################################

#查看其他信息
mysql> show master status; 
mysql> show slave status \G; 
mysql> show slave hosts;
mysql> show processlist \G;
mysql> show full processlist \G;
mysql> show master logs;

###########################################################################################

 

 


MySQL 主从复制

MySQL 主从配置或主主(双主)配置

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值