注:文章中的内容大部分是在网络上搜索得到,通过自己实际修改和配置,能达到所需目的,特将步骤记录下来,以便以后使用。
一、两台安装mysql
master-A:1172.16.16.90
master-B:172.16.16.190
#yum -y install mysql-server mysql
二、配置主主同步备份文件
(1)授权用户
#server mysqld start
master-A>grant replication slave,file on *.* to 'repl1'@'172.16.16.190' identified by '123456';
master-B>grant replication slave,file on *.* to 'repl2'@'172.16.16.90' 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
binlog_format=mixed
#server mysqld start
(3)检测
mysql> show master status\G //查看
(4)用change master 语句指定同步位置,在设置该步骤之前两台都先执行mysql>slave stop;
master-A>change master to master_host='172.16.16.190',master_user='repl2', master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=98;
master-B>change master to master_host='172.16.16.90',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就正常了,不过我做的时候并不成功,毕竟可能会是个方法。)
三、测试
在master-A上面新建一个表,必须在test数据库下
mysql> use test
Database changed
mysql> create table test(id int,name char(10));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test values(1,'zaq');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
在master-B上使用test库,查看发现有test表,在master-B上插入数据
mysql>use test;
mysql>show tables;
mysql> insert into test values(1,'xsw');
Query OK, 1 row affected (0.00 sec)
在master-A上可以查看到
到此mysql主主互备就结束了。
参考http://www.linuxso.com/linuxrumen/11389.html