您好,欢迎访问一九零五行业门户网

使用Amoba 实现MySQL DB 读写分离

amoeba(变形虫)项目是一个开源框架,于2008年开始发布一款 amoeba for mysql软件;
这个软件致力于mysql的分布式数据库前端代理层,它主要在应用层访问mysql的时候充当sql路由功能,专注于分布式数据库代理层(database proxy)开发;位于 client、db server(s)之间,对客户端透明;
===================================================================
1 简介
2 准备
   2.1 时间同步
   2.2 配置mysql主从复制架构
3 ameoba安装配置
   3.1 安装配置jdk
   3.2 安装ameoba
   3.3 配置ameoba
   3.4 使用验证
   3.5 后期扩展
4 问题记录
===================================================================
1 简介
amoeba(变形虫)项目是一个开源框架,于2008年开始发布一款 amoeba for mysql软件;
这个软件致力于mysql的分布式数据库前端代理层,它主要在应用层访问mysql的时候充当sql路由功能,专注于分布式数据库代理层(database proxy)开发;位于 client、db server(s)之间,对客户端透明;
具有负载均衡、高可用性、sql 过滤、读写分离、可路由相关的请求到目标数据库、可并发请求多台数据库并合并结果;
通过amoeba你能够完成多数据源的高可用、负载均衡、数据切片的功能,目前amoeba已在很多企业的生产线上面使用;
2 准备
2.1 时间同步
# crontab -e# dscrip: time sync# ctime: 2014.03.23*/5 * * * * /usr/sbin/ntpdate 172.16.0.1 &>/dev/null
2.2 配置mysql主从复制架构
详见博文mariadb 主从复制
3 ameoba安装配置
3.1 安装配置jdk
chmod +x jdk-6u31-linux-x64-rpm.binvi /etc/profile.d/java.sh # 采用bin文件安装jdkexport java_home=/usr/java/latestexport path=$java_home/bin:$path
3.2 安装ameoba
mkdir /usr/local/amoebatar xf amoeba-mysql-binary-2.2.0.tar.gz -c /usr/local/amoeba # 使用二进制程序文件安装amoebacd /usr/local/amoebabin/amoeba start # 前台运行nohup /usr/local/amoeba/bin/amoeba start & # 后台运行mysql -h127.0.0.1 -uroot -p -p8066 # amoeba默认监听端口为8066
3.3 配置ameoba
cd /usr/local/amoeba/confvi ameoba.xml # 前端定义配置文件# 修改ameoba前端监听端口3306 # 默认端口是8066,修改为3306,便于实现前端程序连接数据库的透明性# 修改连接amoeba接口的认证信息rootmypass # 添加登录密码# 查询路由设置${amoeba.home}/conf/rule.xml${amoeba.home}/conf/rulefunctionmap.xml${amoeba.home}/conf/functionmap.xml1500master # 设定默认节点 master # 设定可写节点,节点定义见dbservers.xml文件 readservers # 设定只读池,可配置多个slave节点 truevi dbservers.xml # 后端节点配置文件# 定义抽象服务器,为每个后端mysql服务器提供默认连接配置${defaultmanager}641283406testrootmagedu # 定义后端mysql的ip地址,一个master,一个slave192.168.0.45192.168.0.46# 定义虚拟服务器组,即只读池readservers1master,slave
3.4 使用验证
在主库上授权:
mariadb [(none)]> grant all on *.* to'root'@'172.16.%.%' identified by 'magedu';query ok, 0 rows affected (0.00 sec)mariadb [(none)]> grant all on *.* to'root'@'%mysql.com' identified by 'magedu'; # 这里的密码应该与dbserver.xml中的数据库密码一致query ok, 0 rows affected (0.00 sec)mariadb [(none)]> flush privileges;query ok, 0 rows affected (0.00 sec)
# 登录验证[root@mysql conf]# mysql -h127.0.0.1 -uroot -p -p3306enter password:welcome to the mariadb monitor.commands end with ; or /g.your mysql connection id is 2097086015server version: 5.1.45-mysql-amoeba-proxy-2.2.0 source distributioncopyright (c) 2000, 2014, oracle, skysql ab and others.type 'help;' or '/h' for help. type '/c' to clear the current input statement.mysql [(none)]> show master status;+------------------+----------+--------------+------------------+| file | position | binlog_do_db | binlog_ignore_db |+------------------+----------+--------------+------------------+| mysql-bin.000030 |326 |||+------------------+----------+--------------+------------------+1 row in set (0.00 sec)mysql [(none)]># 读写验证[root@mysql conf]# mysql -h127.0.0.1 -uroot -p -p3306enter password:welcome to the mariadb monitor.commands end with ; or /g.your mysql connection id is 2097086015server version: 5.1.45-mysql-amoeba-proxy-2.2.0 source distributioncopyright (c) 2000, 2014, oracle, skysql ab and others.type 'help;' or '/h' for help. type '/c' to clear the current input statement.mysql [(none)]> create database amoeba_test;query ok, 1 row affected (0.04 sec)mysql [(none)]>[root@mysql bin]# mysql -h127.0.0.1 -uroot -p -p3406enter password:welcome to the mariadb monitor.commands end with ; or /g.your mariadb connection id is 33server version: 10.0.10-mariadb-log source distributioncopyright (c) 2000, 2014, oracle, skysql ab and others.type 'help;' or '/h' for help. type '/c' to clear the current input statement.mariadb [(none)]> show databases;+--------------------+| database |+--------------------+| amoeba_test|| information_schema || mysql|| performance_schema || test |+--------------------+9 rows in set (0.01 sec)mariadb [(none)]># 从amoeba接口登录创建数据库amoeba_test后,再从主库的接口中去查询数据库已创建,说明写入确实是落在了主库节点上;# 若要验证ameoba对于读操作的调度,则需要暂时停止从库的复制操作,然后在主库上更新数据,这样从ameoba读取数据将出现不一致的情况;
3.5 后期扩展
利用mmm双主复制架构+amoeba代理,可以实现对mysql的高可用性和高性能;
关于mmm的内容参加博文mysql scale out
4 问题记录
现象:使用mysql -uroot -p -p8066命令始终无法连接进入ameoba的配置接口,一直都是进入mysql数据库的配置接口
原因:在测试环境下,ameoba和mysql的主库都部署在同一台主机上,当启动ameoba服务后,即使指定-p8066连接,mysql客户端还是默认采用可被识别的socket文件(/tmp/mysql.sock)连接,同样指定-hlocalhost也是一样的;
当使用mysql命令连接mysqld时:
连接主机为localhost或不指定时,mysql会采用unix socket的连接方式;
连接主机为127.0.0.1时,mysql会采用tcp的方式连接;
解决方法:指定-h127.0.0.1连接即可,即mysql -h127.0.0.1 -uroot -p -p8066
其它类似信息

推荐信息