masterA上:mysql> grant replication slave on *.* to "repl"@"192.168.10.12" identified by "123456";mysql> flush privileges;masterB上:mysql> grant replication slave on *.* to "repl"@"192.168.10.11" identified by "123456";mysql> flush privileges;
2.2 查看主库的状态
masterA上:mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000003 |120 ||| |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)masterB上mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000003 |437 ||| |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)
2.3 配置同步信息:
masterA上:mysql> change master to master_host="192.168.10.12",master_port=3306,master_user="repl",master_password="123456",master_log_file="mysql-bin.000003",master_log_pos=437;mysql> start slave;mysql> show slave statusG; 显示有如下状态则正常:Slave_IO_Running: YesSlave_SQL_Running: YesmasterB上:#本人是测试环境,可以保证没数据写入,否则需要的步骤是:先masterA锁表-->masterA备份数据-->masterA解锁表 -->masterB导入数据-->masterB设置主从-->查看主从mysql> change master to master_host="192.168.10.11",master_port=3306,master_user="repl",master_password="123456",master_log_file="mysql-bin.000003",master_log_pos=120;start slave;mysql> show slave statusG;显示有如下状态则正常:Slave_IO_Running: YesSlave_SQL_Running: Yes
3.测试主从同步
3.1 在masterA上创建一个数据库测试同步效果
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql|| performance_schema || test |+--------------------+4 rows in set (0.00 sec)mysql> create database test01;Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql|| performance_schema || test || test01 |+--------------------+5 rows in set (0.00 sec)mysql> quitBye[root@masterA data]#
3.2 到masterB查看是否已经同步创建数据库
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql|| performance_schema || test || test01 |+--------------------+5 rows in set (0.00 sec)mysql> quitBye[root@masterB data]#
一种主从报错折腾了我半天:Last_IO_Errno: 1236Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: "Could not open log file"后面修改主从同步相关 参数,确认原因是my.cnf增加了如下参数:log-bin = mysql-binrelay-log = mysql-bin从正常主主同步时的二进制日志文件显示,有2套二进制日志。因此推断上面2个参数导致不能产生2套二进制文件,故导致二进制文件错乱和丢失。本文永久更新链接地址