系统架构:
IP | 主从 |
---|---|
10.1.1.20 | 互为主从 |
10.1.1.21 |
安装MySQL,上面有过程,不多说。
1、配置10.1.1.20/etc/my.cnf文件,添加以下内容:
[mysqld]
#任意自然数n,只要保证两台MySQL主机不重复就可以了。
server-id=20
#开启二进制日志
log-bin=mysql-bin
#步进值auto_imcrement。一般有n台主MySQL就填n
auto_increment_increment=2
#起始值。一般填第n台主MySQL。此时为第一台主MySQL
auto_increment_offset=1
#忽略mysql库,可不写
binlog-ignore=mysql
#忽略information_schema库,可不写
binlog-ignore=information_schema
#要同步的数据库,默认所有库
replicate-do-db=aa
配置10.1.1.21/etc/my.cnf文件,添加以下内容:
[mysqld]
#任意自然数n,只要保证两台MySQL主机不重复就可以了。
server-id=21
#开启二进制日志
log-bin=mysql-bin
#步进值auto_imcrement。一般有n台主MySQL就填n
auto_increment_increment=2
#起始值。一般填第n台主MySQL。此时为第二台主MySQL
auto_increment_offset=2
#忽略mysql库,可不写
binlog-ignore=mysql
#忽略information_schema库,可不写
binlog-ignore=information_schema
#要同步的数据库,默认所有库
replicate-do-db=aa
2、重启10.1.1.20和21上的MySQL:
service mysql restart
3、在10.1.1.20的MySQL上创建一个可以在10.1.1.21上登陆进行主从复制的用户:
mysql>grant replication slave on *.* to 'master'@'10.1.1.21'identified by '123456';
在10.1.1.21的MySQL上创建一个可以在10.1.1.20上登陆进行主从复制的用户:
mysql>grant replication slave on *.* to 'rep'@'10.1.1.20'identified by '123456';
4、查看10.1.1.20和10.1.1.21的master信息:
10.1.1.20:
mysql> show master status;
+-----------+-------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+-------+-------------+------------ ----+---------------+
| master-bin.000002 | 120 | | | |
+-------------------+----------+--------------+-------+----------------+
10.1.1.21:
+----------------+----------+--------------+-----------+---------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+-------+-------------+-----------+--------------------+
| mysql-bin.000002 | 120 | | | |
+-----------------+----------+--------------+--------------+------------+
5、在10.1.1.20和10.1.1.21的MySQL上配置主从信息,并启动slave:
10.1.1.20:
mysql>change master to
master_host='10.1.1.21',master_user='rep',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=120;
mysql>start slave;
10.1.1.21:
mysql>change master to
master_host='10.1.1.20',master_user='master',master_password='123456',master_log_file='master-bin.000002',master_log_pos=120;
mysql>start slave;
6、启动后检查slave状态:
10.1.1.20:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.1.20
Master_User: master
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 120
Relay_Log_File: localhost-relay-bin.000008
Relay_Log_Pos: 284
Relay_Master_Log_File: master-bin.000003
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: 461
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: 20
Master_UUID: 464bd858-8279-11e9-b5a7-000c29c380a1
Master_Info_File: /var/lib/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)
10.1.1.21:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.1.21
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 120
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000002
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: 460
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: 21
Master_UUID: 462be4d8-8279-11e9-b5a7-000c291e492e
Master_Info_File: /var/lib/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的‘Slave_IO_Running’和‘Slave_SQL_Running’均为‘yes’,说明主从复制正常。
以上就是两个MySQL互为主从的配置方法。