search
HomeDatabaseMysql TutorialMySQL的使用中实现读写分离的教程_MySQL

mysql-proxy实现读写分离
MySQL Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测、分析或改变它们的通信。它使用灵活,没有限制,常见的用途包括:负载平衡,故障、查询分析,查询过滤和修改等等。
MySQL Proxy就是这么一个中间层代理,简单的说,MySQL Proxy就是一个连接池,负责将前台应用的连接请求转发给后台的数据库,并且通过使用lua脚本,可以实现复杂的连接控制和过滤,从而实现读写分离和负载平衡。对于应用来说,MySQL Proxy是完全透明的,应用则只需要连接到MySQL Proxy的监听端口即可。当然,这样proxy机器可能成为单点失效,但完全可以使用多个proxy机器做为冗余,在应用服务器的连接池配置中配置到多个proxy的连接参数即可。
MySQL Proxy更强大的一项功能是实现“读写分离”,基本原理是让主数据库处理事务性查询,让从库处理SELECT查询。数据库复制被用来把事务性查询导致的变更同步到集群中的从库。


1、安装mysql-proxy
此处下载安装包http://ftp.ntu.edu.tw/pub/MySQL/Downloads/MySQL-Proxy/
如果是编译安装依赖包有    libevent2 1.x   lua 5.1.x  glibc2 2.6.0   pkg-config   libtool 1.5
这里使用glibc的二进制包解压即可mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz

tar -zxvf mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz
 mv mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit /usr/local/mysql-proxy

2、创建好mysql主从复制

master  192.168.216.133:3306
slave   192.168.216.132:3306

3、配置mysql-proxy
创建mysql-proxy配置文件,配置文件中的所有选择都不能加引号

vim /usr/local/mysql-proxy/mysql-proxy.conf

[mysql-proxy]
 daemon=true            #以后台守护进程方式启动
 keepalive=true           #当进程故障后自动重启
 log-level=debug         #设置日志级别为debug,可以在调试完成后改成info
 log-file=/var/log/mysql-proxy.log           #设置日志文件路径
 basedir=/usr/local/mysql-proxy            #设置mysql-proxy的家目录
 proxy-address=192.168.216.132:4040     #指定mysql-proxy的监听地址
 proxy-backend-addresses=192.168.216.133:3306                   #设置后台主服务器
 proxy-read-only-backend-addresses=192.168.216.132:3306          #设置后台从服务器
 proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua   #设置读写分离脚本路径
 admin-address=192.168.216.132:4041    #设置mysql-proxy管理地址,需要家长admin插件
 admin-username=admin                 #设置登录管理地址用户
 admin-password=admin                  #设置管理用户密码
 admin-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua   
 #设置管理后台lua脚本路径,改脚本默认没有要自动定义

配置完mysql-proxy.conf后需要确保该文件的权限是600,并确保包含个lua脚本
通过配置文件启动mysql-proxy

/usr/local/mysql-proxy/bin/mysql-proxy --plugins=proxy --plugins=admin --defaults-file=mysql-proxy.conf
 --plugins=proxy    #指定proxy插件,该配置写入配置文件无法启动
 --plugins=admin    #指定admin插件
 --defaults-file=mysql-proxy.conf      #指定配置文件

4、启动测试


登录管理地址查看当前状态

mysql -uadmin -padmin -h192.168.216.132 -P4041

两个后端服务器当前状态为unknown是因为没有用户通过mysql-proxy连接到后端

mysql-proxy不对用户做身份验证,而是下身份验证交予后端服务器进行验证的,因此需要在后端服务器上对mysql-proxy开放权限

下面是自定义的admin.lua

function set_error(errmsg)
 proxy.response = {
 type = proxy.MYSQLD_PACKET_ERR,
 errmsg = errmsg or "error"
 }
 end
