Mysql 中间件 oneProxy总结

Posted coder_up

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql 中间件 oneProxy总结相关的知识,希望对你有一定的参考价值。

建议使用之前把官方的文档全部通读一遍 这里提供一个我的网盘地址oneproxy 百度网盘

0. 先对 oneproxy 有个大概的了解,知道他所处的位置


1. mysql服务器

  1. 创建test用户,密码test
  2. 存在test数据库
  3. 确认oneproxy能登录

1.1 注意事项

  1. 所有MySQL节点有相同的用户名和口令
  2. 所有MySQL节点有相同的数据库名
  3. 所有MySQL节点使用相同的字符集设置
  4. 前端应用程序可以切换操作集群
    • 命令:use groupname
  5. 前端应用程序不能切换MySQL数据库

2. OneProxy 服务器

  1. 可以和Mysql共同一台机器
  2. 默认client port 3307
    1. mysql -utest -ptest -h 127.0.0.1 -P3307
  3. 默认admin port 4041
    1. mysql -uadmin -pOneProxy -h 127.0.0.1 -P4041

2.1 OneProxy 单机多实例

  1. OneProxy 单实例占用两个端口33074041,所有只要保证端口不重复, 就可以保证单机启动多个实例
  2. 多实例只要换不同端口即可

2.2 关闭OneProxy

  1. 后台运行
    1. –keepalive 启动守护进程
    2. 遇到内存Bug Crash 时候,可以自动重启,应用自动重新连接(开启keepalive后,会有一个单独的进程来监控oneproxy
  2. 杀死进程
    1. ps -ef | grep oneproxy
    2. kill -9 所有oneproxy 进程号
  3. 管理端口里面
    1. shutdown force

3. OneProxy 需要自己的验证机制

3.1 链接后端和client的配置

--proxy-user-list=username/password@dbname
--proxy-database: 指定默认数据库
--proxy-charset: 指定数据库字符集, 查看支持的字符集可以登陆到4041的命令窗口执行 list charset; 查看

这里的用户名和密码是提供oneproxy 链接后端mysql使用的,同时也是 client(php,java, python。。。)链接oneproxy 使用的

3.2 管理接口的用户名和密码配置

admin-user=  admin
admin-password=  OneProxy
admin-address=ip:port

密码必须加密,使用管理端口passwd命令生成
mysql -uadmin -pOneProxy -P4041 -c -h ip:port
> passwd test

4. 健康检查

  1. 每秒钟Proxy会和后端进行通信,检测后端数据库状态。
    1. Markup: 正常状态,可以提供服务
    2. Markdown:异常状态,不可以提供服务
  2. 查看状态
    1. 查看后端机器: list backend;
    2. 查看组:list group;
    3. 查看链接池: list pool;
    4. 查看日志文件:tail -100f oneproxy.log

5. 多个数据库

  1. OneProxy的use命令变成切换集群
  2. OneProxy不支持数据库切换
    1. 程序里不可以有use命令(不良习惯)
  3. 可以设定多个用户(不同的默认库)
    1. --proxy-user-list.1=user/pass@数据库名
    2. --proxy-user-list.2=user/pass@数据库名
  4. 如果不指定,则默认的数据库名为
    1. --proxy-database (默认为test)

6. 连接回收

  1. OneProxy 10秒钟检一次连接池。
  2. 自动回收5分钟不使用的连接。
  3. 要求数据库的超时设置大于300秒 + 10秒

    • 确认interactive_timeout设置 [mysqld]
    • 确认net_read_timeout设置
    • 确认net_write_timeout设置

7. 配置主从

proxy-master-addresses.1  = 192.168.2.190:3306@server1:2
proxy-master-addresses.2  = 192.168.2.190:3306@server1:1
proxy-slave-addresses.1  = 192.168.2.190:3306@server1:1
proxy-slave-addresses.2  = 192.168.2.190:3306@server1:1
proxy-slave-addresses.3  = 192.168.2.190:3306@server1:1

addresses.(1,2,3,4): 这代表指定多台主从,
server1:(1,2,3,4): 这个代表该节点的power, 可以理解为负载均衡中的weight值

8. 常用命令

COMMANDSUMMARYSAMPLE
LIST HELPDisplay the command list.list
LIST CHARSETDisplay the charset list.list charset
LIST POLICYDisplay the traffic policy for server group.list policy
LIST BACKENDDisplay info for all the backends.list backend
LIST SEQUENCEDisplay info for all the sequences.list sequence
LIST GROUPDisplay info for all the server groups.list group
LIST POOLDisplay info for all the connection pools.list pool
LIST THREADSDisplay statitics data for all the threads.list threads
LIST TABLEMAPDisplay the map of tables and server groups.list tablemap
LIST USERSDisplay the user list.list users
LIST SQLSTATSDisplay the sql statistics.list sqlstats
LIST TABSTATSDisplay the table statistics.list tabstats
LIST USERSTATSDisplay the user statistics.list userstats
LIST IPSTATSDisplay the IP based perfstats .list ipstats
LIST IPQOSDisplay the IP based request for last 10 seconds .list IPQOS
LIST DBQOSDisplay the backend request for last 10 seconds .list DBQOS
LIST IPTABSTATSDisplay the IP based table statistics.list iptabstats
LIST IPSQLSTATSDisplay the IP based SQL statistics.list ipsqlstats
LIST SQLTEXTDisplay the sql text.list sqltext
LIST DMLTEXTDisplay the sql text of dml operation.list dmltext
LIST NEWTEXTDisplay the sql text gathered by runtime.list newtext
LIST OLDTEXTDisplay the sql text loaded by firewall.list oldtext
LIST SQLFAILDisplay the failed sql in dry run.list sqlfail
LIST BLACKSQLDisplay the black sql text loaded by firewall.list blacksql
LIST TRANS_DEBUGDisplay the table map of transaction.list trans_debug
LIST TABLESDisplay the partition tables.list tables
LIST PARTITIONSDisplay the table partitions.list partitions
LIST OPTIONDisplay the current options.list option
SET MASTERSet a given backend as master backend.set master ‘192.168.1.119:3306’
SET SLAVESet a given backend as slave backend.set slave ‘192.168.1.119:3306’
SET OFFLINESet a given backend as offline backend.set offline ‘192.168.1.119:3306’
SET ONLINESet a given backend as online backend.set online ‘192.168.1.119:3306’
SET POWERSet the power/capacity number for a given backend.set power ‘192.168.1.119:3306’ [1-5]
SET DBQOSSet the QoS limit for a given backend.set power ‘192.168.1.119:3306’ num
SET GPOLICYSet a feature policy for given server group.set gpolicy default 1
SET GMASTERSet the master backend for given server group.set gmaster default 1
SET GACCESSSet the security level for given server group.set gaccess default 1
SET GDELAYSet the maximum slave delay for given server group.set gdelay default 1
SET GMANUALSet manual master mode for given server group.set gmanual default 1
SET POOLMINSet the min idle connections for given backend.set poolmin ‘192.168.1.119:3306’ 5
SET POOLMAXSet the max idle connections for given backend.set poolmax ‘192.168.1.119:3306’ 300
SET SQLSTATSEnable or disable the sql statistics feature.set sqlstats on
SET IPSTATSClear the ip statistics data.set ipstats clear
SET TABSTATSEnable or disable the table statistics feature.set tabstats on
SET USERSTATSEnable or disable the user statistics feature.set userstats on
SET TRANS_DEBUGEnable or disable the transaction debug mode.set trans_debug on
SET LOG_SQLERROREnable or disable the sql error log feature.set log_sqlerror on
SET SQL_DEBUGEnable or disable the sql debug mode.set sql_debug on
SET SECURITY_LEVELset the security level of the proxy.set security_level 0-maximum
SET SQL_FIREWALLEnable or disable the SQL Firewall feature.set sql_firewall on
SET DML_FIREWALLEnable or disable the DML only Firewall feature.set dml_firewall on
SET WATCH_MODEEnable or disable the Firewall watch mode.set watch_mode on
SET SQL_SIGNATUREEnable or disable the SQL signature feature.set sql_signature on
SET IP_FIREWALLEnable or disable the IP Firewall feature.set ip_firewall on
SET LOGIN_RESTRICTEnable or disable the login restrict mode.set login_restrict on
SET STRICT_POOLMAXEnable or disable the stricted pool max mode.set strict_poolmax on
SET UNSAFE_RESULTEnalbe or disable the unsafe result operation.set unsafe_group on
SET MULTI_INSERTSEnalbe or disable cross partition operation.set multi_inserts on
SET FORWARD_CLIENTIPComment the client ip into the SQL text.set forward_clientip on
SET BLACKIPDisable the access of specific ip address.set blackip ‘192.168.0.1’
SET GREENIPEnable the access of specific ip address.set greenip ‘192.168.0.1’
SET READ_ONLYPut a partition into read only mode.set read_only ‘my_range_0’
SET READ_WRITEPut a partition into writable mode.set read_write ‘my_range_0’
SET PARALLEL_DEGREESet the maximum degree for parallel query.set parallel_degree 8
SET PARALLEL_REQUESTSSet the maximum requests for parallel query.set parallel_requests 512
SET MAX_CACHE_ROWSSet the maximum rows for cache resultset.set max_cache_rows 8
SET MAX_IDLE_TIMESet the maximum idle time for pooled connection.set max_idle_time 60
ADD MASTERAdd a new master(rw) backend to oneproxy.add master ‘192.168.1.120:3306@default’
ADD SLAVEAdd a new slave(ro) backend to oneproxy.add slave ‘192.168.1.120:3306@default’
ADD SEQUENCEAdd a new sequence into oneproxy.add sequence ‘seq_name’
SAVE TABLESsave partition tables to a text file.save tables ‘/tmp/part.txt’
SAVE SQLsave sql list to a text file.save sql ‘/tmp/firewall.sql’
SAVE BLACKSQLsave black sql list to a text file.save blacksql ‘/tmp/blacksql.sql’
SAVE DMLsave dml sql list to a text file.save dml ‘/tmp/firewall_dml.sql’
SAVE NEWsave new sql list to a text file.save new ‘/tmp/firewall_new.sql’
SAVE IPsave ip list to a text file.save ip ‘/tmp/firewall.sql’
LOAD TABLESload partition tables from text file.load tables ‘/tmp/part.txt’
LOAD SQLload firewall sql from text file.load sql ‘/tmp/firewall.sql’
LOAD BLACKSQLload black sql from text file.load blacksql ‘/tmp/blacksql.sql’
LOAD IPload firewall ip list from text file.load ip ‘/tmp/firewall_ip.sql’
MAPMap a table name to a given server group.map my_test1_0 default
UNMAPRemove the server gruop for a given table.unmap my_test1_0
PASSWDEncrypt the password of command options.passwd test
SHUTDOWNShutdown the proxy.shutdown force

9. 开发中的注意事项 (这个是在网上找的,可供参考)

1、不支持 Server Side Cursor 接口,比如 mysql C API 里的 Prepare、Bind、 Execute调用接口

2、不支持 use命令切换数据库

3、默认禁止 CALL, PREPARE, EXECUTE, DEALLOCATE 命令,也就是说不能用存储过程

4、单库(单实例)分表–insert/update/delete要加字段名,如insert into t1(id,name) values(1,’张三’);

5、单库(单实例)分表–目前分了N张表,如果以自增id做关联查询,那么每张表的自增id都是从1开始,在与其他表join关联查询时,数据会不准确

6、单库(单实例)分表–当where条件有分区列时,值不能有函数转换,也不能有算术表达式,必须是原子值,否则结果不准确

7、分库分表(多实例)–不支持垮库join,例如user_0表在10.0.0.1机器里,现在要join关联查询10.0.0.2机器里的money_detail表,不支持

8、分库分表(多实例)–不支持分布式事务,例如user_0表在10.0.0.1机器里,user_1表在10.0.0.2机器里,现在想同时update更新两张表,不支持

9、读写分离 –默认读操作全部访问slave,如果想强制走主库,例如涉及金钱类的查询操作,SQL改为select /master/ from t1 where id=1;

10、分库分表/分表 –where条件带分区列时,直接命中该表,如果未带分区列,会逐一扫描所有分表(单线程),考虑性能问题,要加并行查询(多线程),SQL改为select /parallel/ from t1 where name=’李四’; 并行查询会增加额外的CPU消耗

11、分表规则:支持range(范围),hash(取模),hash规则要提前规划好,具体分多少张表,如前期分64张表,1年后想扩容128张表,数据需要重新导出导入,成本非常高,目前二级分表还不支持。

以上是关于Mysql 中间件 oneProxy总结的主要内容,如果未能解决你的问题,请参考以下文章

MySQL分库分表工具oneproxy安装说明

OneProxy : 面向商业数据库(Oracle/SQL Server)中间件即将面世

mysql中间件

OneProxy实现MySQL分库分表

OneProxy 管理

平民软件OneProxy的强大