Introduction to mysql master-slave and read-write separation

Types of replication supported by mysq

1) Statement-based replication. Execute SQL statements on the server and execute the same statement on the slave server. By default, mysql adopts statement-based replication, which has high execution efficiency.

2) Row-based replication. Copy the changed content, instead of executing the command on the slave server.

3) Replication of mixed types. Statement-based replication is used by default, and row-based replication is used when it is found that statement-based replication cannot be exact.

 

Copy work process

1) Before each transaction updates the data, the master records these changes in the binary log. After writing to the binary log is complete, the master notifies the storage engine to commit the transaction.

2) Slave replicates the master's binary log to its relay log. First the slave starts a worker thread (I/O), the I/O thread opens a normal connection on the master, and then starts the binlog dump process. The binlog dump process reads events from the master's binary log. If it has caught up with the master, it sleeps and waits for the master to generate new events. The I/O thread writes these events to the relay log.

3) The Sql slave thread (sql slave thread) processes the last step of the process. The sql thread reads events from the relay log, and replays the events in it to update the slave data to be consistent with the data in the master, as long as the thread is consistent with the I /O threads are kept consistent, and the relay log will usually be located in the os cache, so the overhead of the relay log is small.

 

MySQL read and write separation

The basic principle of read-write separation is to let the main database handle transactional add, modify, and delete operations (INSERT, UPDATE, DELETE), while the slave database handles SELECT query operations.

 

The more common Mysql read-write separation is divided into the following two types

1) Internal implementation based on program code

Routing is classified according to select and insert in the code. This method is also the most widely used in the current production environment. The advantage is that the performance is better, because the program is implemented in the code, and there is no need to increase additional hardware expenses.

2) Based on the intermediate proxy layer implementation

The proxy is generally between the application server and the database server. The proxy database server receives the request from the application server and forwards it to the back-end database according to its judgment. The following representative programs are:

(1) mysql_proxy. mysql_proxy is an open source project of Mysql, which uses its own lua script for sql judgment.

(2) Atlas. It is a data middle-tier project based on MySQL protocol developed and maintained by Qihoo 360, the infrastructure team of the Web Platform Department. It is based on the 0.8.2 version of mysql-proxy, it has been optimized and some new features have been added. 360 internally uses the mysql service run by Atlas, which carries billions of read and write requests every day. Transactions and stored procedures are supported.

(3) Amoeba (Amoeba). Developed by Ali using the Java language.

 

Introduction to MySQLProxy

MySQLProxy actually establishes a connection pool between client requests and MySQLServer. All client requests are sent to MySQLProxy, and then corresponding analysis is performed through MySQLProxy to determine whether it is a read operation or a write operation, and distribute it to the corresponding MySQLServer. For multi-node slave clusters, it can also achieve the effect of load balancing

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326169409&siteId=291194637