search
HomeDatabaseMysql TutorialWhat is the principle of MySQL read and write separation?

1. The concept of read-write separation

Read-write separation is implemented based on master-slave replication. In an actual application environment, there must be a lot of read operations. Just like when we buy something on an e-commerce platform, we may only buy one or two after looking at 100 items. Therefore, there are always many more read operations than write update operations. Therefore, our read-write separation configuration based on master-slave replication means that a master database is dedicated to modifying data. When writing, it is written exclusively on the master database. The master database synchronizes data changes to the slave through master-slave replication through binlog. library, then other client query requests will eventually be mapped to the slave library, and we have one main library with two or three slave libraries. The main library is specially used for data updates (write operations), and the slave library The library is specially used for read operations. This way, the pressure of reading and writing can be well distributed. It does not need to be concentrated on the main library. It greatly improves the concurrent processing capabilities of back-end services. In addition, it has high availability capacity. In case of disaster, when the main database fails, the designated slave database can be turned into the main database.

What is the principle of MySQL read and write separation?

MySQL client communicates with MySQL Server through the API provided by mysql and uses mysql's customized TCP-based data protocol (referred to as the mysql protocol) to access the MySQL Server database. .

If there is only one server (stand-alone environment), all data additions, deletions, modifications and queries are performed on one server. As our services are used by more and more people, the traffic gradually increases. Concurrency capabilities need to be gradually improved, so if we find that database performance has reached a bottleneck, we can perform read-write separation operations to improve background services.

What is the principle of MySQL read and write separation?

The MySQL main server in the picture is dedicated to write operations. There are two MySQL slave servers connected below to specifically perform read operations. Read requests are forwarded to B and C, and write requests are forwarded. to A.

If we write code directly on the client, write operations such as insert and update are done on A, and read operations such as show and select are done on B and C. This is equivalent to the code and master-slave environment. Strongly bound. This leads to poor stability of the code, because it is strongly related to the environment. When we write code, we must know which machine is the main library responsible for writing operations, and which machine is the slave library responsible for reading operations. The code chooses . At this time, if a certain machine hangs up, the code will not know it and will still forward the request in the original way, and communication problems will occur. Therefore, it is definitely not appropriate to implement the separation of reading and writing in code. So in the actual solution, reading and writing separation needs to rely on the middleware of the database.

2. Introduce the middleware MyCat

In fact, the separation of reading and writing, and sub-database and table sub-reliance need to rely on the database middleware (mycat), which is the role of the proxy server.

What is the principle of MySQL read and write separation?

The client cannot actually tell whether it is connected to MyCat or MySQL, because the communication follows the MySQL communication protocol. How to communicate with MySQL before, how to communicate with MySQL now? MyCat communicates, so there is no need to distinguish.

Configuring read-write separation on MyCat, we do not need to make any changes to the code on the client. There is no need to distinguish which request is for reading and which is for writing in the code. The code accesses directly The one is MyCat, which parses the request and forwards it to the server responsible for the corresponding operation according to the read and write nature of SQL to achieve read and write separation.

You need to configure the information of the master server and slave servers on MyCat. There are three situations: One master and one slave, one master and multiple slaves, and multiple masters and multiple slaves

One master and multiple slaves scenario: When the writing library (master) hangs up, MyCat can also immediately turn a slave library (slave) directly into a writing library (master), which is equivalent to returning to a writing library (master). For machine processing, because there is no master-slave replication configuration between the slave library and the slave library, we also need to configure the master-slave replication between the slave library that becomes the writing library and other slave libraries.

Multiple masters and multiple slaves:

What is the principle of MySQL read and write separation?

As you can see in the picture, the MyCat server has two sets of environments. If one of them The main database is down (its corresponding slave database is also unavailable). At this time, MyCat will automatically switch to another environment, because M1 and M2 are also configured as mutual masters and slaves, so M2 can synchronize the data of M1 and provide exactly the same services as the M1 environment, so its high availability and disaster recovery capabilities are very good.

3. MyCat service port and management port

MySQL service port is 3306, MyCat service port is 8066 (this port can also be changed), which is MySQL The Client connects to port 8066. The interface you see when logging in to port 8066 is the same as logging in to port 3306 of MySQL Server. MyCat also has a management port 9066. You can log in to this management port to view all the status of MyCat's work and the connection to the back-end server, as well as the status of the connection to the data source.

The above is the detailed content of What is the principle of MySQL read and write separation?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software