一、环境
192.168.1.1 MysqlA
192.168.1.2 MysqlB
二,安装配置
1.安装mysql省略,MysqlA和MysqlB版本保持一致就可以了!
2.配置mysql
1)在两台机器上给对方授权
MysqlA
1
2
|
GRANT all privileges ON *.* TO root@192.168.1.2 IDENTIFIED BY '123456' ;
flush privileges ;
|
MysqlB
1
2
|
GRANT all privileges ON *.* TO root@192.168.1.1 IDENTIFIED BY '123456' ;
flush privileges ;
|
2)配置主配置文件
在MysqlA上
1
2
3
4
5
6
7
8
9
10
11
|
[mysqld] server- id = 1
log-bin = mysql-bin log-slave-updates auto-increment-increment = 2 auto-increment-offset = 1 #要同步的库名 #replicate-do-db = test #忽略mysql数据库的同步 replicate-ignore-db=mysql #replicate-ignore-db=test |
在MysqlB上
1
2
3
4
5
6
7
8
9
10
11
|
[mysqld] server- id = 2
log-bin = mysql-bin log-slave-updates auto-increment-increment = 2 auto-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 上
1
2
3
|
#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 上
1
2
3
|
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状态。
1
|
mysql> show slave status\G; |
结果:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
都为yes就说明已经配置成功了
4)验证mysql主主
在MysqlA的mysqldb数据库中
1
2
|
create database test;
create table test ( name varchar (15) not null ,city varchar (15) not null ,birth date not null );
|
在MysqlB的mysqldb数据库中能够查看到该表
1
|
mysql> show tables; |
+----------------+
| Tables_in_test |
+----------------+
| test |
+----------------+
1 row in set (0.00 sec)
在MysqlB中向表test插入数据
1
|
insert into test values ( "aa" , "bb" , "2012-05-16" );
|
1
|
mysql> select * from test;
|
+------+----------+------------+
| name | city | birth |
+------+----------+------------+
| aa | bb | 2012-05-16 |
+------+----------+------------+
1 row in set (0.00 sec)
到此验证完毕.
本文转自birdinroom 51CTO博客,原文链接:http://blog.51cto.com/birdinroom/1403876,如需转载请自行联系原作者