Heim  >  Artikel  >  Datenbank  >  使用Amoba 实现MySQL DB 读写分离

使用Amoba 实现MySQL DB 读写分离

WBOY
WBOYOriginal
2016-06-01 13:13:501164Durchsuche

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前端监听端口<service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">	<property name="port">3306</property> # 默认端口是8066,修改为3306,便于实现前端程序连接数据库的透明性# 修改连接amoeba接口的认证信息<property name="authenticator">	<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator"> 		<property name="user">root</property> 		<property name="password">mypass</property> # 添加登录密码# 查询路由设置<queryrouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">	<property name="ruleLoader">	 		<bean class="com.meidusa.amoeba.route.TableRuleFileLoader">			<property name="ruleFile">${amoeba.home}/conf/rule.xml</property>			<property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>		</bean>	</property>	<property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>	<property name="LRUMapSize">1500</property>	<property name="defaultPool">master</property> # 设定默认节点	<property name="writePool">master</property>	# 设定可写节点,节点定义见dbServers.xml文件	<property name="readPool">readservers</property> # 设定只读池,可配置多个slave节点	<property name="needParse">true</property></queryrouter>vi dbServers.xml # 后端节点配置文件# 定义抽象服务器,为每个后端MySQL服务器提供默认连接配置<dbserver name="abstractServer" abstractive="true">	<factoryconfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">		<property name="manager">${defaultManager}</property>		<property name="sendBufferSize">64</property>		<property name="receiveBufferSize">128</property>		<property name="port">3406</property>		<property name="schema">test</property>		<property name="user">root</property>		<strong><property name="password">magedu</property></strong>	</factoryconfig># 定义后端MySQL的IP地址,一个master,一个slave<dbserver name="master" parent="abstractServer">	<factoryconfig>		<property name="ipAddress">192.168.0.45</property>	</factoryconfig></dbserver><dbserver name="slave" parent="abstractServer">	<factoryconfig>		<property name="ipAddress">192.168.0.46</property>	</factoryconfig></dbserver># 定义虚拟服务器组,即只读池readservers<dbserver name="readservers" virtual="true">	<poolconfig class="com.meidusa.amoeba.server.MultipleServerPool">		<property name="loadbalance">1</property>		<property name="poolNames">master,slave</property>	</poolconfig></dbserver></dbserver></bean></property></service>

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn