使用Amoeba让mysql主从服务器实现读写分离

环境:

  1. 一台CentOS6.5-x64最小化安装(ip:10.0.0.226)

软件:jdk-8u60-linux-x64.rpm, amoeba-mysql-binary-2.2.0.tar.gz

下载地址:链接: http://pan.baidu.com/s/1c04Wmys 密码: 4tqe

2.一台CentOS6.5-x64最小化安装(ip:10.0.0.222)

为了节省资源,本次使用的是mysql多实例。3306为主(master),3307为从(slave).

主从同步这里就不在说明了,创建好主从同步后要先停止从库同步。进入mysql后slave stop;

3.测试机(ip:10.0.0.111)

测试机应事先有装过mysql,不然登陆的amoeba服务器的时候会出现-bash: mysql: command not found.(如果不是这样的,还希望大侠能留下相关办法。谢谢!)

3306主库(master)创建用户,这个用户是用来给amoeba写数据时候使用。语句如下:

mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO ‘amoeba‘@‘10.0.0.%‘ IDENTIFIED BY ‘123456‘;

3307从库(slave)创建用户,这个用户是用来给amoeba查询数据时候使用。语句如下:

mysql> GRANT SELECT ON *.* TO ‘amoeba‘@‘10.0.0.%‘ IDENTIFIED BY ‘123456‘;

Amoeba安装和配置

安装:

[[email protected] ~]# rpm -ivh jdk-8u60-linux-x64.rpm

[[email protected] ~]# ln -s /usr/java/jdk1.8.0_60/ /usr/java/jdk1.8   #做个软连接,方便后面操作

[[email protected] ~]# vi /etc/profile

在最后面添加以下两行:

export JAVA_HOME=/usr/java/jdk1.8

export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

[[email protected] ~]# source /etc/profile    #使刚加入的两行生效

[[email protected] ~]# mkdir -p /usr/local/amoeba

[[email protected] ~]# cd /usr/local/amoeba

[[email protected] amoeba]# tar xf amoeba-mysql-binary-2.2.0.tar.gz

[[email protected] amoeba]# rm amoeba-mysql-binary-2.2.0.tar.gz

[[email protected] amoeba]# chmod 700 -R bin/

配置:

2.2.0和1.3.1有些不一样

1.3.1是只要修改/usr/local/amoeba/conf/amoeba.xml这个配置文件就可以的

2.2.0是把/usr/local/amoeba/conf/amoeba.xml这个配置文件分开了,一个是amoeba.xml,另一个是dbServers.xml

下面贴出配置,尽供参考:

dbServers.xml

[[email protected] conf]# cat dbServers.xml

<?xml version="1.0" encoding="gbk"?>

<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">

<amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">

<!--

Each dbServer needs to be configured into a Pool,

If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration:

add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig

such as ‘multiPool‘ dbServer

-->

<dbServer name="abstractServer1" 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>

<!-- mysql port -->

<property name="port">3306</property>

<!-- mysql schema -->

<property name="schema">test</property>

<!-- mysql user -->

<property name="user">amoeba</property>

<!--  mysql password -->

<property name="password">123456</property>

</factoryConfig>

<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">

<property name="maxActive">500</property>

<property name="maxIdle">500</property>

<property name="minIdle">10</property>

<property name="minEvictableIdleTimeMillis">600000</property>

<property name="timeBetweenEvictionRunsMillis">600000</property>

<property name="testOnBorrow">true</property>

<property name="testOnReturn">true</property>

<property name="testWhileIdle">true</property>

</poolConfig>

</dbServer>

<dbServer name="abstractServer2" 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>

<!-- mysql port -->

<property name="port">3307</property>

<!-- mysql schema -->

<property name="schema">test</property>

<!-- mysql user -->

<property name="user">amoeba</property>

<!--  mysql password -->

<property name="password">123456</property>

</factoryConfig>

<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">

<property name="maxActive">500</property>

