1. 程式人生 > >mysql-proxy 實現讀寫分離

mysql-proxy 實現讀寫分離

實驗環境:
系統:reddhat6.5
mysql 版本:mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
mysql-proxy:mysql-proxy-0.8.4-linux-el6-x86-64bit.tar.gz
下載連結 http://ftp.ntu.edu.tw/pub/MySQL/Downloads/MySQL-Proxy/
172.25.62.4(master)
172.25.62.6 (slave)
172.25.62.3(proxy 代理伺服器)
172.25.62.2(客戶端)
基本原理:
mysql 主從複製和代理:
客戶端傳送請求給 mysql-proxy 代理伺服器,所接受道德請求又 proxy 進行判斷,如果
是寫操作,則交給 master 去處理,如果是讀操作,則交給 slave 處理。讀寫分離的策略基於
lua 指令碼實現。因此客戶端不用去區分讀寫目標,而是由 proxy 決定。
其中 Master 伺服器允許 SQL 查詢、寫入,Slave 伺服器只允許 SQL 查詢
1. 172.25.62.4 和 172.25.62.6 主從複製正常
2. proxy 代理伺服器:
(1)解壓 mysql-proxy 原始碼包到/usr/local
tar zxf mysql-proxy-0.8.4-linux-el6-x86-64bit.tar.gz -C /usr/local
mv /usr/local/mysql-proxy-0.8.4-linux-el6-x86-64bit/ mysql-proxy
cd mysql-proxy/
(2)mysql-proxy 使用 lua 指令碼語言,必須安裝 lua 軟體包,rhel6 的 lua 可以直接 yum
安裝。
yum install lua -y
cd share/doc/mysql-proxy/rw-splitting.lua ./
(3)複製 lua 指令碼(lua 指令碼包含實現讀寫分離的策略)
cp share/doc/mysql-proxy/rw-splitting.lua /usr/local/mysql-proxy/
(4)啟動 mysql-proxy 代理服務
/usr/local/mysql-proxy/bin./mysql-proxy
-P
172.25.62.3
172.25.62.4:3306 -r 172.25.62.6:3306 -s rw-splitting.lua &
*******引數解釋********
-P 172.25.62.3:3306
-b 172.25.62.4:3306
-r 172.25.62.6:3306
-s rw-splitting.lua
:
3306
-b
指定代理監聽的 ip 地址,埠
指定寫伺服器的 ip 地址,埠
指定讀伺服器的 ip 地址,埠
指定 lua 指令碼
過濾監聽埠,確認 mysql-proxy 是否啟動成功
[
[email protected]
bin]# netstat -antlpe | grep mysql
tcp
0
0 172.25.62.3:3306
LISTEN
0
9560
1124/mysql-proxy
0.0.0.0:*3. 客戶端測試:
在 Master 伺服器上授權使用者,允許其從 172.25.62.0 的客戶機遠端訪問。
mysql> grant all on *.* to [email protected]'172.25.62.%' identified by 'Westos+123';
Query OK, 0 rows affected, 1 warning (0.19 sec)
[
[email protected]
~]# mysql -h172.25.62.3 -uhzy -p
//客戶端上訪問代理伺服器的
mysql 資料庫
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6341
Server version: 5.7.17-log MySQL Community Server (GPL)
mysql> create database student;
//新建 student 庫
Query OK, 1 row affected (0.11 sec)
mysql> use student;
Database changed
mysql> create table info(id int(4),name varchar(48)); //建立 info 表
Query OK, 0 rows affected (1.21 sec)
mysql> insert into info values(1,'westos'),(2,'linux'); //插入資料資訊
Query OK, 2 rows affected (0.18 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from info;
+------+--------+
| id | name
|
+------+--------+
|
1 | westos |
|
2 | linux |
+------+--------+
2 rows in set (0.01 sec)
mysql> quit
4. 在 master 和 slave 上檢視客戶端新建的資料庫表資訊
master:
[
[email protected]
~]# mysql -h172.25.62.4 -uhzy -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6342
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+--------------------+
| Database
|
+--------------------+
| information_schema |
| mysql
|
| performance_schema |
| student
|
| sys
|
+--------------------+
5 rows in set (0.00 sec)
mysql> use student;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| info
|
+-------------------+
1 row in set (0.00 sec)
mysql> select * from info;
+------+--------+
| id | name
|
+------+--------+
|
1 | westos |
|
2 | linux |
+------+--------+
2 rows in set (0.00 sec)
登陸 slave:
[[email protected] ~]# mysql -h172.25.62.6 -uhzy -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+| Database
|
+--------------------+
| information_schema |
| mysql
|
| performance_schema |
| student
|
| sys
|
+--------------------+
5 rows in set (0.00 sec)
mysql> select * from studnet.info;
ERROR 1146 (42S02): Table 'studnet.info' doesn't exist
mysql> select * from student.info;
+------+--------+
| id | name
|
+------+--------+
|
1 | westos |
|
2 | linux |
+------+--------+
2 rows in set (0.00 sec)
5.過濾 proxy 伺服器上 mysql 訪問的網路連線:

netstat -anpt | grep mysql

[[email protected] ~]# netstat -antlpe |grep mysql
tcp         0        0  172.25.62.3:3306              0.0.0.0:*                                  LISTEN            0
       10315         1191/mysql-proxy                         
tcp         0        0  172.25.62.3:46553            172.25.62.4:3306                   ESTABLISHED 0
       10319         1191/mysql-proxy
Proxy 連線了 master(172.25.62.4)執行寫操作。

master 上:

[[email protected] ~]# netstat -antlpe |grep mysql
tcp        0         0  :::3306                                                      :::*                                           LISTEN             27

       16680          1890/mysqld

tcp        0         0  ::ffff:172.25.62.4:3306                              ::ffff:172.25.62.3:46553          ESTABLISHED  27

       16695          1890/mysqld

tcp        0         0  ::ffff:172.25.62.4:3306                              ::ffff:172.25.62.6:33130          ESTABLISHED  27

       16691          1890/mysqld

相關推薦

Mysql-proxy實現分離

讀寫分離,基本的原理是讓主資料庫處理事務性增、改、刪操作(INSERT、UPDATE、DELETE),而從資料庫處理SELECT查詢操作。資料庫複製被用來把事務性操作導致的變更同步到叢集中的從資料庫。資料庫的“寫”(寫10000條資料到oracle可能要3分鐘)操作是比較耗時

MySQL Proxy實現分離

工作圖:   MySQL Proxy有一項強大功能是實現“讀寫分離”,基本原理是讓主資料庫處理寫方面事務,讓從庫處理SELECT查詢。 Amoeba for MySQL是一款優秀的中介軟體軟體,同樣可以實現讀寫分離,負載均衡等功能,並且穩定性也高於MySQL Proxy,

mysql-proxy實現分離

一:mysql主從資料庫的配置 要求:配置主 從複製: server2 主 172.25.28.2 server3 從 172.25.28.3 二:安裝配置mysql-proxy [root@server1 /]# ta

mysql-proxy 實現分離

實驗環境: 系統:reddhat6.5 mysql 版本:mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar mysql-proxy:mysql-proxy-0.8.4-linux-el6-x86-64bit.tar.gz 下載連結 http:

mysql+mysql_proxy實現分離

mysql-_proxymysql讀寫分離需要基於主從架構實現 mysql主從配置:http://hongchen99.blog.51cto.com/12534281/1917137 mysql-proxy:用於實現mysql主從分離,基於主從架構讀寫分離存在的最大問題就是主從同步延遲 安裝my

MySQL主從(MySQL proxy Lua分離設置,一主多從同步配置,分庫分表方案)

否則 count user username 2個 ons 基礎 zxvf 路徑 Mysql Proxy Lua讀寫分離設置一.讀寫分離說明讀寫分離(Read/Write Splitting),基本的原理是讓主數據庫處理事務性增、改、刪操作(INSERT、UPDATE、DE

mysql+mycat實現分離

-- sys prop lse 版本 text oss 機制 mys centos7 master slave mycat1.6 client 192.168.41.10 192.168.41.11 192.168.41.12 192.168.41.13

mysql proxy 資料庫分離字符集亂碼

  mysql proxy 資料庫讀寫分離字符集亂碼    解決辦法 在對應配置後端資料庫伺服器的配置.cnf中加入如下程式碼 init-connect='SET NAME UTF8' skip-character-set-client-handshake cha

mysql proxy搭建分離

rhel 6.10 主伺服器 :  192.168.2.121   rac01 從伺服器 :  192.168.2.122   rac02 排程伺服器: 192.168.2.123   rac03 --建立目錄,

MySQL - MyCat 實現分離

前言         MyCat是一個徹底開源的,面向企業應用開發的大資料庫叢集,支援事務、ACID、可以替代MySQL的加強版資料庫.其功能有可以視為MySQL叢集的企業級資料庫,用來替代昂貴的Oracle叢集.融合了記憶體快取技術、NoSQL技術、HDFS大資料的

mysql資料庫實現分離

1.mysql讀寫分離背景 在專案中使用mysql資料庫,所有的增刪改查操作都在主庫處理,隨著查詢訪問量的增加,單庫處理的壓力驟增,為了防止主庫故障,使用一主多從的方式,通過讀寫分離,把所有的查詢處理都放到從伺服器上,減少單點故障導致整個服務掛掉的情況。 2.mysql讀寫分離的實現

mycat+mysql叢集:實現分離,分庫分表

1.mycat文件:https://github.com/MyCATApache/Mycat-doc       官方網站:http://www.mycat.org.cn/2.mycat的優點:配置簡單,靈活可實現讀寫分離可利用多種規則實現分庫分表心跳機制,自動踢出故障機組

mysql jdbc 實現分離

這種方式直接在程式碼級別實現了mysql 讀寫分離 很簡單,只需要改一下配置檔案,就搞定了,是不是很嗨? jdbc.driverClassName=com.mysql.jdbc.ReplicationDriver jdbc.url=jdbc:mysql

Docker配置mysql主從實現分離

1. 獲取mysql映象獲取映象$ sudo docker pull mysql 檢視映象$ sudo docker images REPOSITORY TAG IMAGE ID CREATED

SpringBoot2.0.3+Mybatis+Mysql+druid實現分離+事務

       mysql支援一主多從,即在寫庫的資料庫發生變動時,會同步到所有從庫,只是同步過程中,會有一定的延遲(除非業務中出現,立即寫立即讀,否則稍微的延遲是可以接收的)。        當資料庫有主從之分了,那應用程式碼也應該讀寫分離了。這時候的事務就不像單個數據

MySQL+MyCat實現分離和主備熱切換

配置MyCat的schema.xml檔案 <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100"> <tab

mysql實現分離中介軟體MySQL Proxy

MySQL Proxy是一個處於你的client端和MySQL server端之間的簡單程式,它可以監測、分析或改變它們的通訊。它使用靈活,沒有限制,常見的用途包括:負載平衡,故障、查詢分析,查詢過濾和修改等等。 MySQL Proxy就是這麼一箇中間層代理,簡單的說,MySQL Proxy就是一個連線池,

Mysql主從配置,實現分離

windows安裝 建議 xid 分布式 唯一標識 -1 在線下載 命令 進行 大型網站為了軟解大量的並發訪問,除了在網站實現分布式負載均衡,遠遠不夠。到了數據業務層、數據訪問層,如果還是傳統的數據結構,或者只是單單靠一臺服務器扛,如此多的數據庫連接操作,數據庫必然會崩潰,

MySQL主從復制 + Mycat實現分離

date windows repl ron 信息 決定 不用 ati 刪掉 說明:兩臺MySQL服務器都是使用CentOS6.5系統,MySQL版本為mysql-5.7.17 MySQL一主一被實現主從復制 註意:寫包括insert,delete,update 操作;讀只有

使用Spring實現分離MySQL實現主從復制)

sign eve replicat win [] 做了 用戶名 指定 ati 1. 背景 我們一般應用對數據庫而言都是“讀多寫少”,也就說對數據庫讀取數據的壓力比較大,有一個思路就是說采用數據庫集群的方案, 其中一個是主庫,負責寫入數據,我們稱之為:寫庫; 其它都是從庫,