一、环境
192.168.1.1 MysqlA
192.168.1.2 MysqlB
二,安装配置
1.安装mysql省略,MysqlA和MysqlB版本保持一致就可以了!
2.配置mysql
1)在两台机器上给对方授权
MysqlA
GRANT all privileges ON *.* TO root@192.168.1.2 IDENTIFIED BY '123456';flush privileges;
MysqlB
GRANT all privileges ON *.* TO root@192.168.1.1 IDENTIFIED BY '123456';flush privileges;
2)配置主配置文件
在MysqlA上
[mysqld]server-id = 1log-bin = mysql-binlog-slave-updatesauto-increment-increment = 2auto-increment-offset = 1#要同步的库名#replicate-do-db = test#忽略mysql数据库的同步replicate-ignore-db=mysql#replicate-ignore-db=test
在MysqlB上
[mysqld]server-id = 2log-bin = mysql-binlog-slave-updatesauto-increment-increment = 2auto-increment-offset = 2#要同步的库名#replicate-do-db = test#忽略mysql数据库的同步replicate-ignore-db=mysql#replicate-ignore-db=test
注:二库都只有server-id不同和auto-increment-offset不同:
auto-increment-offset是用来设定数据库中自动增长的起点的,回为这两能服务器都设定了一次自动增长值2,所以它们的起点必须得不同,这样才能避免两台服务器数据同步时出现主键冲突
replicate-do-db 指定同步的数据库,我们只在两台服务器间同步test数据库
另:auto-increment-increment的值应设为整个结构中服务器的总数,本案例用到两台服务器,所以值设为2
配置完成后,重启两台数据库
service mysqld restart
或
/etc/init.d/mysql restart
3)同步两台数据库
分别在两台数据库中,执行下命令,查看potion,以及binlog。
MysqlA上面执行
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 107| | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MysqlB上面执行
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 107 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
##在 MysqlA 上
#stop slave;CHANGE MASTER TO master_host='192.168.1.2', master_user='root', master_password='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107;start slave;
##在 MysqlB 上
stop slave;CHANGE MASTER TO master_host='192.168.1.1', master_user='root', master_password='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107;start slave;
##分别查看两台数据库的slave状态。
mysql> show slave status\G;
结果:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
都为yes就说明已经配置成功了
4)验证mysql主主
在MysqlA的mysqldb数据库中
create database test;create table test (name varchar(15) not null,city varchar(15) not null,birth date not null);
在MysqlB的mysqldb数据库中能够查看到该表
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test |
+----------------+
1 row in set (0.00 sec)
在MysqlB中向表test插入数据
insert into test values ("aa","bb","2012-05-16");
mysql> select * from test;
+------+----------+------------+
| name | city | birth |
+------+----------+------------+
| aa | bb | 2012-05-16 |
+------+----------+------------+
1 row in set (0.00 sec)
到此验证完毕.
本文出自 “宅鸟乐园” 博客,请务必保留此出处http://birdinroom.blog.51cto.com/7740375/1403876