环境:CentOS6.5(三个),MySQL5.7.18
master1:192.168.8.57
master2:192.168.8.58
master3:192.168.8.59

1、配置基本环境
安装操作系统,安装MySQL数据库,关闭防火墙和selinux
2、修改MySQL配置文件
master1

点击(此处)折叠或打开

  1. [root@master2 ~]# cat /etc/my.cnf
  2. [mysqld]
  3. user=mysql
  4. datadir=/mysql/data
  5. default-storage-engine=InnoDB
  6. #skip-grant-tables
  7. socket=/tmp/mysqld.sock
  8. skip_ssl
  9. log-error=/mysql/data/mysql-err.log

  10. server-id = 2
  11. log-bin = /mysql/data/mysql-bin
  12. relay-log = relay-log
  13. read-only = false
  14. log-slave-updates
  15. auto_increment_offset=2
  16. auto_increment_increment=3

  17. [client]
  18. default-character-set=utf8
  19. socket=/tmp/mysqld.sock
  20. [mysql]
  21. socket=/tmp/mysqld.sock
  22. default-character-set=utf8

master2

点击(此处)折叠或打开

  1. [root@master2 ~]# cat /etc/my.cnf
  2. [mysqld]
  3. user=mysql
  4. datadir=/mysql/data
  5. default-storage-engine=InnoDB
  6. #skip-grant-tables
  7. socket=/tmp/mysqld.sock
  8. skip_ssl
  9. log-error=/mysql/data/mysql-err.log

  10. server-id = 2
  11. log-bin = /mysql/data/mysql-bin
  12. relay-log = relay-log
  13. read-only = false
  14. log-slave-updates
  15. auto_increment_offset=2
  16. auto_increment_increment=3

  17. [client]
  18. default-character-set=utf8
  19. socket=/tmp/mysqld.sock
  20. [mysql]
  21. socket=/tmp/mysqld.sock
  22. default-character-set=utf8

master3

点击(此处)折叠或打开

  1. [root@master3 ~]# cat /etc/my.cnf
  2. [mysqld]
  3. user=mysql
  4. datadir=/mysql/data
  5. default-storage-engine=InnoDB
  6. #skip-grant-tables
  7. socket=/tmp/mysqld.sock
  8. skip_ssl
  9. log-error=/mysql/data/mysql-err.log

  10. server-id = 3
  11. log-bin = /mysql/data/mysql-bin
  12. relay-log = relay-log
  13. read-only = false
  14. log-slave-updates
  15. auto_increment_offset=2
  16. auto_increment_increment=3

  17. [client]
  18. default-character-set=utf8
  19. socket=/tmp/mysqld.sock
  20. [mysql]
  21. socket=/tmp/mysqld.sock
  22. default-character-set=utf8

注意:此次配置的是环形同步,所以需要处理主键自增问题,需要注意这两个参数
auto_increment_increment:自增值
auto_increment_offset:漂移值,也就是步长
3、在三个节点添加复制用户,并授权
master1:
grant replication slave on *.* to 'repl'@'192.168.8.58' identified by 'mysql';
master2:
grant replication slave on *.* to 'repl'@'192.168.8.59' identified by 'mysql';
master3:
grant replication slave on *.* to 'repl'@'192.168.8.57' identified by 'mysql';
4、初始化三个节点的数据
master1:
flush tables with read lock;
mysqldump -uroot -p --database test > test.sql
master2和master3同步数据
unlock tables;
5、查看三个节点的二进制日志名和偏量值,改操作的目的是从库启动之后,从这个点开始恢复数据
mysql> show master status;
+----------------------+------------+--------------------+----------------------+-------------------+
| File                        | Position   | Binlog_Do_DB   | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+------------+--------------------+----------------------+-------------------+
| mysql-bin.000010| 1675384 |                           |                             |                         |
+----------------------+------------+--------------------+----------------------+-------------------+
master1:
change master to
master_host='192.168.8.59',
master_port=3306,
master_user='repl',
master_password='mysql',
master_log_file='mysql-bin.000012',
master_log_pos=110251;
master2:
change master to
master_host='192.168.8.57',
master_port=3306,
master_user='repl',
master_password='mysql',
master_log_file='mysql-bin.000009',
master_log_pos=13142983;
master3:
change master to
master_host='192.168.8.58',
master_port=3306,
master_user='repl',
master_password='mysql',
master_log_file='mysql-bin.000011',
master_log_pos=92962;
6、启动复制进程
start slave
7、查看三个节点的复制进程状态:

点击(此处)折叠或打开

  1. mysql> show slave status \G
  2. *************************** 1. row ***************************
  3.                Slave_IO_State: Waiting for master to send event
  4.                   Master_Host: 192.168.8.59
  5.                   Master_User: repl
  6.                   Master_Port: 3306
  7.                 Connect_Retry: 10
  8.               Master_Log_File: mysql-bin.000013
  9.           Read_Master_Log_Pos: 1489066
  10.                Relay_Log_File: relay-log.000006
  11.                 Relay_Log_Pos: 1482198
  12.         Relay_Master_Log_File: mysql-bin.000013
  13.              Slave_IO_Running: Yes
  14.             Slave_SQL_Running: Yes
  15.               Replicate_Do_DB:
  16.           Replicate_Ignore_DB:
  17.            Replicate_Do_Table:
  18.        Replicate_Ignore_Table:
  19.       Replicate_Wild_Do_Table:
  20.   Replicate_Wild_Ignore_Table:
  21.                    Last_Errno: 0
  22.                    Last_Error:
  23.                  Skip_Counter: 0
  24.           Exec_Master_Log_Pos: 1489066
  25.               Relay_Log_Space: 1712245
  26.               Until_Condition: None
  27.                Until_Log_File:
  28.                 Until_Log_Pos: 0
  29.            Master_SSL_Allowed: No
  30.            Master_SSL_CA_File:
  31.            Master_SSL_CA_Path:
  32.               Master_SSL_Cert:
  33.             Master_SSL_Cipher:
  34.                Master_SSL_Key:
  35.         Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37.                 Last_IO_Errno: 0
  38.                 Last_IO_Error:
  39.                Last_SQL_Errno: 0
  40.                Last_SQL_Error:
  41.   Replicate_Ignore_Server_Ids:
  42.              Master_Server_Id: 3
  43.                   Master_UUID: c79685e5-1ce8-11e8-869c-080027656007
  44.              Master_Info_File: /mysql/data/master.info
  45.                     SQL_Delay: 0
  46.           SQL_Remaining_Delay: NULL
  47.       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  48.            Master_Retry_Count: 86400
  49.                   Master_Bind:
  50.       Last_IO_Error_Timestamp:
  51.      Last_SQL_Error_Timestamp:
  52.                Master_SSL_Crl:
  53.            Master_SSL_Crlpath:
  54.            Retrieved_Gtid_Set:
  55.             Executed_Gtid_Set:
  56.                 Auto_Position: 0
  57.          Replicate_Rewrite_DB:
  58.                  Channel_Name:
  59.            Master_TLS_Version: 

8、分别在三个节点进行增删改查,创建删除对象操作,测试同步全部正常,到此完毕。