Home Database Mysql Tutorial 读写分离实践:mysql

读写分离实践:mysql

Jun 07, 2016 pm 03:39 PM
mysql separation practice environment system Read 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 of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Huawei's Qiankun ADS3.0 intelligent driving system will be launched in August and will be launched on Xiangjie S9 for the first time Huawei's Qiankun ADS3.0 intelligent driving system will be launched in August and will be launched on Xiangjie S9 for the first time Jul 30, 2024 pm 02:17 PM

On July 29, at the roll-off ceremony of AITO Wenjie's 400,000th new car, Yu Chengdong, Huawei's Managing Director, Chairman of Terminal BG, and Chairman of Smart Car Solutions BU, attended and delivered a speech and announced that Wenjie series models will be launched this year In August, Huawei Qiankun ADS 3.0 version was launched, and it is planned to successively push upgrades from August to September. The Xiangjie S9, which will be released on August 6, will debut Huawei’s ADS3.0 intelligent driving system. With the assistance of lidar, Huawei Qiankun ADS3.0 version will greatly improve its intelligent driving capabilities, have end-to-end integrated capabilities, and adopt a new end-to-end architecture of GOD (general obstacle identification)/PDP (predictive decision-making and control) , providing the NCA function of smart driving from parking space to parking space, and upgrading CAS3.0

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

Always new! Huawei Mate60 series upgrades to HarmonyOS 4.2: AI cloud enhancement, Xiaoyi Dialect is so easy to use Always new! Huawei Mate60 series upgrades to HarmonyOS 4.2: AI cloud enhancement, Xiaoyi Dialect is so easy to use Jun 02, 2024 pm 02:58 PM

On April 11, Huawei officially announced the HarmonyOS 4.2 100-machine upgrade plan for the first time. This time, more than 180 devices will participate in the upgrade, covering mobile phones, tablets, watches, headphones, smart screens and other devices. In the past month, with the steady progress of the HarmonyOS4.2 100-machine upgrade plan, many popular models including Huawei Pocket2, Huawei MateX5 series, nova12 series, Huawei Pura series, etc. have also started to upgrade and adapt, which means that there will be More Huawei model users can enjoy the common and often new experience brought by HarmonyOS. Judging from user feedback, the experience of Huawei Mate60 series models has improved in all aspects after upgrading HarmonyOS4.2. Especially Huawei M

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

See all articles