Mysql 主主配置(互为主从)

回复 收藏
本帖最后由 追风者 于 2016-2-3 09:31 编辑

Mysql安装不说,自己查铭哥资料

mysql主主备份同步,其实就是互相主从,A对B主从,B对A主从

注:在实验之前请先关闭iptables和selinux ,iptables -F && service iptables save

vim /etc/selinux/config 中selinux值改成disabled

环境描述:A:192.168.3.116

          B:192.168.3.251

Mysql版本mysql5.1.73

Linux系统版本:CentOS6.5


给ABmysql设置登录密码,命令:mysqladmin -uroot password 'yourpass'

各登录ABmysql

A:grant replication slave,file on *.* to 'repl'@'192.168.3.251' identified by '123456';

flush privileges;

B: grant replication slave,file on *.* to 'repl'@'192.168.3.116' identified by '123456';

flush privileges;

配置各mysql配置文件

A:

vim /etc/my.cnf

在server-id出修改并添加如下语句

server-id       = 1

binlog-do-db=test

binlog-ignore-db=mysql

replicate-do-db=test

replicate-ignore-db=mysql

log-slave-updates

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1


B:

server-id       = 2

binlog-do-db=test

binlog-ignore-db=mysql

replicate-do-db=test

replicate-ignore-db=mysql

log-slave-updates

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=2

其中:log-slave-updates    将执行的复制sql记录到二进制日志
sync_binlog  当有二进制日志写入binlog文件的时候,mysql服务器将它同步到磁盘上
auto_increment_increment,auto_increment_offset 主要用于主主复制中,用来控制AUTO_INCREMENT列的操作

重启ABmysql服务,并进入MySQL数据库

A:flush tables with read lock;

show master status;查看此时A的master_log_file 和master_log_pos值

unlock tables;

slave stop;

change master to master_host='192.168.3.251',master_user='repl',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=106;#此处的master_log_file和master_log_pos为B上show master status值

start slave;


B:

flush tables with read lock;

show master status;查看此时B的master_log_file 和master_log_pos值

unlock tables;

slave stop;

change master to master_host='192.168.3.116',master_user='repl',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=106;#此处的master_log_file和master_log_pos为A上show master status值

start slave;


查看状态,在A和B上分别show slave status;若同时出现

Slave_IO_Running: Yes   

Slave_SQL_Running: Yes

则表示互为主从配置成功;

测试:针对同步的数据库test操作,分别在A上create table tb1 ,在B上show tables;若出现A上创建的tb1则表示A到B同步成功;在B上create table tb2,A上show tables;出现tb2,则表示B到A同步成功;


2016-02-02 15:35 举报
已邀请:
0

一笑而过

赞同来自:

我看下
0

心怡呆呆

赞同来自:

为啥我的show master status;这个命令执行完成后是空的。

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: