资讯专栏INFORMATION COLUMN

OneProxy简单实现读写分离与分库分表

whinc / 1947人阅读

摘要:实现读写分离环境主机主机中间件一配置主从略注意关闭三端防火墙,也可以开放端口,数据库端口,连接端口,管理端口当配置同步之后需要在主机创建库并添加的连接用户会自动到数据库在数据库中设置项,只读模式,不会影响同步复制的功能,它可以限定普通用户进

OneProxy实现Mysql读写分离

环境:

master主机:192.168.1.10
slave主机:192.168.1.12
oneproxy中间件:192.168.1.11

一、配置主从(略)

①注意关闭三端防火墙,也可以开放端口,数据库端口3306,oneproxy连接端口3307,oneproxy管理端口4041

②当配置同步之后需要在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%’;

Variable_name Value
innodb_read_only OFF
read_only OFF
super_read_only OFF
tx_read_only OFF

mysql>set global read_only = 1;

二、配置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/local

②在预启动脚本中修改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.service

....
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子表
一、配置master主机

步骤:
①分配连接用户权限并创建数据库
mysql> grant all on . to oneproxy@"%" identified by "123.com";
mysql>create database test;

②关闭防火墙或开启端口
[root@192 ~]# systemctl stop firewalld

二、配置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文件有错误

三、验证

步骤:
①从客户端登录连接端口
[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实现了分离数据,验证成功

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/27268.html

相关文章

  • 数据库的分库分表

    摘要:如成千上万的蚂蚁完成一项搬运工作纵向扩展又叫垂直扩展,扩展一个点的能力支撑更大的请求。如利用个人的能力,如蜘蛛侠逼停火车参考分库分表的基本思想 一 概念:什么是分库分表(sharding) 1 将集中于单一节点的数据拆分并分别存储到多个数据库或表,称为分库分表 2 数据切分分为两种方式,垂直切分和水平切分 3 分库:因为表多导致数据过多使用垂直切分,垂直切分就是根据业务的耦合性,将关联...

    elarity 评论0 收藏0

发表评论

0条评论

whinc

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<