In mysql, you can use "mysql-proxy" to achieve read-write separation; "mysql-proxy" is a software officially provided by mysql to achieve read-write separation, also called middleware, which can allow the main database to Write operations are processed, and query operations are processed from the database. Database consistency is achieved through master-slave replication.
The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.
What is the implementation method of mysql read-write separation?
The plug-ins in Mysql that can realize read-write separation include mysql-proxy / Mycat / Amoeba. Mysql-proxy is a plug-in that comes with the system. This experiment mainly uses it to achieve read and write separation
mysql-proxy is a software that implements "read/write separation (Read/Write Splitting)" (officially provided by MySQL, also called middleware), the basic principle It is to let the main database handle write operations (insert, update, delete), and the slave database handles query operations (select). The consistency of the database is achieved through master-slave replication
MySQL-proxy can realize the distinction between read and write statements mainly relying on an internal Lua script (which can realize the judgment of read and write statements)
If the data writing operation is only completed on the master server (write server), no writing operation is performed on the slave server at this time, and there is no data.
At this time, another technology needs to be used to implement the master-slave server. Data consistency, this technology is called master-slave replication technology, so master-slave replication is the basis of read-write separation
Read-write separation (MySQL-Proxy) means to let the master handle the write operation and let the slave handle the read operation , very suitable for scenarios with relatively large read operations, which can reduce the pressure on the master.
Use mysql-proxy to achieve read-write separation of mysql. mysql-proxy actually acts as a proxy for the back-end mysql master-slave server. It directly accepts the client's request, analyzes the SQL statement, determines whether it is a read operation or a write operation, and then distributes it to the corresponding mysql server
Because the write operation of the database is more time-consuming than the read operation. Therefore, the separation of reading and writing in the database solves the problem of writing to the database, which affects the efficiency of queries.
Configure gtid master-slave replication on server1 and server2 first.
The previous blog on gtid master-slave replication has been completed. Explanation, I will not go into details here, only show the final effect
You can see that a westos database is established on server1, and the corresponding server2 will be synchronized
Configuration server3 proxy (mysql-proxy)
Build mysql-proxy proxy server on server3 (to realize the client writing on server1 and reading data on server2)
(1) Obtain mysql- from the physical machine Proxy installation package to server3
(2) Configure on server3
[root@server3 ~]# systemctl status mysqld ##查看mysqld服务状态 [root@server3 ~]# systemctl stop mysqld ##关闭mysqld服务,因为代理服务器要用3306端口 [root@server3 ~]# tar zxf mysql-proxy-0.8.5-linux-glibc2-x86-64bit.tar.gz -C /usr/local/ ##解压到/usr/local/目录下
Make a soft connection for management
ln -s mysql-proxy-0.8.5-linux-glibc2-x86-64bit mysql-proxy
There is no configuration file in the mysql-proxy directory, so you need to create a directory for the configuration file yourself and create the configuration file
Use the following two commands to check Parameters written in the configuration file
[root@server3 bin]# ./mysql-proxy --help [root@server3 bin]# ./mysql-proxy --help-proxy
[mysql-proxy] ##指定语句块 proxy-address=0.0.0.0:3306 ##指定proxy访问的主机和端口,3306是一个对外的通用端口 proxy-read-only-backend-addresses=172.25.254.2:3306 ##读主机的ip和端口 proxy-backend-addresses=172.25.254.1:3306 ##执行写主机的ip和端口 proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua ##指定读写分离操作使用的lua文件路径 pid-file=/usr/local/mysql-proxy/log/mysql-proxy.pid ##pid存放路径 log-file=/usr/local/mysql-proxy/log/mysql-proxy.log ##日志存放路径 plugins=proxy ##指定使用的插件 log-level=debug ##日志的等级 keepalive=true ##开启守护进程 daemon=true ##使用后台方式运行
After saving, you need to change the permissions of the configuration file to 660 and create a log directory
Modify the maximum and minimum number of connections when read-write separation occurs in the database
[root@server3 mysql-proxy]# find . -name *.lua ./share/doc/mysql-proxy/rw-splitting.lua [root@server3 mysql-proxy]# cd share/doc/mysql-proxy [root@server3 mysql-proxy]# ls [root@server3 mysql-proxy]# vim rw-splitting.lua ##将lua脚本里原本启动机制的最小4个最大8个连接,改为1和2 min_idle_connections = 1, 最小连接数 max_idle_connections = 2, 最大连接数
(3) Start mysql -proxy
/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf ##启动 cat /usr/local/mysql-proxy/log/mysql-proxy.log ##查看日志
Test read-write separation
(1) Create a new user on server1 and authorize
mysql> grant insert,update,select on *.* to kkxili@'%' identified by 'Red1hat*'; mysql> FLUSH PRIVILEGES; ##刷新授权表 mysql> USE westos; Database changed mysql> CREATE TABLE linux ( -> username varchar(10) not null, -> password varchar(15) not null); mysql>DESC linux;
(2)server3安装lsof
(3)在用户端虚拟机server4上第一次连接数据库代理server3
在server3上面:lsof -i:3306
(4)在用户端虚拟机server4上第二次连接数据库代理server3
在server3上面:lsof -i:3306
(5)在用户端虚拟机server4上第三次连接数据库代理server3
在server3上面:lsof -i:3306
开始读写分离上面是读写分离的读访问测试
写测试
在用户端插入数据
use westos; insert into linux values('user1','123');
server1和server2都可以看到插入的数据
在server2中关闭主从复制
用户端再次写入数据,看不到刚刚写的数据
写在server1上,可以查看到数据
在server2上实现了读写分离
server2重新开启主从复制可以看到数据
客户端读的是server2,server2只能读,不能写,因此看不到刚才写进去的东西,server1可以看到
实现了客户端(虚拟机)对server1的写,对server2的读
当访问数据库的用户数量很多时,数据库的代理就把后端的数据库实现读写分离
server1是写的数据库、server2是读的数据库
当server1和server2满足gtid的主从复制时,用户往数据库写入的数据其实是写入了server1,并没有写入server2,server2上面的数据是复制过去的,因此server1、server2、客户机上面都能查到刚刚写进去的数据,其实客户机查的是server2(读)
当关闭server1和server2的异步复制时,客户机往数据库写入的数据只写进了server1,没有写进去server2,server2也没有复制一份
因此server1可以查看到,server2和客户机上面都查不到刚刚写进去的数据,此时的客户机读的是server2
推荐学习:mysql视频教程
The above is the detailed content of What is the implementation method of mysql read and write separation?. For more information, please follow other related articles on the PHP Chinese website!

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc


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

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

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

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.

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
