MySQL主从同步
1. 两台服务器,
- master: 192.168.31.11
- slave: 192.168.31.12
2 master配置
1. 编辑主库配置文件
vim /etc/my.cnf
# 设置主服务 的ID (id可以自己随便设置但是要保证和slave的id不一样)
server-id=27
# binlog日志文件名
log-bin=master-slave-bin
# binlog的格式
binlog_format=mixed
# 这个表示只同步某个库 (如果没有此项,表示同步所有的库)
# binlog-do-db=xxxx
2. 重启数据库
systemctl restart mariadb
3. 增加从库登录主库用户以及权限
# 进入数据库
mysql -uroot -p
# 增加用户
mysql> GRANT REPLICATION SLAVE ON *.* to 'username'@'%' identified by 'mysqlmima';
Query OK, 0 rows affected, 1 warning (0.01 sec)
# 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
4. 查看状态
# 查看状态,记录File和Position
mysql> show master status \G;
*************************** 1. row ***************************
File: master-slave-bin.000001
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
3. 从库配置
1. 修改salve配置文件
vim /etc/my.cnf
#设置从库服务的ID (id可以自己随便设置但是要保证和slave的id不一样)
server-id=28
#binlog 日志文件名
log-bin=mysql-bin-28
#忽略的库
replicate-ignore-db = mysql
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
slave-skip-errors=all
slave-net-timeout=60
2. 重启数据库
# 重启数据库
systemctl restart mariadb
3. 修改从库log-bin文件
# 进入数据库
mysql -uroot -p
# 增加用户
mysql> change master to master_host='192.168.31.11',master_user='username',master_password='mysqlmima', master_log_file='master-slave-bin.000001',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
master_host: 主机的ip
master_user : 主机授权的用户.
master_password : 主机授权时候填写的密码
master_log_file : 主机show master status;中的File
master_log_pos: 主机show master status;中的Position
4. 启动salve
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
5. 查看状态
# 查看状态
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes
# 表示主从配置成功
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.31.11
Master_User: username
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-slave-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 327
Relay_Master_Log_File: master-slave-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,information_schema,performance_schema
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: 154
Relay_Log_Space: 538
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: 200
Master_UUID: 557e9fe9-e774-11ea-8415-000c29b40512
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 more updates
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified