您的位置:首页 > 数据库 > MySQL

mysql 实现主从双向同步

2017-03-22 15:20 302 查看
在Ubuntu 14.x 64位操作系统

MySQL版本:5.5.28

我的mysql 是默认安装的,实现 192.168.0.88 和 192.168.0.89 双向同步

1,Server1:192.168.0.88

配置:my.cnf

vim/etc/mysql/my.cnf


找到 如下内容 进行修改和增加

server-id = 88          #保持唯一
binlog-do-db = testdb  #需要同步的binlog
binlog-ignore-db = mysql#不需要同步的binlog
log-slave-updates
replicate-do-db = testdb #需要同步的数据库 如果有多个可添加多个此项
replicate-ignore-db = mysql #不需要同步的数据库 如果需要多个可添加多个此项
同步testdb数据库,这里不对mysql数据库进行同步


新增同步用户:

进入mysql

mysql -uroot -ptest123


grant replication slave on *.* to slaver@192.168.0.89 identified by 'test123';


重新启动Sever1;

service mysql restart 或者 /etc/init.d/mysql restart


查看MySQL同步binlog文件名以及pos值:

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000052 |  7173181 | testdb        | mysql            |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)


2,Server2:192.168.0.89

配置:my.cnf

vim/etc/mysql/my.cnf


找到 如下内容 进行修改和增加

server-id = 89         #保持唯一
binlog-do-db = testdb  #需要同步的binlog
binlog-ignore-db = mysql#不需要同步的binlog
log-slave-updates
replicate-do-db = testdb #需要同步的数据库 如果有多个可添加多个此项
replicate-ignore-db = mysql #不需要同步的数据库 如果需要多个可添加多个此项
同步testdb数据库,这里不对mysql数据库进行同步


记得my.cnf里都要打开 二进制日志记录:log-bin = mysql-bin

新增同步用户:

进入mysql

mysql -uroot -ptest123


grant replication slave on *.* to slaver@192.168.0.88 identified by 'test123';


重新启动Sever2;

service mysql restart 或者 /etc/init.d/mysql restart


先配置 Server1 为主 Server2 为从

登录mysql

mysql -uroot -ptest123


change master to master_host='192.168.0.88',master_user='slaver',master_password='test123',master_log_file='mysql-bin.000052',master_log_pos=107;


启动从服务

slave start;


查看主从状态

mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.88
Master_User: slaver
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000052
Read_Master_Log_Pos: 7173181
Relay_Log_File: mysqld-relay-bin.000004
Relay_Log_Pos: 7171961
Relay_Master_Log_File: mysql-bin.000052
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: testdb
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 7173181
Relay_Log_Space: 7173630
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replica
4000
te_Ignore_Server_Ids:
Master_Server_Id: 88


注意

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

表示成功了了。否则失败

到这里 server 1 主 server2 从已经配置完毕

下面返过来配置一下就可以了。

下面自己测试就可以了。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql-主从 主从