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.
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.
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.
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:
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!

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment