MySQL的應用中完成讀寫分別的教程。本站提示廣大學習愛好者:(MySQL的應用中完成讀寫分別的教程)文章只能為提供參考,不一定能成為您想要的結果。以下是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/</span>
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中界說的