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 12345 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/ 12 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 123 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"                 }             }         } } 123456789101112131415161718192021 {   "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中定义的 12 /** MySQL主机 */ define('DB_HOST' 'myapp'); #这的myapp是在mysqlnd_ms_plugin.ini中定义的

转自:http://www.zhengdazhi.com/archives/937

收 藏
分享这篇文章:
收藏+0