<property name="maxIdle">500</property>

<property name="minIdle">10</property>

<property name="minEvictableIdleTimeMillis">600000</property>

<property name="timeBetweenEvictionRunsMillis">600000</property>

<property name="testOnBorrow">true</property>

<property name="testOnReturn">true</property>

<property name="testWhileIdle">true</property>

</poolConfig>

</dbServer>

<dbServer name="server1"  parent="abstractServer1">

<factoryConfig>

<!-- mysql ip -->

<property name="ipAddress">10.0.0.222</property>

</factoryConfig>

</dbServer>

<dbServer name="server2"  parent="abstractServer2">

<factoryConfig>

<!-- mysql ip -->

<property name="ipAddress">10.0.0.222</property>

</factoryConfig>

</dbServer>

<dbServer name="master" virtual="true">

<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">

<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->

<property name="loadbalance">1</property>

<!-- Separated by commas,such as: server1,server2,server1 -->

<property name="poolNames">server1</property>

</poolConfig>

</dbServer>

<dbServer name="slave" virtual="true">

<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">

<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->

<property name="loadbalance">1</property>

<!-- Separated by commas,such as: server1,server2,server1 -->

<property name="poolNames">server2</property>

</poolConfig>

</dbServer>

</amoeba:dbServers>

 amoeba.xml

[[email protected] conf]# cat amoeba.xml

<?xml version="1.0" encoding="gbk"?>

<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">

<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">

<proxy>

<!-- service class must implements com.meidusa.amoeba.service.Service -->

<service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">

<!-- port -->

<property name="port">8066</property>

<!-- bind ipAddress -->

<!--  -->

<property name="ipAddress">10.0.0.226</property>

<property name="manager">${clientConnectioneManager}</property>

<property name="connectionFactory">

<bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">

<property name="sendBufferSize">128</property>

<property name="receiveBufferSize">64</property>

</bean>

</property>

<property name="authenticator">

<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">

<property name="user">root</property>

<property name="password">123456</property>

<property name="filter">

<bean class="com.meidusa.amoeba.server.IPAccessController">

<property name="ipFile">${amoeba.home}/conf/access_list.conf</property>

</bean>

</property>

</bean>

</property>

</service>

<!-- server class must implements com.meidusa.amoeba.service.Service -->

<service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer">

<!-- port -->

<!--  default value: random number

<property name="port">9066</property>

-->

<!-- bind ipAddress -->

<property name="ipAddress">127.0.0.1</property>

<property name="daemon">true</property>

<property name="manager">${clientConnectioneManager}</property>

<property name="connectionFactory">

<bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean>

</property>

</service>

<runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">

<!-- proxy server net IO Read thread size -->

<property name="readThreadPoolSize">20</property>

<!-- proxy server client process thread size -->

<property name="clientSideThreadPoolSize">30</property>

<!-- mysql server data packet process thread size -->

<property name="serverSideThreadPoolSize">30</property>

<!-- per connection cache prepared statement size  -->

<property name="statementCacheSize">500</property>

<!-- query timeout( default: 60 second , TimeUnit:second) -->

<property name="queryTimeout">60</property>

</runtime>

</proxy>

<!--

Each ConnectionManager will start as thread

manager responsible for the Connection IO read , Death Detection

-->

<connectionManagerList>

<connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">

<property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property>

<!--

default value is avaliable Processors

<property name="processors">5</property>

-->

</connectionManager>

<connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">

<property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>

<!--

default value is avaliable Processors

<property name="processors">5</property>

-->

</connectionManager>

</connectionManagerList>

<!-- default using file loader -->

<dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">

<property name="configFile">${amoeba.home}/conf/dbServers.xml</property>

</dbServerLoader>

<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>

<property name="readPool">slave</property>

<property name="needParse">true</property>

</queryRouter>

</amoeba:configuration>

启动:

[[email protected] bin]# ./amoeba start

