MySQL深入10-利用Ameoba实现读写分离

本篇博文主要介绍利用Ameoba实现MySQL的读写分离功能,而MySQL官方的mysql-proxy需要自己编写复杂的lua脚本才能实现同样的功能,故个人还是倾向使用amoeba哦;

===================================================================

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主从复制架构

详见博文”Maria10实现主从复制架构及SSL复制”

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前端监听端口<service><property>3306</property> # 默认端口是8066,修改为3306,便于实现前端程序连接数据库的透明性# 修改连接amoeba接口的认证信息<property><bean><property>root</property><property>mypass</property> # 添加登录密码# 查询路由设置<queryRouter><property><bean><property>${amoeba.home}/conf/rule.xml</property><property>${amoeba.home}/conf/ruleFunctionMap.xml</property></bean></property><property>${amoeba.home}/conf/functionMap.xml</property><property>1500</property><property>master</property> # 设定默认节点<property>master</property> # 设定可写节点,节点定义见dbServers.xml文件<property>readservers</property> # 设定只读池,可配置多个slave节点<property>true</property></queryRouter>vi dbServers.xml # 后端节点配置文件# 定义抽象服务器,为每个后端MySQL服务器提供默认连接配置<dbServer abstractive=”true”><factoryConfig><property>${defaultManager}</property><property>64</property><property>128</property><property>3406</property><property>test</property><property>root</property><property>magedu</property></factoryConfig># 定义后端MySQL的IP地址,一个master,一个slave<dbServer parent=”abstractServer”><factoryConfig><property>192.168.0.45</property></factoryConfig></dbServer><dbServer parent=”abstractServer”><factoryConfig><property>192.168.0.46</property></factoryConfig></dbServer># 定义虚拟服务器组,,即只读池readservers<dbServer virtual=”true”><poolConfig><property>1</property><property>master,slave</property></poolConfig></dbServer>

3.4 使用验证

# 登录验证[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的内容参加博文”2台主机极致实现双主复制架构及MMM”

4 问题记录

现象:使用mysql -uroot -p -P8066命令始终无法连接进入ameoba的配置接口,一直都是进入mysql数据库的配置接口

享受每一刻的感觉,欣赏每一处的风景,这就是人生。

MySQL深入10-利用Ameoba实现读写分离

相关文章:

你感兴趣的文章:

标签云: