[Mysql] Advanced optimization --- Mysql master-slave replication

Mysql master-slave replication


The overall master-slave replication of Mysql is similar to the master-slave replication of redis


1. The basic principle of replication

slave 会从 master 读取 binlog 来进行数据同步

Three steps:

  1. The master records the changes to the binary log . These recording processes are called binary log time , binary log events
  2. slave to the master copy to its binary log ebents relay log (relay log)
  3. The slave redo the time in the relay log and apply the changes to its own database . MySQL replication is asynchronous and serialization of

Schematic diagram:

Insert picture description here


2. The basic principles of replication

  • Each slave has only one master
  • Each slave can only have a unique server ID
  • Each master can have multiple salves

Three, the biggest problem of replication

Copy the biggest question is: there will be a delay


Four, common configuration of one master and one slave

If it is on the host and virtual machine, make sure that they are on the same network segment

(1) The mysql version is consistent and the background runs as a service

Insert picture description here

Insert picture description here


(2) The master and slave are all configured under the [mysqld] node, all in lowercase


(3) Host modify my.ini configuration file (Windows)

Only configure two required items

① [Required] The unique ID of the main server
server-id =1

② [Must] enable binary log
log-bin=自己本地的路径/mysqlbin
log-bin=D:\Mysql5\mysql5\data\mysqlbin

③ [Optional] Start error log
log-err=自己本地的路径/mysqlerr

④ [Optional] Root directory
basedir="自己本地路径"
basedir=D:\Mysql5\mysql5

⑤ [Optional] Temporary directory
tmpdir="自己的本地路劲"

⑥ [Optional] Data Catalog
datadir="自己本地路径/Data/"
datadir=D:\Mysql5\mysql5\data

⑦ read-only=0 (host read and write support)
read-only=0

⑧ [Optional] Set the database not to be copied
binlog-lgnore-db=mysql

⑨ [Optional] Set the data to be copied
binlog-do-db=需要复制的主数据库名字

(4) Modify my.cnf configuration file from the machine (Linux)

(1) [Required] The unique ID of the slave server
server-id =2

(2) [Optional] Enable binary files
log-bin=/var/lib/mysql/mysql-bin

(5) Because the configuration file has been modified, please start the background mysql service on both the host and slave

Insert picture description here

Insert picture description here


(6) Turn off the firewall for both the host and the slave

The host manually turns off the firewall

Shut down from the machine command line:service iptables stop
Insert picture description here


(7) Create an account on the Windows host and authorize the slave

① Authorization
GRANT REPLICATION SLAVE  ON *.* TO '用户名'@'从机器数据库IP’ IDENTIFIED BY '密码';
GRANT REPLICATION SLAVE  ON *.* TO 'zhangsan'@'192.168.43.129' IDENTIFIED BY '123456';

Insert picture description here


② flush privileges;

Insert picture description here


③ Query the status of the master
show master status;

Record the value of File and Position

Insert picture description here


④ After performing this step, the main server MySQL will not be executed to prevent the change of the state value of the main server

(8) Configure the host to be copied on the Linux slave

① Configure on the slave
CHANGE MASTER TO MASTER_HOST='主机IP',MASTER_USER='zhangsan',MASTER_PASSWORD='123456',MASTER_LOG_FILE='File名字',MASTER_LOG_POS=759
CHANGE MASTER TO MASTER_HOST='192.168.43.187',MASTER_USER='zhangsan',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysqlbin.000002',MASTER_LOG_POS=1059;

Insert picture description here

Insert picture description here


② Start the copy function from the server
start slave;

Insert picture description here


③ show slave status\G

The following two parameters are both YES, indicating that the master-slave configuration is successful!

Slave_IO_Running:Yes
Slave_SQL_Running:Yes

(9) The master creates a new library, new table, insert record, and copy from the machine


(10) How to stop copying functions from the service

stop slave;

Guess you like

Origin blog.csdn.net/weixin_45260385/article/details/114901049