搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,实现主从复制
主数据库: 192.168.1.39
从数据库: 192.168.1.40
mysql版本都是5.7.25, for linux
CREATE USER 'slave_user'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
# 主从复制 #
log-bin=mysql-bin
server-id=1
binlog-do-db=test #要同步多个数据库,就多加几个binlog-do-db=数据库名
binlog-do-db=test2 #要同步多个数据库,就多加几个binlog-do-db=数据库名
#binlog-ignore-db=mysql //要忽略的数据库
innodb_flush_log_at_trx_commit=1
sync_binlog=1
systemctl restart mysql
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 2460 | test | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
注意:记录好File和Position,后面要用
[mysqld]
server-id=2
#可以指定要复制的库
#replicate-do-db=test-xxx #在master端不指定binlog-do-db,在slave端用replication-do-db来过滤
#replicate-ignore-db=mysql #忽略的库
#网上还有下面配置
#relay-log=mysqld-relay-bin
mysql> stop slave;
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.39',
-> MASTER_USER='slave_user',
-> MASTER_PASSWORD='123456',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=154;
mysql> start slave;
下面是可以直接复制使用的命令
stop slave;
CHANGE MASTER TO
MASTER_HOST='192.168.1.39',
MASTER_USER='slave_user',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=154;
start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.39
Master_User: slave_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 2460
Relay_Log_File: vmware40-relay-bin.000007
Relay_Log_Pos: 597
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
....
Seconds_Behind_Master: 0 #表示已同步
....
stop slave;
set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave;