mysql master 配置_MySQL双Master配置(转)

刚刚抽空做了一下MYSQL 的主主同步。把步骤写下来,至于会出现的什么问题,以后随时更新。这里我同步的数据库是TEST 1、环境描述。主机:192.168.0.231(A)主机:192.168.0.232(B)MYSQL 版本为5.1.21 2、授权用户。A: mysql> grant replication slave,file on *.* to 'repl1'@'192.168.0.232' identified by '123456';Query OK, 0 rows affected (0.00 sec) mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)B: mysql> grant replication slave,file on *.* to 'repl2'@'192.168.0.231' identified by '123456';Query OK, 0 rows affected (0.00 sec) mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)然后都停止MYSQL 服务器。 3、配置文件。在两个机器上的my.cnf里面都开启二进制日志 。A:user = mysqllog-bin=mysql-binserver-id       = 1binlog-do-db=testbinlog-ignore-db=mysqlreplicate-do-db=testreplicate-ignore-db=mysql log-slave-updatesslave-skip-errors=all

skip-name-resolve sync_binlog=1auto_increment_increment=2auto_increment_offset=1B:user = mysqllog-bin=mysql-binserver-id       = 2binlog-do-db=testbinlog-ignore-db=mysqlreplicate-do-db=testreplicate-ignore-db=mysql log-slave-updatesslave-skip-errors=all

skip-name-resolve sync_binlog=1auto_increment_increment=2auto_increment_offset=2至于这些参数的说明具体看手册。红色的部分非常重要,如果一个MASTER 挂掉的话,另外一个马上接管。紫红色的部分指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步 。 4、重新启动MYSQL服务器。在A和B上执行相同的步骤 [root@localhost ~]# /usr/local/mysql/bin/mysqld_safe &[1] 4264[root@localhost ~]# 071213 14:53:20 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'./usr/local/mysql/bin/mysqld_safe: line 366: [: -eq: unary operator expected071213 14:53:20 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data 5、进入MYSQL的SHELL。A: mysql> flush tables with read lock\GQuery OK, 0 rows affected (0.00 sec) mysql> show master status\G*************************** 1. row ***************************File: mysql-bin.000007Position: 528Binlog_Do_DB: testBinlog_Ignore_DB: mysql1 row in set (0.00 sec)B: mysql> flush tables with read lock;Query OK, 0 rows affected (0.00 sec) mysql> show master status\G*************************** 1. row ***************************File: mysql-bin.000004Position: 595Binlog_Do_DB: testBinlog_Ignore_DB: mysql1 row in set (0.00 sec)然后备份自己的数据,保持两个机器的数据一致。方法很多。完了后看下一步。 6、在各自机器上执行CHANGE MASTER TO命令。A: mysql> change master to -> master_host='192.168.0.232', -> master_user='repl2', -> master_password='123456', -> master_log_file='mysql-bin.000004', -> master_log_pos=595;Query OK, 0 rows affected (0.01 sec)mysql> start slave;Query OK, 0 rows affected (0.00 sec)B: mysql> change master to -> master_host='192.168.0.231', -> master_user='repl1', -> master_password='123456', -> master_log_file='mysql-bin.000007', -> master_log_pos=528;Query OK, 0 rows affected (0.01 sec)mysql> start slave;Query OK, 0 rows affected (0.00 sec) 7、查看各自机器上的IO进程和 SLAVE进程是否都开启。A: mysql> show processlist\G*************************** 1. row ***************************Id: 2User: replHost: 192.168.0.232:54475db: NULLCommand: Binlog DumpTime: 1590 State: Has sent all binlog to slave; waiting for binlog to be updatedInfo: NULL*************************** 2. row ***************************Id: 3User: system userHost:db: NULLCommand: ConnectTime: 1350 State: Waiting for master to send eventInfo: NULL*************************** 3. row ***************************Id: 4User: system userHost:db: NULLCommand: ConnectTime: 1149 State: Has read all relay log; waiting for the slave I/O thread to update itInfo: NULL*************************** 4. row ***************************Id: 5User: rootHost: localhostdb: testCommand: QueryTime: 0State: NULLInfo: show processlist4 rows in set (0.00 sec)B: mysql> show processlist\G*************************** 1. row ***************************Id: 1User: system userHost:db: NULLCommand: ConnectTime: 2130 State: Waiting for master to send eventInfo: NULL*************************** 2. row ***************************Id: 2User: system userHost:db: NULLCommand: ConnectTime: 1223 State: Has read all relay log; waiting for the slave I/O thread to update itInfo: NULL*************************** 3. row ***************************Id: 4User: rootHost: localhostdb: testCommand: QueryTime: 0State: NULLInfo: show processlist*************************** 4. row ***************************Id: 5User: repl2Host: 192.168.0.231:50718db: NULLCommand: Binlog DumpTime: 1398 State: Has sent all binlog to slave; waiting for binlog to be updatedInfo: NULL4 rows in set (0.00 sec)如果红色部分没有出现,检查DATA目录下的错误文件。 8、释放掉各自的锁,然后进行插数据测试。 mysql> unlock tables;Query OK, 0 rows affected (0.00 sec) 插入之前两个机器表的对比:A: mysql> show tables;+----------------+| Tables_in_test |+----------------+| t11_innodb     || t22            |+----------------+B: mysql> show tables;+----------------+| Tables_in_test |+----------------+| t11_innodb     || t22            |+----------------+ 从A机器上进行插入A: mysql> create table t11_replicas -> (id int not null auto_increment primary key, -> str varchar(255) not null) engine myisam;Query OK, 0 rows affected (0.01 sec) mysql> insert into t11_replicas(str) values -> ('This is a master to master test table');Query OK, 1 row affected (0.01 sec) mysql> show tables;+----------------+| Tables_in_test |+----------------+| t11_innodb     || t11_replicas   || t22            |+----------------+3 rows in set (0.00 sec) mysql> select * from t11_replicas;+----+---------------------------------------+| id | str                                   |+----+---------------------------------------+|  1 | This is a master to master test table |+----+---------------------------------------+1 row in set (0.00 sec)现在来看B机器: mysql> show tables;+----------------+| Tables_in_test |+----------------+| t11_innodb     || t11_replicas   || t22            |+----------------+3 rows in set (0.00 sec) mysql> select * from t11_replicas;+----+---------------------------------------+| id | str                                   |+----+---------------------------------------+|  1 | This is a master to master test table |+----+---------------------------------------+1 row in set (0.00 sec) 现在反过来从B机器上插入数据:B: mysql> insert into t11_replicas(str) values('This is a test 2');Query OK, 1 row affected (0.00 sec) mysql> select * from t11_replicas;+----+---------------------------------------+| id | str                                   |+----+---------------------------------------+|  1 | This is a master to master test table ||  2 | This is a test 2                      |+----+---------------------------------------+2 rows in set (0.00 sec) 我们来看AA: mysql> select * from t11_replicas;+----+---------------------------------------+| id | str                                   |+----+---------------------------------------+|  1 | This is a master to master test table ||  2 | This is a test 2                      |+----+---------------------------------------+2 rows in set (0.00 sec)好了。现在两个表互相为MASTER。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值