mysql主從複製之mysql-proxy實現讀寫分離

來源:互聯網
上載者:User

本文系統Centos6.0

MySQL代理是一個介於MySQL用戶端和MySQL伺服器之間的簡單程式,可用來監視、分析或者傳輸他們之間的通訊。他的靈活性允許你最大限度的使用它,這些包括Server Load Balancer、容錯 、分析查詢和修正等等。

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/2221425513-0.jpg" title="3.jpg" />

master : 192.168.182.128

slave   :192.168.182.129

mysql-proxy:192.168.182.130

1、主從伺服器安裝資料庫;

master、slave操作:

[root@master ~]# yum install mysql mysql-devel -y

配置主從複製;

master:

vim /etc/my.cnflog-bin=mysql-bin   #新增server-id=1              #新增

slave:

vim /etc/my.cnflog-bin=mysql-bin   #新增server-id=2             #新增

master:  添加授權使用者;

mysql> grant replication slave on *.* to 'leo'@'%' identified by 'leo123';

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/2221424348-1.jpg" title="4.jpg" />

slave:

mysql> change master to master_host='192.168.182.128',master_user='leo',master_password='leo123',master_log_file='mysql-bin.000001',master_log_pos=247;mysql> start slave;

查看狀態:

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/222142E31-2.jpg" title="5.jpg" />

主從複製ok;

下面在mysql-proxy上配置讀寫分離;

[root@mysql-proxy ~]# wget http://cdn.mysql.com/Downloads/MySQL-Proxy/mysql-proxy-0.8.3-linux-glibc2.3-x86-32bit.tar.gz[root@mysql-proxy ~]# useradd -r mysql-proxy[root@mysql-proxy ~]# tar zxvf mysql-proxy-0.8.3-linux-glibc2.3-x86-32bit.tar.gz -C /usr/local[root@mysql-proxy ~]# mv /usr/local/mysql-proxy-0.8.3-linux-glibc2.3-x86-32bit /usr/local/mysql-proxy[root@mysql-proxy ~]# export PATH=$PATH:/usr/local/mysql-proxy/bin/[root@mysql-proxy ~]# echo "PATH=$PATH:/usr/local/mysql-proxy/bin" >> /etc/profile

這裡先來介紹下mysql-proxy的相關參數;

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/222142N34-3.jpg" title="6.jpg" />

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/2221421641-4.jpg" title="7.jpg" />

--help-all   :擷取全部協助資訊;--proxy-address=host:port  :代理服務監聽的地址和連接埠;--admin-address=host:port  :管理模組監聽的地址和連接埠;--proxy-backend-addresses=host:port :後端mysql伺服器的地址和連接埠;--proxy-read-only-backend-addresses=host:port :後端唯讀mysql伺服器的地址和連接埠;--proxy-lua-script=file_name :完成mysql代理功能的Lua指令碼;--daemon  :以守護進程模式啟動mysql-proxy;--keepalive  :在mysql-proxy崩潰時嘗試重啟之;--log-file=/path/to/log_file_name :記錄檔名稱;--log-level=level :記錄層級;--log-use-syslog :基於syslog記錄日誌;--plugins=plugin:在mysql-proxy啟動時載入的外掛程式;--user=user_name  :運行mysql-proxy進程的使用者;--defaults-file=/path/to/conf_file_name : 預設使用的設定檔路徑;其配置段使用[mysql-proxy]標識;--proxy-skip-profiling : 禁用profile;--pid-file=/path/to/pid_file_name :進程檔案名稱;

下面我們啟動下:

首先在master\slave上建立用來遠端測試使用者

master:

mysql> grant all on *.* to root@'%' identified by 'root123';

slave上就不操作了;我配置mysql庫也同步;所以無需多餘操作;

開始啟動:

[root@mysql-proxy ~]# mysql-proxy --daemon --log-level=debug --user=mysql-proxy --keepalive --log-file=/var/log/mysql-proxy.log --plugins="proxy" --proxy-backend-addresses="192.168.182.128:3306" --proxy-read-only-backend-addresses="192.168.182.129:3306" --proxy-lua-script="/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua" --plugins=admin --admin-username="admin" --admin-password="admin" --admin-lua-script="/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua"

查看相關資訊:

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/22214264A-5.jpg" title="8.jpg" />

495進程是mysql-proxy啟動該進程

root是keepalive在起作用;進程崩潰時嘗試重啟;這也是mysql-proxy新的特性;

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/2221425526-6.jpg" title="9.jpg" />

4040是proxy連接埠;4041是admin連接埠;也就是管理

下面進行測試了;

開幾個終端;

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/22214225U-7.jpg" title="4.jpg" />

登入admin查看

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/2221422021-8.jpg" title="5.jpg" />

因為rw-splitting.lua指令碼預設有4個連結才啟用分離;所以多開啟幾個終端;多測試幾下;你也可以去修改裡面的相關值;

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/222142L46-9.jpg" title="2.jpg" />

出現該state;分離成功;


本文出自 “Coffee_藍山” 部落格,請務必保留此出處http://lansgg.blog.51cto.com/5675165/1242674

相關文章

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.