Principle and basic configuration of master-slave replication of MySQL 5.7

Keywords: MySQL Database SQL firewall

Principle:

Traditional mysql replication is master-slave replication. It has one master, one or more slaves. After submitting and executing things at the master node, it sends them (asynchronously) to the slave node through a log file for re-execution (in statement-based replication) or application (in line-based replication). By default, all server members have a complete copy of the database.

There are two kinds of master-slave replication: asynchronous replication and semi-synchronous replication. mysql defaults to asynchronous replication.

Asynchronous replication:
After executing the command on the primary node, a log file is generated, then the log file is sent to the slave node, and finally submitted. After receiving the log file from the slave node, the data (things) in the log file are synchronized, and finally the data consistent with the master node is obtained.
The results are as follows (the pictures are from the official retrieved MGR documents published in Jingdong):

Semi-synchronous replication:
Semi-synchronous replication is a synchronization step added to the protocol. After sending the log file, the master node waits for the response from the slave node and receives the synchronization confirmation from the slave node before submitting the data (to ensure that the data must be backed up).

Generally, master-slave replication involves three threads: Binlog Dump (master) - > IO Thread (slave) - > SQL Thread (slave).

Replication can only be one-way, from master to slave.
The master user writes data and generates event records into binary-log.
Slve receives the binlog from the master and then applies it sequentially to reproduce user actions on the master.

Configuration (default asynchronous replication):

Environment: redhat EL 6.5
Database Version: 5.7
Firewall and selinux: Close
Node: server 5: 172.25.12.5 master node
Serr6: 172.25.12.6 slave node

Red hat EL 6.5 MySQL 5.7 Installation Tutorial:
Rehat EL 6.5 Installation of MySQL 5.7 and Common Questions

Host node configuration:

Configure the / etc/my.cnf file:

[root@server5 ~]# cat /etc/my.cnf | grep -v '#'

[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

log-bin=mysql-bin    #Start the binary file system
server-id=1          #A positive integer value that must be between 1 and 231

#Specific parameters can also be added as needed.
#binlog-ignore-db=mysql     #Asynchronous database mysql
#binlog-do-db=test          #Just synchronize the database test, and if you want to synchronize multiple databases, write another line of binlog-do-db=***, and so on.

Start the database:

/etc/init.d/mysqld start

To add synchronization accounts to slaves:
Mysql 5.7 has password strength requirements, so in order to set a simple password, we can modify the password strength requirements by command, but this setting is temporary, permanent modification needs to download plug-ins.
Of course, you can also directly set a password that meets its strength requirements (including special characters for upper and lower case numbers).

#Setting the password level
mysql>  set global validate_password_policy=0;          
Query OK, 0 rows affected (0.00 sec)

#Set password length

mysql> set global validate_password_length=0;          
Query OK, 0 rows affected (0.00 sec)
#Set mysql password of slave synchronization account to redhat slave ip 172.25.12.6
mysql>  grant replication slave,reload,super on *.* to mysql@'172.25.12.6' identified by 'redhat';        
Query OK, 0 rows affected, 1 warning (0.11 sec)

Testing:

Save the File and Position data displayed by the master node status, and synchronize them from the node later.
File: mysql-bin.000007
Position: 463

Since then, the master node configuration has been completed

Slave node configuration:

Configure / etc/mysql.cnf:

Add this line:
server_id=2 (inconsistent with host server_id)

Start up service:

/etc/init.d/mysql.d start

Database configuration:

#Turn off slave first
mysql> stop slave;    
Query OK, 0 rows affected (0.09 sec)

#Synchronized Host Database
#logfile and pos are data previously acquired by show master status on the host, and user and passwd are synchronous accounts previously set on the host.
mysql> change master to master_host='172.25.12.5',master_user='mysql',master_password='redhat',master_log_file='mysql-bin.000007',master_log_pos=463;
Query OK, 0 rows affected, 2 warnings (0.22 sec)

#Open slave again
mysql> start slave

This synchronization is pos synchronization, that is, there is a POS value in the log file. This block is set to allow the slave node to synchronize data from the master node pos=463. The document to be synchronized is mysql-bin.000007.

Testing:

As long as slave_IO_running and slave_SQL_Running are yes, the synchronization is successful.
Represents the normal operation of two threads on slave nodes

Posted by 5kyy8lu3 on Thu, 20 Jun 2019 19:19:27 -0700