mysql实现主主同步(转载1)

参考http://peaceweb.blog.51cto.com/3226037/728862

一、两台安装mysql
master-A:192.168.152.128
master-B:192.168.152.129
#yum -y install mysql-server mysql

二、配置主主同步备份文件
(1)授权用户
#server mysqld start
master-A>grant replication slave,file on *.* to 'repl1'@'192.168.152.129' identified by '123456';
master-B>grant replication slave,file on *.* to 'repl2'@'192.168.152.128' identified by '123456';
#server mysqld stop 

(2)编辑配置文件vim /etc/my.cnf  加入以下内容
log-bin=mysql-bin                                               //启动二进制日志系统
server-id=1                                                         //本机数据库ID,另一台为2
binlog-do-db=test                                               //二进制同步的数据库名
binlog-ignore-db=mysql                                      //避免同步mysql用户配置
replicate-ignore-db=mysql                                   //屏蔽对mysql库的同步
replicate-do-db=test                                            //同步数据库名称
log-slave-updates
slave-skip-errors=all
sync-binlog=1
auto-increment-increment=2
auto-increment-offset=1                                       //另一台也改为2

#server  mysqld start

(3)检测
>flush tables with read lock\G                            (锁库表)
> show master status\G                                        //查看

(master-A)                                                  (master-B)

 

两个File会不一样,将A的mysql重启下即可

(4)用change master 语句指定同步位置

master-A>change master to master_host='192.168.152.129',master_user='repl2', master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=98;

master-B>change master to master_host='192.168.152.128',master_user='repl1', master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=98;

A,B都要启动
> start slave;
> show slave status\G;                                        //查看

 

IO与SQL 都是YES 即可,(有人说IO是NO解决办法是先stop slave在reset slave在start slave就正常了,不过我做的时候并不成功,毕竟可能会是个方法。)

mysql> unlock tables;     //解锁表(因为之前锁表了,所以这里要解锁)

三、测试

在master-A上使用test库,里面没表,创建个aa表

 

在master-B上使用test库,查看发现有aa表

在master-B上插入数据

 

在master-A上可以查看到

 

到此mysql主主互备就结束了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值