實現mysql讀寫分離的mysqlnd的mysqlnd_ms外掛程式使用教程

來源:互聯網
上載者:User

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中定義的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.