search
HomeDatabaseMysql Tutorial读写分离实践:mysql

读写分离实践:mysql

Jun 07, 2016 pm 03:39 PM
mysqlseparationpracticeenvironmentsystemRead and write

【系统环境】 ubuntu12.04 64bit 【步骤】 下载mysql-proxy ftp://mirror.switch.ch/mirror/mysql/Downloads/MySQL-Proxy/mysql-proxy-0.8.4.tar.gz 安装依赖包 apt-get install libevent-dev apt-get install lua5.1-dev apt-get install libglib2.0-dev 解

【系统环境】

ubuntu12.04 64bit

【步骤】

下载mysql-proxy

ftp://mirror.switch.ch/mirror/mysql/Downloads/MySQL-Proxy/mysql-proxy-0.8.4.tar.gz


安装依赖包

apt-get install libevent-dev

apt-get install lua5.1-dev

apt-get install libglib2.0-dev


解压mysql-proxy-0.8.4.tar.gz得到mysql-proxy-0.8.4,进入目录

./configure --prefix=/usr/local/mysql-proxy --with-mysql=/usr/local/mysql/

注意编译要依赖mysql,所以应该先安装mysql,否则编译不通过。

make && make install

接下来把需要用到的lua脚本拷贝到安装目录中

cp lib/rw-splitting.lua /usr/local/mysql-proxy/

修改rw-splitting.lua内容,找到如下

-- connection pool
if not proxy.global.config.rwsplit then
        proxy.global.config.rwsplit = {
                min_idle_connections = 4,
                max_idle_connections = 8,


                is_debug = false
        }
end

把4和8都换成1,因为只有当客户端连接大于配置的4时候,才会启用读写分离,否则都从master读写,换成1后,客户端多起几个连接,多查几次就发现读写分离起作用了。

cp lib/admin.lua /usr/local/mysql-proxy/

在安装目录中新建 /usr/local/mysql-proxy/mysql-proxy.cnf 配置文件,内容如下

[mysql-proxy]
admin-username = test
admin-password = mima
keepalive=true
proxy-backend-addresses = 192.168.1.101:3306    
proxy-read-only-backend-addresses = 192.168.1.93:3306 
proxy-lua-script = /usr/local/mysql-proxy/rw-splitting.lua 
admin-lua-script = /usr/local/mysql-proxy/admin.lua
log-file = /data/log/mysql-proxy.log  
log-level = debug

admin-username 访问proxy时候用到的用户名,这个要求后端所有的db都用同一套用户名密码才能访问。

admin-password 密码

keepalive 找个参数很有用,保持mysql-proxy断线重连

proxy-backend-addresses 主db(master)地址,可读可写

proxy-read-only-backend-addresses 从db(slave)地址,只读

proxy-lua-script 执行读写分离的lua脚本地址

admin-lua-script 验证用户名密码的脚本

log-file 日志地址

log-level 日志级别,debug表示调试


启动

/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/mysql-proxy.cnf&

测试

mysql -utest -pmima -P4040

这里设置端口为4040,是因为proxy默认端口是4040

连接上后,可以测试sql命令了。

【读写分离】

为了看到读写分离效果,如果原本主从db之间有replication同步,需要关掉,否则在master修改后立刻会同步到slave,这样就看不到两个db不同的差异了,读的时候不知道从哪个db读的。

确认同步已经关闭,客户端多建立几条连接,这样一开始默认连接的是master,后来的连接查询就会转到slave。试着修改一个数据库表的值,然后查询该值,多查几次会发现你修改过的值没有修改,那是因为已经读取了从db。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
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

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

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

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),