1.环境
操作系统:centos6.5(主服务器IP:192.168.100.170,从服务器IP:192.168.100.171)
软件版本:mysql5.6.24
2.开始安装:
a.主库上设置从库访问的权限
grant replication slave on *.* to repl_user@'192.168.100.171' identified by '123456'; flush privileges;
可以在从库上用repl_user尝试连接
b.打开主库的二进制日志,并设置服务标识(server_id)
vim /etc/my.cnf
[mysql]
log-bin = mysql-bin
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 512M
binlog-format = MIXED
sync-binlog = 1
server_id = 1
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
binlog-ignore-db = test
auto-increment-increment = 10
auto-increment-offset = 1
c.备份主库,和获取同步初态
FLUSH TABLES WITH READ LOCK; #锁定数据库
mysqldump --master-data -root -p py_db > py_db.sql #py_db是我要做主从的库
获取同步初态:
主库的备份和同步初态获取完成之后,解锁数据库:
unlock tables;
d.从库参数设置,并设置服务标识(server_id)
vim /etc/my.cnf
[mysql]
log-bin = mysql-bin binlog_cache_size = 4M max_binlog_cache_size = 8M max_binlog_size = 512M binlog-format = MIXED sync-binlog = 1 server_id = 2 binlog-ignore-db = mysql binlog-ignore-db = information_schema binlog-ignore-db = performance_schema binlog-ignore-db = test relay_log = mysql-relay-bin log-slave-updates = on
e.从库导入备份(如果从库没有数据库,需要建立相应的数据库,再进行导入)
create database py_db default charset utf8;
mysql -uroot -p py_db < py_db.sql
如果你刚才导出了多个数据库, 需要把他们都一一上传导入。
f.开始同步(从库操作)(需要刚才获取的主库初态)。
mysql> change master to -> master_host='192.168.100.170', -> master_user='repl_user', -> master_password='123456', -> master_log_file='mysql-bin.000001', -> master_log_pos=120; Query OK, 0 rows affected, 2 warnings (0.05 sec)
g.验证主从状态:
h.主库试着编辑数据,看看是否从库会同步
3.常用命令:
mysql>slave stop; mysql>SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1 #跳过一个事务 mysql>slave start