MySQL master-slave replication
1. Why do we need master-slave replication?
-
In a system with complex business, there is such a scenario: if there is a SQL statement required
锁表
, resulting in temporary不能使用读
service, it will greatly affect the running business. Using master-slave replication, the master library is responsible for writing and the slave library is responsible for reading. In this way, even if the master library locks the table, the normal operation of the business can be guaranteed by reading the slave library. -
for data
热备
-
The business volume is increasing, and the frequency of I/O access is too high, which cannot be satisfied by a single machine. At this time, multi-database storage is performed to reduce the frequency of disk I/O access and improve the I/O performance of a single machine.
2. What is master-slave replication?
MySQL master-slave replication means that data can be replicated from a MySQL database server master node to
一个
or from a多个
slave node .
MySQL adopts the 异步复制
method by default, so that the slave node does not have to visit the master server to update its own data, the data update can be performed on a remote connection, and the slave node can copy all the databases in the master database or a specific database, or a specific table.
3. Principle
master服务器
The data changes are recorded in the binarybinlog日志
, and when the data on the master occurs改变
, the changes are written to the binary log.slave服务器
The master binary log will be detected at a certain time interval to see if it has changed, and if there is a change, aI/OThread
request master binary event will be started.- At the same time, the master node starts a dump thread for each I/O thread to send binary events to it and save it to the local relay log of the slave node.
- The slave node
启动SQL线程
will read the binary log from the relay log and replay it locally so that its data is consistent with the master node. - Finally I/OThread and SQLThread will go to sleep, waiting for the next wake up.
Summarize:
从库
Generate two threads, one I/O thread, and one SQL thread, be sure to ensure that these two are开启
in the state.I/O线程
It will request the binlog of the main library, and write the obtained binlog to the local relay-log (relay log) file.- The main library will generate one to pass the binlog
log dump线程
to the slave library I/O thread . - The SQL thread will read
relay log
the log in the file and parse it into SQL statements to execute one by one.
4. Configuration
4.1 Prepare two servers (MySQL has been installed)
master:xxx
slave:xxx
4.2 Modify the configuration
Two machines create a database with the same name at the same time.
create database test_db charset=utf8mb4;
4.2.1 master master server
- turn up
my.cnf
sudo vim /etc/mysql/my.cnf
- add this information
#在mysqld模块中添加如下配置信息
[mysqld]
log-bin=master-bin #二进制文件名称
binlog-format=ROW #二进制日志格式,有row、statement、mixed三种格式,row指的是把改变的内容复制过去,而不是把命令在从服务器上执行一遍,statement指的是在主服务器上执行的SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高。mixed指的是默认采用基于语句的复制,一旦发现基于语句的无法精确的复制时,就会采用基于行的复制。
server-id=1 # 要求各个服务器的id必须不一样
binlog-do-db=test_db # 同步的数据库名称
- Configure the account authorization for the slave server to log in to the master server
grant replication slave on *.* to 'root'@'%' identified by 'root';
- Refresh permissions
flush privileges;
- restart the master server
service mysql restart
master
Viewed in mysqlstatus
show master stataus
4.2.2 slave slave server
- find the
my.cnf
file
- configuration information
This server-id
must be remembered with the main server 不一样
!
- restart the
mysql
service
service mysqld restart
- connect to the main server
change master to master_host='主服务地址',master_user='root',master_password='root',master_port=3306,master_log_file='master-bin.000001',master_log_pos=154;
This master_log_file
is show master stataus
the information above.
This master_log_pos
is show master stataus
the information above.
- start slave
start slave
- View slave
show slave status \G(注意没有分号)
When we look at these two, it YES
's ok, be careful not to be the otherCONNECTING
- stop slave
stop slave
4.2.3 Verification
-
master
main server
-
slave
sub server
空
The state of both servers
First, we can master节点
insert a statement on
And then look at slave节点
it
so we see that it has been implemented 主从复制
.