log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml

2015-10-20 21:12:18,932 INFO  context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-2.2.0

log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf

2015-10-20 21:12:19,593 INFO  net.ServerableConnectionManager - Amoeba for Mysql listening on /10.0.0.226:8066.

2015-10-20 21:12:19,608 INFO  net.ServerableConnectionManager - Amoeba Monitor Server listening on /10.0.0.226:17877.

[[email protected] bin]# netstat -lntp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      932/sshd

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1008/master

tcp        0      0 ::ffff:10.0.0.226:17877     :::*                        LISTEN      1673/java

tcp        0      0 :::22                       :::*                        LISTEN      932/sshd

tcp        0      0 ::1:25                      :::*                        LISTEN      1008/master

tcp        0      0 ::ffff:10.0.0.226:8066      :::*                        LISTEN      1673/java

看到以上相关数据,说明已经起动成功。

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

    验证:

    为了能明显看出效果,这里先暂停主从同步。使用命令slave stop结束。

先进入主库3306[master],查看测试数据库tudou的test表,内容如下

mysql> select * from test;

+--------+

| name   |

+--------+

| shanqi |

| shanqi |

+--------+

再进入从库3307[slave],查看测试数据库tudou的test表,内容如下

mysql> select * from test;

+--------+

| name   |

+--------+

| shanqi |

| shanqi |

| green  |

+--------+

这里已事先在从库插入一条主库没有的记录,方便接下来的测试。

 使用一台测试机(IP:10.0.0.111)

登陆

[[email protected] amoeba]# mysql -uroot -p123456 -P8066 -h10.0.0.226

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1093642553

Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| ccmate             |

| mysql              |

| performance_schema |

| test               |

| tudou              |

+--------------------+

6 rows in set (0.00 sec)

查询的时候,amoeba会自动去从库查询。

mysql> select * from tudou.test;

+--------+

| name   |

+--------+

| shanqi |

| shanqi |

| green  |

+--------+

3 rows in set (0.01 sec)

插入一条记录到测试表tudou.test,然后去主库[master]查询下

mysql> insert into tudou.test values("test");

Query OK, 1 row affected (0.04 sec)

然后去主库[master]查询下

mysql> select * from test;

+--------+

| name   |

+--------+

| shanqi |

| shanqi |

| test   |

+--------+

3 rows in set (0.00 sec)

到此说明amoeba读写分离成功。

问题:
        启动的时候会出现以下问题:

[[email protected] bin]# ./amoeba start

 The stack size specified is too small, Specify at least 228k

Error: Could not create the Java Virtual Machine.

Error: A fatal exception has occurred. Program will exit.

这里是amoeba 启动脚本判断时有个地方设置过小,需要修改下

[[email protected] bin]# vi amoeba

找到DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss128k"这一行,把128改成256即可

时间: 2024-10-15 07:16:37

使用Amoeba让mysql主从服务器实现读写分离的相关文章

【纯干货】Amoeba实现MySQL主从同步与读写分离

[纯干货]Amoeba实现MySQL主从同步与读写分离 一.简介 amoeba简介 Amoeba(变形虫)项目,该开源框架于2008年开始发布一款 Amoeba for Mysql软件.这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的 时候充当SQL路由功能,专注于分布式数据库代理层(Database Proxy)开发.座落与 Client.DB Server(s)之间,对客户端透明.具有负载均衡.高可用性.SQL 过滤.读写分离.可路由相关的到目标数据库.可并发

CentOS 7上部署Amoeba实现MySQL主从同步,读写分离,负载均衡高可用群集

实验描述 本实验需要四台主机安装mysql,一台部署Amoeba,一台主数据库服务器,两台从数据库服务器,还需一台主机做客户端访问测试,本实验结束可实现主数据库服务器与从数据库服务器之间数据同步,读写分离(客户机读从服务器的数据,写入的数据存储在主服务器,主服务器再同步给从服务器),负载均衡. 实验拓扑 实验环境 实验步骤 一.Master同步时间 1.Master安装ntp yum install ntp -y 2.修改ntp配置文件 vim /etc/ntp.conf #空白处插入如下内容

