并且支持lua来编写proxy规则,从而实现读写分离和

作者: 前端  发布:2019-12-28

MySQL Proxy公布了0.7本子,那么些版本有如下重要的订正: * script cache to improve the proxy performance * central low-level decoding + lua wrappers * plugins for proxy, admin and debug * admin plugin is now lua based (more flexible) * improved test coverage * close a connection where we did throw a assertion before可是这段时间要么阿尔法阶段,不提议在生育景况使用。当MySQL Proxy 1.0宣布之后,就足以在生产条件用了。

Mysql proxy是MySQL官方开荒三个mysql数据库前端代理,使用C语言编写,并且帮衬lua来编排proxy法规,能够用来促成mysql的读写分离,负载均衡等等成效。经过了一年多的等候,mysql proxy终于从0.6.1升格到了0.7本子。0.7本子被拆成4个模块: * a chassis which handles cmdline, service, logging, plugin loading * a core library for the life-cycle, network handling and protocol decoding * a lua layer wrapping around the internals * a set of plugins: proxy, admin, debug, replicant此外还会有七个工具: * mysql-binlog-dump * mysql-myisam-dump详细的ChangeLog如下:Changelog:Chassis * added support for SIGHUP to reopen logfiles * added win32-service support * added --basedir * added experimental --log-backtrace-on-crash * added DTrace probes * added a angel to keep the chasses alive * added --user option to change the system user the proxy runs as * added config-file support that maps cmdline options to configfile optionsCore * fixed handling of RBR for long multi-byte CHAR() fields (#37426) * fixed assertions when COM_TIME, COM_PROCESS_INFO or COM_DEBUG are received * replaced assertions in protocol decoders by proper error-codes * added handling of the 4.0 auth packets * added parser for master.info files * added a writev() network backendLua Layer * expose chassis_log_* functions to lua * added the posix and lpeg module from luaforge * use the LPEG lua-module to parse statements *9159.com, added memory profiling * added { resultset_is_needed = false } as default * moved proxy.backends.* to proxy.global.backends.* * added a lua-wrapper around some glib-2.0 functions * added a script cache * removed explicit call to lua_gc() to improve the speedProxy Plugin * track rows and bytes of a resultset * fixed quoting of SQL commands like COMMIT and ROLLBACK * added test-cases for connection-pooling, fail-over, ... * refactored the SQL tokenizer into a lua module * fixed handling -- comments in the SQL tokenizer * fixed backend state cacheAdmin Plugin * replaced the hardcoded admin commands by a lua script interface * added authentication supportDebug Plugin * return "number" as DOUBLE or INTsMySQL Binlog Dump * added a library and cmdline interface to dump binlogs incl. MySQL 5.1 RBRMyISAM Dump * added mysql-myisam-dump to dump the content of a .FRM files and .MYD files

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此处下载安装包 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:3306slave 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 proxy.response = { type = proxy.MYSQLD_PACKET_ERR, errmsg = errmsg or "error" } endfunction read_query if packet:byte() ~= proxy.COM_QUERY then set_error("[admin] we only handle text-based queries  return proxy.PROXY_SEND_RESULT end local query = packet:sub 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 endproxy.response = { type = proxy.MYSQLD_PACKET_OK, resultset = { fields = fields, rows = rows } } return proxy.PROXY_SEND_RESULT end

5、相关问题解决 [network-mysqld.c:1134]: error on a connection . closing client connection.

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

、假诺遭遇乱码需求调节后端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; #这的myapp是在mysqlnd_ms_plugin.ini中定义的

本文由9159.com发布于前端,转载请注明出处:并且支持lua来编写proxy规则,从而实现读写分离和

关键词: