mysql manual 16_ master-slave replication

mysql manual 16_ master-slave replication


Replication refers to the transfer of DDL and DML operations in the master database to the slave database server through the binary log for re-execution, so as to keep the data in the slave database synchronized with the master database.

process:

1. Master 主库在事务提交时,会把数据变更作为时间 Events 记录在二进制日志文件 Binlog 中
2. 主库推送二进制日志文件 Binlog 中的日志事件到从库中的中继日志 Relay Log
3. slave 重做中继日志中的事件,将改变反映它自己的数据

Advantage:

当主库出现问题,可快速切换到从库提供服务
可以在从库上执行查询,在主库上执行更新,实现读写分离,降低主库压力
可以在从库中执行备份,以避免备份影响主库的服务

Build a master-slave replication cluster:

关闭主从节点的防火墙:
service iptables stop
在master的 my.cnf 配置文件中配置如下内容:

# mysql 服务ID,保证整个集群环境中唯一:
server-id=1

# mysql binlog 日志的存储路径:
log-bin=/var/lib/mysql/mysqlbin

# 是否只读,1代表只读,0代表读写
read-only=0

# 忽略的数据库(不需要同步)
binlog-ignore-db=mysql

配置完成后需要重启mysql
service mysql restart
登录主节点mysql,创建同步数据的账户并进行授权操作:
grant replication slave on *.* to 'blu'@'192.168.100.2' identified by '123456';

flush privileges;
show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 |    10528 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
在从节点的 my.cnf 配置文件中配置如下内容:

# mysql 服务ID,保证整个集群环境中唯一:
server-id=2

# mysql binlog 日志的存储路径:
log-bin=/var/lib/mysql/mysqlbin

配置完成后需要重启mysql
service mysql restart
登录从节点mysql,指定从库对应的主库ip,用户名,密码,从哪个日志文件开始的那个位置开始同步推送日志
change master to master_host='192.168.100.1',master_user='blu',master_password='123456',master_log_file='binlog.000001',master_log_pos=10528;

开启同步:
start slave;
查看从节点状态:
show slave status\G;

Guess you like

Origin blog.csdn.net/BLU_111/article/details/108409252