function read_query(packet)
 if packet:byte() ~= proxy.COM_QUERY then
 set_error("[admin] we only handle text-based queries (COM_QUERY)")
 return proxy.PROXY_SEND_RESULT
 end
 local query = packet:sub(2)
 local rows = { }
 local fields = { }
 if query:lower() == "select * from backends" then
 fields = {
 { name = "backend_ndx",type = proxy.MYSQL_TYPE_LONG },
 { name = "address",type = proxy.MYSQL_TYPE_STRING },
 { name = "state",type = proxy.MYSQL_TYPE_STRING },
 { name = "type",type = proxy.MYSQL_TYPE_STRING },
 { name = "uuid",type = proxy.MYSQL_TYPE_STRING },
 { name = "connected_clients",type = proxy.MYSQL_TYPE_LONG },
 }
 for i = 1, #proxy.global.backends do
 local states = {
 "unknown",
 "up",
 "down"           }
 local types = {
 "unknown",
 "rw",
 "ro"
 }
 local b = proxy.global.backends[i]
 rows[#rows + 1] = {
 i,
 b.dst.name,     -- configured backend address
 states[b.state + 1], -- the C-id is pushed down starting at 0
 types[b.type + 1],  -- the C-id is pushed down starting at 0
 b.uuid,       -- the MySQL Server's UUID if it is managed
 b.connected_clients -- currently connected clients
 }
 end
 elseif query:lower() == "select * from help" then
 fields = {
 { name = "command",type = proxy.MYSQL_TYPE_STRING },
 { name = "description",type = proxy.MYSQL_TYPE_STRING },
 }
 rows[#rows + 1] = { "SELECT * FROM help", "shows this help" }
 rows[#rows + 1] = { "SELECT * FROM backends", "lists the backends and their state" }
 else
 set_error("use 'SELECT * FROM help' to see the supported commands")
 return proxy.PROXY_SEND_RESULT
 end
proxy.response = {
 type = proxy.MYSQLD_PACKET_OK,
 resultset = {
 fields = fields,
 rows = rows
 }
 }
 return proxy.PROXY_SEND_RESULT
 end

5、相关问题解决
(1)、如果日志中提示 (debug) [network-mysqld.c:1134]: error on a connection (fd: -1 event: 0). closing client connection.

可以修改 rw-splitting.lua中的min_idle_connections = 4和max_idle_connections = 8的只,将其调大

(2)、如果遇到乱码需要调整后端mysql的设置的字符集

[mysqld]
 skip-character-set-client-handshake
 init-connect      = 'SET NAMES utf8'
 character_set_server  = utf8


mysqlnd_ms实现mysql读写分离
mysqlnd_ms是mysqlnd的一个插件,该插件实现了连接保存和切换、负载均衡、读写分离的功能。要想使用mysqlnd_ms的读写分离功能必须在安装php时使用–with-mysqlnd。mysqlnd实现的功能是可以不需要在php服务器上安装mysql,在php5.3之前编译安装php需要通过–with-mysql=/path/to/mysql指定mysql的安装路径。


1、安装mysqlnd_ms模块

tar -zxvf mysqlnd_ms-1.5.2.tgz
 cd mysqlnd_ms-1.5.2
 /usr/local/php/bin/phpize
 ./configure --with-php-config=/usr/local/php/bin/php-config
 make && make install

出现以下类似提示,记录下面的路径需要拥有配置php.ini

Installing shared extensions:   /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/
Installing header files:     /usr/local/php/include/php/

2、编辑 /usr/local/php/etc/php.ini

extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/mysqlnd_ms.so
 mysqlnd_ms.enable = On
 mysqlnd_ms.config_file = /usr/local/php/etc/mysqlnd_ms_plugin.ini

3、创建mysqlnd_ms_plugin.ini配置文件

{
  "myapp": {
    "master": {
      "master_0": {
        "host": "192.168.6.135",
        "socket": "\/tmp\/mysql.sock"
      }
    "slave": {
      "slave_0": {
        "host": "192.168.6.136",
        "port": "3306"
      "slave_1": {
        "host": "192.168.6.137",
        "port": "3306"
      "filters": {
        "random": {
          "sticky": "1"
        }
      }
    }
}

这里使用到了1主2从的mysql服务器
filters是定义访问从服务器的策略,random是随机选择一台服务器,strick参数设置成1是指将一次请求都指向一台服务器

4、测试
使用wordpress进行测试,编辑配置文件wp-config.php

/** MySQL主机 */
 define('DB_HOST', 'myapp'); #这的myapp是在mysqlnd_ms_plugin.ini中定义的

以上就是MySQL的使用中实现读写分离的教程_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!

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
How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

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

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

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.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment