OneProxy簡單實現讀寫分離與分庫分表

OneProxy實現Mysql讀寫分離

環境:mysql

master主機:192.168.1.10
slave主機:192.168.1.12
oneproxy中間件:192.168.1.11linux

1、配置主從(略)

①注意關閉三端防火牆,也能夠開放端口,數據庫端口3306,oneproxy鏈接端口3307,oneproxy管理端口4041sql

②當配置同步以後須要在master主機建立test庫並添加oneproxy的鏈接用戶會自動replication到slave數據庫
mysql>create database test;
mysql>grant select,delete,insert,update,create on test.* to oneproxy@'%' identified by '123.com';數據庫

③在slave數據庫中設置read_only項,read_only=1只讀模式,不會影響slave同步複製的功能,它能夠限定普通用戶進行數據修改的操做,但不會限定具備super權限的用戶的數據修改操做,這裏開啓它讓oneproxy自動識別讀寫服務器
mysql>show variables like ‘%read_only%’;vim

Variable_name Value
innodb_read_only OFF
read_only OFF
super_read_only OFF
tx_read_only OFF

mysql>set global read_only = 1;安全

2、配置OneProxy中間件

步驟:
①安裝oneproxy
[root@192 ~ ]# wget http://www.onexsoft.com/softw...
[root@192 ~ ]# tar zxf oneproxy-rhel6-linux64-v6.2.0-ga.tar.gz -C /usr/localbash

②在預啓動腳本中修改oneproxy目錄
[root@192 ~ ]# vim /usr/local/oneproxy/demo.sh服務器

#/bin/bash
#
export ONEPROXY_HOME=/usr/local/oneproxy
ulimit -c unlimited

# valgrind --leak-check=full \
  ${ONEPROXY_HOME}/bin/oneproxy --defaults-file=${ONEPROXY_HOME}/conf/proxy.conf

③修改啓動腳本oneproxy目錄
[root@192 ~ ]# vim /usr/local/oneproxy/oneproxy.serviceide

....
ONEPROXY_HOME=/usr/local/oneproxy
ONEPROXY_SBIN="${ONEPROXY_HOME}/bin/oneproxy"
ONEPROXY_CONF="${ONEPROXY_HOME}/conf/proxy.conf"
ONEPROXY_PID="${ONEPROXY_HOME}/log/oneproxy.pid"
....

④獲取oneproxy加密後的密文密碼
[root@192 ~]# cd /usr/local/oneproxy/bin
[root@192 bin]# ls
mysqlpwd oneproxy
[root@192 bin]# ./mysqlpwd 123.com
7FB703DA3682A0CCC20168D44E8A7E92FE676A51加密

⑤修改oneproxy主配文件
[root@192 ~ ]# vim /usr/local/oneproxy/conf/proxy.conf

[oneproxy]
#proxy-license              = A2FF461456A67F28,D2F6A5AD70C9042D
keepalive = 1
event-threads = 4
proxy-group-policy = test:read-slave
log-file = log/oneproxy.log
pid-file = log/oneproxy.pid
lck-file = log/oneproxy.lck
proxy-auto-readonly = 1
proxy-forward-clientip = 1
proxy-trans-debug = 1
mysql-version = 5.7.18
proxy-master-addresses.1 = 192.168.1.10:3306@test
proxy-slave-addresses.2 = 192.168.1.12:3306@test
proxy-user-list = oneproxy/7FB703DA3682A0CCC20168D44E8A7E92FE676A51@test
proxy-part-template = conf/template.txt
proxy-part-tables.1      = conf/part.txt
proxy-part-tables.2      = conf/part2.txt
proxy-part-tables.3      = conf/cust1.txt
proxy-charset            = utf8_bin
proxy-secure-client = 127.0.0.1
proxy-httpserver = :8080
proxy-httptitle = OneProxy Monitor

上述中:

proxy-auto-readonly:自動切換讀寫角色
proxy-group-policy:預約義策略,對應真實要管理的數據庫
proxy-user-list:指明鏈接數據庫用戶密文密碼要與真實數據庫用戶密碼對應@符後指的是數據庫

⑥啓動oneproxy
[root@192 oneproxy]# chmod +x demo.sh
[root@192 oneproxy]# ./demo.sh
[root@192 oneproxy]# ./oneproxy.service start
Starting OneProxy ... [ OK ]
[root@192 conf]# ss -anpt | grep 3307
LISTEN 0 128 :3307 :* users:(("oneproxy",pid=3401,fd=11))
[root@192 conf]# ss -anpt | grep 4041
LISTEN 0 128 :4041 :* users:(("oneproxy",pid=3401,fd=13))

⑦訪問oneproxy管理網頁
輸入http://oneproxy_ip:8080
默認用戶名admin,密碼OneProxy
圖片描述

⑧驗證
使用客戶端登陸oneproxy鏈接端口3307
[root@192 ~]# mysql -uoneproxy -p123.com -h 192.168.1.11 -P3307
mysql> create table test.tb (id int);
ERROR 1044 (42000): Access denied due to security policy, DDL disabled or DML restricted!
因爲oneproxy爲了安全起見,初始靜止了DDl語句,須要去到4041管理端口打開底層數據庫權限
[root@192 ~]# mysql -uadmin -pOneProxy -P4041 -h 192.168.1.11
mysql> set gaccess test 0 ;
[root@192 ~]# mysql -uoneproxy -p123.com -h 192.168.1.11 -P3307
mysql> create table test.tb (id int);
mysql> insert into test.tb values(1);
mysql> insert into test.tb values(2);
.....多插入幾條
mysql> select * from test.tb;
.....多查看幾回
圖片描述
觀察管理網頁master和slave中query的變化,讀負載的增長與寫負載的增長,驗證成功

OneProxy實現Mysql分庫分表

環境:

master主機:192.168.1.12
oneproxy中間件:192.168.1.11

思路:

客戶端→oneproxy→master主機
          ↘       ↓
             test庫→user表→user_0子表
                          →user_1子表
                          →user_2子表
                          →user_3子表

1、配置master主機

步驟:
①分配鏈接用戶權限並建立數據庫
mysql> grant all on . to oneproxy@'%' identified by '123.com';
mysql>create database test;

②關閉防火牆或開啓端口
[root@192 ~]# systemctl stop firewalld

2、配置OneProxy中間件

安裝部署過程略

步驟:
①配置proxy.conf
[root@192 ~ ]# vim /usr/local/oneproxy/conf/proxy.conf

[oneproxy]
#proxy-license              = A2FF461456A67F28,D2F6A5AD70C9042D
keepalive = 1
event-threads = 4
proxy-group-policy.1 = test:master-only
proxy-group-security = test:0
log-file = log/oneproxy.log
pid-file = log/oneproxy.pid
lck-file = log/oneproxy.lck
proxy-forward-clientip = 1
proxy-trans-debug = 1
mysql-version = 5.7.18
proxy-master-addresses.1 = 192.168.1.12:3306@test
proxy-user-list = oneproxy/7FB703DA3682A0CCC20168D44E8A7E92FE676A51@test
proxy-part-template = conf/template.txt
proxy-part-tables.1      = conf/part.txt
proxy-part-tables.2      = conf/part2.txt
proxy-part-tables.3      = conf/cust1.txt
proxy-charset            = utf8_bin
proxy-httpserver = :8080
proxy-httptitle = OneProxy Monitor

②配置part2.txt

[
{
    "table" : "user",
    "pkey" : "id",
    "type" : "int",
    "method" : "hash",
    "partitions" :
      [
        { "suffix" : "_0", "group": "test" },
        { "suffix" : "_1", "group": "test" },
        { "suffix" : "_2", "group": "test" },
        { "suffix" : "_3", "group": "test"}
      ]
  }
]

③啓動oneproxy
[root@192 oneproxy]# sh demo.sh
[root@192 oneproxy]# ./oneproxy.service start
Starting OneProxy ... [ OK ]
[root@192 oneproxy]# ss -anpt | grep 3307
LISTEN 0 128 :3307 :* users:(("oneproxy",pid=37997,fd=11))
[root@192 oneproxy]# ss -anpt | grep 4041
LISTEN 0 128 :4041 :* users:(("oneproxy",pid=37997,fd=13))
若是端口查詢不到,請查看oneproxy日誌文件oneproxy.log,若是沒有日誌記錄,則可能主配或part文件有錯誤

3、驗證

步驟:
①從客戶端登陸鏈接端口
[root@192 ~]# mysql -h 192.168.1.11 -u oneproxy -p123.com -P3307
mysql>show databases;

Database
test

②插入數據
mysql>create table user(id int ,c1 int);
mysql>insert into user(id,c1) values (1,1);
mysql>insert into user(id,c1) values (2,2);
mysql>insert into user(id,c1) values (3,3);
mysql>insert into user(id,c1) values (4,4);
mysql>insert into user(id,c1) values (5,5);
mysql>select * from user;

id c1
4 4
1 1
5 5
2 2
3 3

③回到master主機查看存儲位置
[root@192 ~]# mysql
mysql> use test
mysql> show tables ;

Tables_in_test
user
user_0
user_1
user_2
user_3

mysql> select * from user_0;

id c1
4 4

1 row in set (0.00 sec)

mysql> select * from user_1;

id c1
1 1
5 5

2 rows in set (0.00 sec)

mysql> select * from user_2;

id c1
2 2

1 row in set (0.00 sec)

mysql> select * from user_3;

id c1
3 3

1 row in set (0.00 sec)

經過oneproxy實現了分離數據,驗證成功

相關文章
相關標籤/搜索