温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mysql+amoeba读写分离

发布时间:2020-05-26 20:04:27 来源:网络 阅读:797 作者:独一无二zz 栏目:MySQL数据库

mysql+amoeba读写分离
一 简介:
Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy。它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特定的数据库上执行。基于此可以实现负载均衡、读写分离、高可用性等需求。与MySQL官方的MySQL Proxy相比,作者强调的是amoeba配置的方便(基于XML的配置文件,用SQLJEP语法书写规则,比基于lua脚本的MySQL Proxy简单)。
Amoeba相当于一个SQL请求的路由器,目的是为负载均衡、读写分离、高可用性提供机制,而不是完全实现它们。用户需要结合使用MySQL的 Replication等机制来实现副本同步等功能。amoeba对底层数据库连接管理和路由实现也采用了可插拨的机制,第三方可以开发更高级的策略类来替代作者的实现。这个程序总体上比较符合KISS原则的思想。
优势
Amoeba主要解决以下问题:
a). 数据切分后复杂数据源整合
b). 提供数据切分规则并降低数据切分规则给数据库带来的影响
c). 降低数据库与客户端连接
d). 读写分离路由
不足
a)、目前还不支持事务
b)、暂时不支持存储过程(近期会支持)
c)、不适合从amoeba导数据的场景或者对大数据量查询的query并不合适(比如一次请求返回10w以上甚至更多数据的场合)
d)、暂时不支持分库分表,amoeba目前只做到分数据库实例,每个被切分的节点需要保持库表结构一致:
二 准备
1三台centos7系统(注意我是安装的minimal的系统,需要通yum安装一些依赖包如:,yum -y groupinstall development tools,现网建议安装DVD版)
Amoeba:192.168.161.141
Msysql master:192.168.161.142
Mysql slave:192.168.161.143
2 Amoeba上安装jdk-7u80-linux-x64.tar.gz、amoeba-mysql-3.0.5-RC-distribution.zip
3 Mysql上面安装MySQL-client-5.6.6_m9-1.rhel5.x86_64.rpm、
MySQL-server-5.6.6_m9-1.rhel5.x86_64.rpm
三 关闭防火墙和selinux
systemctl stop firewalld
systemctl disable firewalld

setenforce 0

四 在161.142和161.143上面安装mysql,并配置主从同步。
1 161.142和161.143上面都安装mysql
rpm -ivh MySQL-server-5.6.6_m9-1.rhel5.x86_64.rpm
rpm -ivh MySQL-client-5.6.6_m9-1.rhel5.x86_64.rpm
2修改配置文件
Mysql master(161.142):
[root@localhost home]# cat /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=1
binlog-ignore-db=information_schema #新增的配置,忽略information_schema 的同步

Mysql slave(161.143):
[root@localhost home]# cat /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=2

3 启动两台mysql,配置主从
(1)启动mysql:
systemctl start mysql
(2)修改mysql的默认密码:
mysqladmin -uroot password '123'
(3)进入161.142 mysql master:
[root@localhost home]# mysql -uroot -p123
mysql> show master status;
+------------------+----------+--------------+--------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------+-------------------+
| mysql-bin.000004 | 120 | | information_schema | |
+------------------+----------+--------------+--------------------+-------------------+
1 row in set (0.00 sec)
(4)进入161.143 mysql slave:
执行:
配置slave服务器
change master to master_host='192.168.161.142',master_user='root',master_password='123',
master_log_file='mysql-bin.000004',master_log_pos=120;
启动从服务器的复制功能:
start slave;
查看从服务器复制的状态:
mysql> show slave status\G'
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.161.142
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 120
Relay_Log_File: localhost-relay-bin.000009
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 120
Relay_Log_Space: 623
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: f2e02c4b-f6c3-11e7-a14e-66ab28c66abe
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
1 row in set (0.00 sec)
Slave_IO_Running: Yes,Slave_SQL_Running: Yes 两个都为也是则没有问题。

(5)测试:略
五 在161.141上面配置amoeba
1 161.141上面搭建Java环境
(1)解压Java包
tar -xzvf jdk-7u80-linux-x64.tar.gz
(2)配置环境变量
修改配置文件:
Vi /etc/profile

JAVA_HOME=/home/jdk1.7.0_80
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH

使配置文件立即生效:
source /etc/profile

验证Java:
[root@localhost home]# java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

2 配置amoeba配置文件
(1)下载
wget -c
https://ncu.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/3.x/amoeba-mysql-3.0.5-RC-distribution.zip

(2)解压
unzip amoeba-mysql-3.0.5-RC-distribution.zip -d /usr/local/amoeba
(如没有unzip命令请自行安装:yum install -y unzip zip)

(3)给amoeba授权mysql远程账户(不推荐使用root)
grant all on . to amoeba@"%" identified by "amoeba";
flush privileges;
(4)修改配置文件
Amoeba做读写分离只需要修改dbServers.xml、amoeba.xml 这两个配置文件

[root@localhost amoeba-mysql-3.0.5-RC]# cd /usr/local/amoeba/amoeba-mysql-3.0.5-RC/conf/
[root@localhost conf]# ls
access_list.conf amoeba.dtd amoeba.xml dbserver.dtd dbServers.xml function.dtd functionMap.xml log4j.dtd log4j.xml rule.dtd ruleFunctionMap.xml rule.xml

修改dbServers.xml
[root@localhost 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="abstractServer" abstractive="true">
    <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
        <property name="connectionManager">${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>#设置amoeba连接后端数据库服务器的账号和密码,因此需要在所有后端数据库上创建该用户,并授权amoeba服务器可连接

        <property name="password">amoeba</property>
    </factoryConfig>

    <poolConfig class="com.meidusa.toolkit.common.poolable.PoolableObjectPool">
        <property name="maxActive">500</property>
        <property name="maxIdle">500</property>
        <property name="minIdle">1</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="master"  parent="abstractServer">
    <factoryConfig>
        <!-- mysql ip -->
        <property name="ipAddress">192.168.161.142</property>
    </factoryConfig>
</dbServer>

<dbServer name="slave"  parent="abstractServer">
    <factoryConfig>
        <!-- mysql ip -->
        <property name="ipAddress">192.168.161.143</property>
    </factoryConfig>
</dbServer>

<dbServer name="multiPool" 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">slave</property>
    </poolConfig>
</dbServer>

</amoeba:dbServers>
[root@localhost conf]#

修改amoeba.xml
[root@localhost conf]# more 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.mysql.server.MySQLService">
        <!-- port -->
        <property name="port">8066</property>

        <!-- bind ipAddress -->
        <!-- 
        <property name="ipAddress">127.0.0.1</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="authenticateProvider">
            <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">

                <property name="user">amoeba</property>#提供客户端连接amoeba时需要使用这里设定的账号 (这里的账号密码和amoeba连接后端数据库服务器的密码无关)

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

                <property name="filter">
                    <bean class="com.meidusa.toolkit.net.authenticate.server.IPAccessController">
                        <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
                    </bean>
                </property>
            </bean>
        </property>

    </service>

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

        <!-- proxy server client process thread size -->
        <property name="executeThreadSize">128</property>

        <!-- per connection cache prepared statement size  -->
        <property name="statementCacheSize">500</property>

        <!-- default charset -->
        <property name="serverCharset">utf8</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="defaultManager" class="com.meidusa.toolkit.net.MultiConnectionManagerWrapper">
        <property name="subManagerClassName">com.meidusa.toolkit.net.AuthingableConnectionManager</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>
(5)启动amoeba
[root@server3 amoeba]# bin/launcher 
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. 
错误文字上看,应该是由于stack size太小,导致JVM启动失败,修改jvm.properties文件JVM_OPTIONS参数 
jvm.properties(/usr/local/amoeba/amoeba-mysql-3.0.5-RC)

        JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m"
改为
        JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k"
解决jdk7以上要求的启动xss参数。

[root@server3 amoeba]# netstat -unlpt | grep java ##查看监听的端口 
tcp 0 0 :::8066 :::* LISTEN 1506/java

启动/关闭amoeba
        最好先前台启动,检查没有错误之后再后台启动。
关闭
        # /usr/local/amoeba/amoeba-mysql-3.0.5-RC/bin/shutdown
启动
        # /usr/local/amoeba/amoeba-mysql-3.0.5-RC/bin/launcher
后台启动并把日志保存到/var/log/amoeba.log
        # /usr/local/amoeba/amoeba-mysql-3.0.5-RC/bin/launcher > /var/log/amoeba.log 2>&1 &

(6)注意:
dbServers.xml、amoeba.xml 配置文件中的账号密码都是mysql数据库中的账号,填写不对会出错:
ERROR 1000 (42S02): Access denied for user 'amoeba'@'192.168.161.142:57952'(using password: YES)

六测试:
1 在161.142上面连接amoeba
(1)关掉master 数据库161.142,测试是否只读
systemctl stop mysql
[root@localhost etc]# mysql -uamoeba -pamoeba -h292.168.161.141 -P8066
mysql> use test;
Database changed
mysql> select * from b ;
+------+------+
| sf | ff |
+------+------+
| 1 | 1 |
| 3 | 3 |
| 5 | 5 |
+------+------+
3 rows in set (0.01 sec)

mysql> insert into b values(5,5);
ERROR 1044 (42000): Amoeba could not connect to MySQL server[192.168.161.142:3306],Connection refused
mysql>

(2)关掉slave数据库161.143,测试是否只能写,不能读
mysql> use test;
Database changed
mysql> select * from b ;
ERROR 1044 (42000): Amoeba could not connect to MySQL server[192.168.161.143:3306],Connection refused
mysql> insert into b values(5,5);
Query OK, 1 row affected (0.03 sec)
七 借鉴网址
http://blog.csdn.net/oufua/article/details/77373851
http://blog.csdn.net/sds15732622190/article/details/69262632
http://blog.csdn.net/Mryiyi/article/details/73521861

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI