版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengyi_L/article/details/82220856
1.两台机器上开启mysql服务,机器ip分别为192.168.0.1(master) 192.168.0.2(slave)
2.master上的mysql开启bin-log服务,在my.cnf 的 [mysqld]模块添加
server-id=200 #设置主服务器的ID
innodb_flush_log_at_trx_commit=2 #
sync_binlog=1 #开启binlog日志同步功能
log-bin=mysql-bin-200 #binlog日志文件名
3.重启mysql,登录mysql服务器后输入
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show master status;
如果输出master的binlog信息则证明成功开启
| mysql-bin-200.000002 | 120 | | |
在master添加slave的数据库账号
mysql> create user 'slave'@'x.x.x.x' identified by 'pass'
赋予slave账号replication权限
mysql>grant replication slave ON *.* TO 'slave'@'X.X.X.X' identified by 'password';
至此主库的配置全部完成
4.登录slave的服务器,在my.cnf中进行相同配置
server-id=201
innodb_flush_log_at_trx_commit=2
sync_binlog=1
log-bin=mysql-bin-201
5.登录slave的mysql数据库,进行从库账号的配置,这里master-log-file 以及master-log-pos从主库的 show master status获取
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.0.1',
-> MASTER_USER='slave',
-> MASTER_PASSWORD='mysql',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin-200.000002',
-> MASTER_LOG_POS=120,
-> MASTER_CONNECT_RETRY=10;
6.验证从库配置情况,如果发现有如下两项是ok则表明从库配置完成
mysql> show slave status \G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
7.原理理解:
从库每次通过Master_Log_File和Read_Master_Log_Pos去主库找文件获取最新的的操作,完成同步操作后更新Master_Log_File和Read_Master_Log_Pos。下次同步时继续以上流程
Master_Log_File: mysql-bin-200.000005
Read_Master_Log_Pos: 120