读写分离实践:mysql
【系统环境】 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。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.
