A place to hold mainly reading notes, and some technical stuff occasionally. 这里主要是一些读书笔记、感悟;还有部分技术相关的内容。
目录[-]
打开编辑MySQL配置文件:vi /etc/my.cnf
,一般server-id
用服务器ip的最后一部分,eg: 192.168.0.89,取89作为server-id
。
server-id=89
log-bin=db89-bin
binlog-do_db=rep_db
grant replication slave on *.* to rep@'从库IP' identified by '123456';
FLUSH PRIVILEGES;
USE mysql;
SELECT user, host FROM user;
flush tables with read lock;
备份,非必需
mysqldump -hhost -uroot -ppassword dbname > /opt/rep_db.sql
重点关注:File, Position,即快照,这在配置从库时需要使用
SHOW MASTER STATUS;
UNLOCK TABLES;
导入备份的数据到从库,以实现与主库一致
source rep_db.sql
server-id=110
CHANGE MASTER TO
MASTER_HOST='IP',
MASTER_USER='rep',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='db89-bin.000002', #File
MASTER_LOG_POS=6456972; #Position
主要看:两个Yes,表明主从库同步成功
SHOW SLAVE STATUS \G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
通过 START SLAVE;
与STOP SLAVE;
可分别启动与关闭从库的同步操作。
SHOW SLAVE HOSTS;
binlog
日志,这里配置同步日志保存5天。show binary logs;
show variables like '%log%';
set global expire_logs_days = 5;
replicate-wild-ignore-table=dbname.tablename
比如主键冲突错误,可以在从库配置文件中配置跳过1062
号错误:
Last_Error: Could not execute Write_rows event on table a.b; Duplicate entry ‘aa56233933e548b98bc63449861a0d26’ for key ‘PRIMARY’, Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event’s master log db89-bin.000007, end_log_pos 822402286
slave_skip_errors = 1062 # 若忽略多个错误,中间用逗号隔开,忽略所有用all
还可以配置跳过错误的个数:set GLOBAL SQL_SLAVE_SKIP_COUNTER=n;
# n为正整数,有几个错误,就跳过几个
为防止从库被意外修改,建议单独为从库专门的只读用户。
grant select on dbname.* to reader@'%' IDENTIFIED BY 'password';
flush privileges;
Reference:《高性能MySQL》第10章
If you have any questions or any bugs are found, please feel free to contact me.
Your comments and suggestions are welcome!