Three, mysql master-slave replication

1 MySQL master-slave replication
1.1 Master-slave replication meaning
In MySQL multi-server architecture, you must have at least a master node (master), with a relative of the master node, we call it slave node (slave).
From the master copy, it is to copy the data to the master node from the one or more nodes. The master and the slave servers of the IP may be on a different, remote synchronization data connection through, this is an asynchronous process.
From the master copy of the form 1.2
A master-slave /
A master multi-slave 
From a multi-master
Dual master replication
Cascading replication
1.3 replication master from use
Data backup: copying data to a different machine, so a single server failure occurs when data is lost.
Separate read and write: Let the main library is responsible for writing, reading from the library is responsible to improve concurrency to read and write.
HA HA: When a node fails, automatically transferred to other nodes, increase availability.
Extended: Combining mechanism loads shared equally all applications access requests, reducing the stand-alone IO.
 
Master-slave replication is how to achieve it? Review: Redis master-slave replication how to achieve?
1.4 binlog
When a client for MySQL database operations, including DDL and DML statements, the server will be in the form of events recorded in the log file with all the operation records, this document is binlog file (part of the logical log file similar with Redis of AOF) .
Based binlog, we can achieve the main slave replication and data recovery. Binlog is not turned on by default, you need to configure the server manually. Note that there are some performance loss.
1.4.1 binlog Configuration
Edit /etc/my.cnf
log-bin=/var/lib/mysql/mysql-bin 
server-id=1
Restart MySQL service
service mysqld stop 
service mysqld start 
## If an error occurs view the log
we /var/log/mysqld.log
cd /var/lib/mysql
 
Whether to open binlog
show variables like 'log_bin%';

 

 

1.4.2 binlog format
STATEMENT: each SQL statement recording the modified data (the amount of log reduction, saving IO).
ROW: Which recording data is modified, modified into what looks like (after 5.7 default).
MIXED: combination of the two ways, with a STATEMENT general statement, the function of the class with the ROW.
 
View binlog format:
show global variables like '%binlog_format%';

 

 

View binlog list 
show binary logs;

 

 

View binlog content
show binlog events in 'mysql-bin.000001';

 

 

, Time-based view binlog use mysqlbinlog tool
(Note that this is Linux commands, not SQL) 
/usr/bin/mysqlbinlog --start-datetime='2019-08-22 13:30:00' --stop-datetime='2019-08-22 14:01:01' -d gupao /var/lib/mysql/mysql-bin.000001

 

 

1.5 Master-Slave Replication Works
1.5.1 master-slave configuration replication
1, open the binlog main library, provided server-id
2. Create a user has permission to copy in the main library that allows connection from the library 
GRANT REPLICATION SLAVE,
REPLICATION CLIENT ON *.* TO 'repl'@'192.168.8.147' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
3、从库/etc/my.cnf 配置,重启数据库
server-id=2 
log-bin=mysql-bin 
relay-log=mysql-relay-bin 
read-only=1
log-slave-updates=1
log-slave-updates 决定了在从 binlog 读取数据时,是否记录 binlog,实现双主和级联的关键
4、在从库执行
stop slave;
change master to master_host='192.168.8.146',
master_user='repl',master_password='123456',master_log_file='mysql-bin.000001',
master_log_pos=4;
start slave;
5、查看同步状态 
SHOW SLAVE STATUS \G
以下为正常: 

 

 

 

1.5.2 主从复制原理
这里面涉及到几个线程:

 

 

1、slave 服务器执行 start slave,开启主从复制开关, slave 服务器的 IO 线程请
求从 master 服务器读取 binlog(如果该线程追赶上了主库,会进入睡眠状态)。
2、master 服务器创建 Log Dump 线程,把 binlog 发送给 slave 服务器。slave 服
务器把读取到的 binlog 日志内容写入中继日志 relay log(会记录位置信息,以便下次继
续读取)。
3、slave 服务器的 SQL 线程会实时检测 relay log 中新增的日志内容,把 relay log
解析成 SQL 语句,并执行。

Guess you like

Origin www.cnblogs.com/flgb/p/12057711.html