MySQL master-slave replication and read-write separation, after reading this you will fully understand

content

1. Master-slave replication

1. Why do you need master-slave replication?

2. What is the master-slave replication of mysql?

3. MySQL replication principle

4. Specific steps:

Second, the introduction of read-write separation

​1. The reason why MySQL read-write separation can improve system performance is:

2. Common implementations

If this blog is helpful to you, please remember to leave a message + like + favorite. 


1. Master-slave replication

1. Why do you need master-slave replication?

1. In a system with complex business, there is such a situation. There is a sql statement that needs to lock the table, so that the read service cannot be used temporarily, which will greatly affect the running business. Use master-slave replication and let the master database be responsible for writing, The slave library is responsible for reading, so that even if the master library locks the table, the normal operation of the business can be guaranteed by reading the slave library.

2. Do hot backup of data

3, the expansion of the structure. 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 the master-slave replication of mysql?

MySQL master-slave replication means that data can be replicated from a MySQL database server master node to one or more slave nodes. MySQL adopts the asynchronous replication method by default, so that the slave node does not have to access 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. MySQL replication principle

principle:

​ (1) The master server records the data changes in the binary binlog log, and when the data on the master changes, the changes are written into the binary log;

​ (2) The slave server will detect whether the master binary log has changed within a certain time interval. If there is a change, it will start an I/OThread requesting the master binary event.

​ (3) At the same time, the master node starts a dump thread for each I/O thread to send binary events to it and save them to the local relay log of the slave node. The slave node will start the SQL thread to read from the relay log Take the binary log and replay it locally, so that its data is consistent with that of the master node, and finally I/OThread and SQLThread will go to sleep, waiting for the next wake-up.

That is:

  • The slave library will generate two threads, one I/O thread and one SQL thread;

  • The I/O thread 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 a log dump thread to pass binlog to the slave library I/O thread;

  • The SQL thread will read the log in the relay log file and parse it into SQL statements to execute one by one;

Notice:

1--The master records the operation statement in the binlog log, and then grants the slave remote connection permission (the master must enable the binlog binary log function; usually for data security reasons, the slave also enables the binlog function).

2--slave opens two threads: IO thread and SQL thread. Among them: the IO thread is responsible for reading the master's binlog content into the relay log relay log; the SQL thread is responsible for reading the binlog content from the relay log log and updating it to the slave database, so that the slave data and master data can be maintained. agree.

3--Mysql replication requires at least two Mysql services. Of course, Mysql services can be distributed on different servers, or multiple services can be started on one server.

4--Mysql replication is best to ensure that the Mysql versions on the master and slave servers are the same (if the version cannot be satisfied, then ensure that the version of the master master node is lower than the version of the slave node) 5--The time between master and slave nodes need to be synchronized

4. Specific steps:

1. The slave library connects to the master library by manually executing the change master to statement, providing all the conditions of the connected user (user, password, port, ip), and letting the slave library know the starting point of the binary log (file name position number); start slave

2. Establish a connection between the IO thread of the slave library and the dump thread of the main library.

3. According to the file name and position number provided by the change master to statement from the library, the IO thread initiates a binlog request to the main library.

4. The main library dump thread sends the local binlog to the slave library IO thread in the form of events according to the request of the slave library.

5. Receive binlog events from the library IO thread and store them in the local relay-log. The transmitted information will be recorded in master.info

6. Apply the relay-log from the library SQL thread, and record the applied ones in relay-log.info. By default, the applied relays will be automatically cleaned and purged

read-write separation

Second, the introduction of read-write separation

The basic principle of MySQL read-write separation is to let the master database handle write operations, and the slave database handle read operations. The master synchronizes the changes of the write operation to each slave node.

​1. The reason why MySQL read-write separation can improve system performance is:

​ 1. The increase of physical servers and the improvement of machine processing capacity. Trade hardware for performance.

2. The master and slave are only responsible for their own reading and writing, which greatly alleviates the contention of X locks and S locks.

3. The slave can configure the myiasm engine to improve query performance and save system overhead.

​ 4. The direct writing of the master is concurrent, and the data recovery of the binlog sent by the slave through the main library is asynchronous.

5. The slave can set some parameters separately to improve its read performance.

​ 6. Increase redundancy and improve availability.

2. Common implementations

M ysql-proxy : It is an open source project of mysql, and sql is judged by its own lua script . Although it is an official product of mysql, mysql official does not recommend its use in the production environment.

Amoeba: The program is developed in the Java language. This software is dedicated to the distributed database front-end proxy layer of mysql. It mainly acts as a sql routing function when the application layer accesses mysql. Amoeba can complete high availability, load balancing, data slicing and other functions of multiple data sources.

MyCat: MyCat is currently the most popular database middleware based on Java language . It is a server that implements the MySql protocol. Its core function is to sub-database and sub-table . With the master-slave mode of the database, read- write separation can also be achieved .

If this blog is helpful to you, please remember to leave a message + like + favorite . 

Guess you like

Origin blog.csdn.net/promsing/article/details/123411966