mysql主主复制配置:
1、修改my.cnf文件
节点1:
[mysqld]
datadir = /u02/mysql
port = 3306
server_id = 1
socket = /var/lib/mysql/mysql.sock
log-error=/u02/mysql/mysqld.log
log-bin = /u02/mysql/master-bin/binlog
relay-log = /u02/mysql/slave-relay/binlog
binlog_format = row
skip-name-resolve
sync_binlog = 1
log_slave_updates = 1
read_only = 0
auto-increment-increment = 2
auto-increment-offset = 1
节点2:
[mysqld]
datadir = /u02/mysql
port = 3306
server_id = 2
socket = /var/lib/mysql/mysql.sock
log-error=/u02/mysql/mysqld.log
log-bin = /u02/mysql/master-bin/binlog
relay-log = /u02/mysql/slave-relay/binlog
binlog_format = row
skip-name-resolve
sync_binlog = 1
log_slave_updates = 1
read_only = 0
auto-increment-increment = 2
auto-increment-offset = 2
[mysql@erptest etc]$ mkdir /u02/mysql/master-bin/binlog
[mysql@erptest etc]$ mkdir /u02/mysql/slave-relay/binlog
创建复制权限用户:
节点1:
mysql> GRANT replication slave ON *.* TO 'repluser'@'10.81.35.3' identified by 'repluser';
mysql>flush privileges;
节点2:
mysql> GRANT replication slave ON *.* TO 'repluser'@'10.81.35.2' identified by 'repluser';
mysql>flush privileges;
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status \G
*************************** 1. row ***************************
File: binlog.000001
Position: 120
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.03 sec)
互为master:
节点1:
change master to master_host='10.81.35.3', master_port=3306, master_user='repluser',master_password='repluser', master_log_file='binlog.000001',master_log_pos=120;
节点2:
change master to master_host='10.81.35.2', master_port=3306, master_user='repluser',master_password='repluser', master_log_file='binlog.000001',master_log_pos=120;
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.81.35.3
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000001
Read_Master_Log_Pos: 120
Relay_Log_File: binlog.000002
Relay_Log_Pos: 280
Relay_Master_Log_File: binlog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 120
Relay_Log_Space: 444
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
Master_UUID: 98b6f1e4-7026-11e7-b381-e8611f1a5e44
Master_Info_File: /u02/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.81.35.2
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000001
Read_Master_Log_Pos: 120
Relay_Log_File: binlog.000002
Relay_Log_Pos: 280
Relay_Master_Log_File: binlog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 120
Relay_Log_Space: 444
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: cd2fab5f-7025-11e7-b37c-e8611f1a5ff8
Master_Info_File: /u02/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
测试:
节点1:
mysql> create table test_s_s as select * from mysql.user;
Query OK, 11 rows affected (0.04 sec)
Records: 11 Duplicates: 0 Warnings: 0
节点2:
mysql> select count(*) from test_s_s;
+----------+
| count(*) |
+----------+
| 11 |
+----------+
1 row in set (0.02 sec)
节点2:
mysql> drop table test_s_s;
Query OK, 0 rows affected (0.00 sec)
节点1:
mysql> select count(*) from test_s_s;
ERROR 1146 (42S02): Table 'test.test_s_s' doesn't exist
可以看到已经配置成功,任意节点上的修改都会同步到另外一个节点。