MySQL的主从备份,主要用于确保数据安全,避免一台机器硬盘损坏导致数据永久丢失;本篇文档实现一主一备。
根据上一篇文档安装两个mysql数据库,分别是192.168.209.128:3306
和192.168.209.129:3306
;
一、主库(Master)
配置
128的机器为主库:
配置主库的my.cnf ,
vim /etc/my.cnf 中增加以下两条内容
##启用二进制日志
log-bin=master_log
##服务器唯一ID,一般取IP最后一段
server-id=128
重启数据库:
service mysqld restart
进入数据库内部 ,创建从库访问主库时的用户backup
及密码123456
,192.168.209.129
为从库的地址,有几个从库就配置几条
mysql> grant all privileges on *.* to 'backup'@'192.168.209.129' identified by '123456' with grant option;
mysql> flush privileges;
查看用户及查看master状态
mysql> use mysql;
mysql> select host, user from user;
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master_log.000006 | 1185 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#记住你自己的master_log.000006和1185的值
主库配置完成。
二、从库(Slave)
配置
配置从库的my.cnf ,
vim /etc/my.cnf 中增加以下一条内容
server-id=129
重启数据库:
service mysqld restart
进入数据库内部 ,
mysql> stop slave; #停止slave
mysql> change master to master_host='192.168.209.128', master_user='backup', master_password='123456', master_log_file='master_log.000006';
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.209.128
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master_log.000006
Read_Master_Log_Pos: 1185
Relay_Log_File: localhost-relay-bin.000015
Relay_Log_Pos: 1400
Relay_Master_Log_File: master_log.000006
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: 1185
Relay_Log_Space: 1778
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: 128
Master_UUID: ffcca6d0-adb3-11e8-9d65-000c29a44819
Master_Info_File: /usr/local/mysql/data/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
配置完成 可以在主库中创建database、table、及库表的数据,可以查看从库也会相应的操作,说明配置成功。使用中和原来一样在程序中连接主库就可以。
三、常见问题
show slave status\G; 出现下面错误
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from position > file size'
- 在主库的安装bin目录下,
./mysqlbinlog /usr/local/mysql/data/master_log.000002 > yoon.log
// 如果报错:mysqlbinlog: [ERROR] unknown variable 'default-character-set=utf8mb4'; 则执行:
./mysqlbinlog --no-defaults /usr/local/mysql/data/master_log.000002 > yoon.log
tail -f yoon.log
//查看 end_log_pos 154,的数值
- 在从库的>MySQL 中
mysql> stop slave;
change master to master_host='192.168.209.128',master_port=3306,master_user='backup',master_password='123456',master_log_file='master_log.000002',master_log_pos=154;
mysql> start slave;
mysql> show slave status\G;