MySQL主从同步、读写分离配置步骤

现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用. 为了避免不必要的麻烦,主从服务器MySQL版本尽量保持一致; 环境:192.168.0.1 (Master) 192.168.0.2 (Slave) MySQL Version:Ver 14.14 Distrib 5.1.48, for pc-linux-gnu (i686) using readline 5.1 1.登录Master服务器,修改my.cnf,添加如下内容: server-id = 1 //数据库ID号,

MySQL主从同步、读写分离配置步骤、问题解决笔记

根据要求配置MySQL主从备份.读写分离,结合网上的文档,对搭建的步骤和出现的问题以及解决的过程做了如下笔记:       现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用.       为了避免不必要的麻烦,主从服务器MySQL版本尽量保持一致; 环境:192.168.0.1 (Master)           192.168.0.2 (Slave) MySQL Version:Ver 14.14 Distrib 5.1.48, for pc-linux-gnu (i6

mysql主从同步、读写分离

一:所需服务器 服务器名称 运行服务 服务端口 Ip地址 T1 Nginx 80 192.168.11.158 负载均衡 T2 Nginx/httpd 80,873 192.168.11.157 实际网站服务 T3 Nginx/httpd 80,873 192.168.11.161 实际网站服务 T4 Nginx/httpd 80,873 192.168.11.162 实际网站服务 T5 Mysql 8066 192.168.11.163 Mysql读写分离 T6 mysql 3306 192.

MySQL主从复制技术与读写分离技术amoeba应用

前言:眼下在搭建一个人才站点,估计流量会非常大,须要用到分布式数据库技术,MySQL的主从复制+读写分离技术.读写分离技术有官方的MySQL-proxy,阿里巴巴的Amoeba.Amoeba能在阿里巴巴这么大流量的平台投入使用并且执行稳定,Amoeba的性能是非常优越的.相信眼前事实,所以选择了Amoeba. 一.名词解析 1. 主从复制. 将主server上的数据拷贝到从server上,保护数据免受意外的损失. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5u

mysql主从同步+mycat读写分离+.NET程序连接mycat代理

背景 最近新项目需要用到mysql数据库,并且由于数据量大的原因,故打算采用1主1从(主数据库负责增.删.改操作:从数据库负责查操作)的数据库架构,在实现主从之后还要实现读写分离的代理,在网上搜寻了很久,自己也了解了一些资料,目前有三种代理组件:1.mysql_proxy   2.amoeba   3.mycat   我采用了第3种,选择mycat的原因下面会提到. 部署环境 我在我虚拟机下面装了三台Centos7_x64系统: 1.192.168.8.47 主mysql服务器 2.192.16

mysql主从配置,读写分离

Mysql主从配置,实现读写分离 大型网站为了软解大量的并发访问,除了在网站实现分布式负载均衡,远远不够.到了数据业务层.数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器扛,如此多的数据库连接操作,数据库必然会崩溃,数据丢失的话,后果更是 不堪设想.这时候,我们会考虑如何减少数据库的联接,一方面采用优秀的代码框架,进行代码的优化,采用优秀的数据缓存技术如:memcached,如果资金丰厚的话,必然会想到假设服务器群,来分担主数据库的压力.Ok切入今天微博主题,利用MySQL主从配置,

DBA成长之路---mysql主从同步,读写分离

主从同步 什么是主从同步: 让其他的数据库服务器自动同步正在提供服务的数据库服务器上 的数据. 1 添加授权用户 mysql> grant replication slave on *.* to [email protected]"192.168.4.4" identified by '123456'; 2启用binlog日志 [[email protected] ~]# vim /etc/my.cnf [mysqld] server_id=3 log-bin=master11