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
创新互联建站长期为上千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为睢阳企业提供专业的成都做网站、成都网站设计、成都外贸网站建设,睢阳网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。
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
${defaultManager}
64
128
3306
test
amoeba #设置amoeba连接后端数据库服务器的账号和密码,因此需要在所有后端数据库上创建该用户,并授权amoeba服务器可连接
amoeba
500
500
1
600000
600000
true
true
true
192.168.161.142
192.168.161.143
1
slave
[root@localhost conf]#
修改amoeba.xml
[root@localhost conf]# more amoeba.xml
8066
128
64
amoeba #提供客户端连接amoeba时需要使用这里设定的账号 (这里的账号密码和amoeba连接后端数据库服务器的密码无关)
amoeba
${amoeba.home}/conf/access_list.conf
128
500
utf8
60
com.meidusa.toolkit.net.AuthingableConnectionManager
${amoeba.home}/conf/dbServers.xml
${amoeba.home}/conf/rule.xml
${amoeba.home}/conf/ruleFunctionMap.xml
${amoeba.home}/conf/functionMap.xml
1500
master
master
slave #这两行原先的注释要去掉
true
(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
网站名称:mysql+amoeba读写分离
当前链接:http://lswzjz.com/article/jocips.html