Amoeba(变形虫)项目,该开源框架于2008年 开始发布一款 Amoeba for Mysql软件。这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的 时候充当SQL路由功能,专注于分布式数据库代理层(Database Proxy)开发。座落与 Client、DB Server(s)之间,对客户端透明。具有负载均衡、高可用性、SQL 过滤、读写分离、可路由相关的到目标数据库、可并发请求多台数据库合并结果。 通过Amoeba你能够完成多数据源的高可用、负载均衡、数据切片的功能,目前Amoeba已在很多 企业的生产线上面使用。
实验拓扑图:
-----------------------------------------------------------------------------------------
实验环境:
centos-6.4
amoeba:172.16.8.10 | 192.168.100.254
主mysql:192.168.100.1
从mysql:192.168.100.2
-----------------------------------------------------------------------------------------
node1安装mysql数据库安装参考上篇博客:
http://gyulong.blog.51cto.com/6844383/1299852
1.node1安装完后编辑mysql配置文件
1.[root@node1 local]# vim /etc/my.cnf 添加如下内容 thread_concurrency = 8 datadir= /mydata/data 数据存放目录 binlog_format=ROW 二进制文件格式 log-slave-updates=true 开启二进制更新 gtid-mode=on 开启全局事务ID模块 enforce-gtid-consistency=true 强制GTID的一致性 master-info-repository=TABLE master信息的记录位子 relay-log-info-repository=TABLE 中继日志存放位置 sync-master-info=1 同步级别 slave-parallel-workers=2 从复制线程 binlog-checksum=CRC32 二进制的校验算法 master-verify-checksum=1 主服务器是否校验 slave-sql-verify-checksum=1 从服务器是否校验 binlog-rows-query-log_events=1 二进制请求日志 report-port=3306 端口 report-host=node1.yulong.com 主机 server-id=1 不要和从的ID一样了
2.node2上安装配置mysql配置文件
thread_concurrency = 8 datadir= /mydata/data 数据存放目录 binlog_format=ROW 二进制文件格式 log-slave-updates=true 开启二进制更新 gtid-mode=on 开启全局事务ID模块 enforce-gtid-consistency=true 强制GTID的一致性 master-info-repository=TABLE master信息的记录位子 relay-log-info-repository=TABLE 中继日志存放位置 sync-master-info=1 同步级别 slave-parallel-workers=2 从复制线程 binlog-checksum=CRC32 二进制的校验算法 master-verify-checksum=1 主服务器是否校验 slave-sql-verify-checksum=1 从服务器是否校验 binlog-rows-query-log_events=1 二进制请求日志 report-port=3306 端口 report-host=node1.yulong.com 主机 server-id=20 不要和从的ID一样了
3.在node1主服务器上授权复制用户
mysql> grant replication slave,replication client on *.* to 'dba'@192.168.100.2 identified by 'mypass'; mysql> flush privileges; 刷新下 Query OK, 0 rows affected (0.30 sec) mysql> SHOW GRANTS FOR 'dba'@192.168.100.2; 查看用户授权信息 +------------------------------------------------------------------------------------------------------------------------------------------------+ | Grants for [email protected] | +------------------------------------------------------------------------------------------------------------------------------------------------+ | GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'dba'@'192.168.100.2' IDENTIFIED BY PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4' | +------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) 在授权一个前端调度器amoeba访问用户,我们这里使用默认的root mysql> grant all on *.* to 'root'@192.168.100.254 identified by 'mypass';
4.在node2上启动从节点的复制线程
mysql> CHANGE MASTER TO MASTER_HOST='192.168.100.2', MASTER_USER='guo', MASTER_PASSWORD='mypass', MASTER_AUTO_POSITION=1; mysql> start slave; 启动从服务器 Query OK, 0 rows affected
5.查看从服务器的状态
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.100.1 Master_User: guo Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 2785 Relay_Log_File: node2-relay-bin.000002 Relay_Log_Pos: 2084 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes
6.查看主从数据是否同步
主的:mysql> show databases; 从的:mysql> show databases; +--------------------+ +--------------------+ | Database | | Database | +--------------------+ +--------------------+ | information_schema | | information_schema | | mysql | | mysql | | nihao | | nihao | | performance_schema | | performance_schema | | qq | | qq | | taobao | | taobao | | test | | test | +--------------------+ +--------------------+ 7 rows in set (0.03 sec) 7 rows in set (0.03 sec)
主从配置好后我们来配置调度器,通过amoeba来实现动静分离和负载均衡,前端节点amoeba
7.前端调度器创建amoeba目录
[root@amoeba ~]# mkdir -pv /usr/local/amoeba-mysql-2.2.0 [root@amoeba local]# ln -sv amoeba-mysql-2.2.0 amoeba
8.编辑amoeba的配置文件,添加链接数据库的dbserver
[root@amoeba conf]# cd /usr/local/amoeba/conf/ [root@amoeba conf]# ls access_list.conf amoeba.xml dbserver.dtd function.dtd log4j.dtd rule.dtd rule.xml amoeba.dtd backup dbServers.xml functionMap.xml log4j.xml ruleFunctionMap.xml [root@amoeba conf]# vim dbServers.xml <property name="port">3306</property> 修改这项为mysql的默认端口 <property name="password">mypass</property 修改这项为上面为amoeba授权的密码 <dbServer name="node1" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.100.1</property> </factoryConfig> </dbServer> <dbServer name="node2" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.100.2</property> </factoryConfig> </dbServer> <dbServer name="readPool" 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">node2,node2,node1</property> </poolConfig> </dbServer> </amoeba:dbServers> 需要修改的选项有如下内容: dbServer name:就是前端mysql的主机名 parent="abstractServer" 继承上面定义的全局选项,可以自定义修改 <property name="ipAddress">后端mysql的ip地址 <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA--> 负载均衡的调度算法 <property name="poolNames">node2,node2,node1</property> 定义一组dbserver组,这里修改为刚才定义的dbserver的名字
9.接下来配置amoeba的主配置文件
[root@amoeba conf]# vim amoeba.xml <property name="port">3306</property> 修改为默认3306 <property name="ipAddress">0.0.0.0</property> 监听的IP地址四个0表示所有 <property name="user">root</property> 添加一个前端的连接mysql用户 <property name="password">mypass</property> 添加一个密码,这里密码可以随意 <property name="defaultPool">node1</property> 默认连接的节点 <property name="writePool">node1</property> 定义mysql写入的节点 <property name="readPool">readPool</property 定义一组mysql只读的节点,readPool是在dbService中定义过的这里直接调用
10.添加环境变量
[root@amoeba ~]# vim /etc/profile.d/amoeba.sh export AMOEBA_HOME=/usr/local/amoeba export PATH=$AMOEBA_HOME/bin:$PATH
11.启动amoeba
root@amoeba conf]# amoeba startlog4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml 2013-08-26 22:54:24,513 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 2013-08-26 22:54:25,873 INFO net.ServerableConnectionManager - Amoeba for Mysql listening on /0.0.0.0:3306. 2013-08-26 22:54:25,885 INFO net.ServerableConnectionManager - Amoeba Monitor Server listening on /127.0.0.1:10536.
12.前端连接下mysql数据库
[root@amoeba ~]# mysql -uroot -h 172.16.8.10 -pmypass Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 561980460 Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 MySQL Community Server (GPL) Copyright (c) 2000, 2012, 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> 连接成功
13.在两个后端mysql服务器使用抓包工具看看前端发过来的读写是否发送到指定读写服务器
[root@node1 ~]# tcpdump -i eth0 -s0 -nn -A tcp dst port 3306 and dst host 192.168.100.1 [root@node2 ~]# tcpdump -i eth0 -s0 -nn -A tcp dst port 3306 and dst host 192.168.100.2
14.连接前端调度器mysql用抓包工具查看写的是不是送给主服务器和读的是否发送给读的服务器
[root@amoeba ~]# mysql -uroot -h 172.16.8.10 -pmypass mysql> select Host,Password from mysql.user; mysql> show databases; 读一个命令验证 mysql> create database mydb; 写一个命令验证 +--------------------+ | Database | +--------------------+ | information_schema | | hello | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.63 sec)
15.读的报文从服务器已经抓到,而主的没有,说明是成功的。
node2上的报文已经有了刚才上面的操作 #...8".....show databases ..)..W..&....select Host,Password from mysql.user 20:18:39.174347 IP 192.168.100.254.44669 > 192.168.100.1.3306: Flags [.], ack 294, win 490, options [nop,nop,TS val 34219364 ecr 30620072], length 0 E..4..@[email protected]..}...%s.#xS...../L..... . %d..9. node1主上没有任何查询的报文 17:43:51.548119 IP 192.168.100.254.37292 > 192.168.100.2.3306: Flags [R], seq 34364003, win 0, length 0 E..(..@[email protected]......... 17:43:51.612890 IP 192.168.100.254.37293 > 192.168.100.2.3306: Flags [R], seq 1437744405, win 0, length 0 E..(..@[email protected].=.....P...40........ 17:43:51.613014 IP 192.168.100.254.37294 > 192.168.100.2.3306: Flags [R], seq 764481052, win 0, length 0
16.刚才创建了一个mydb,查看是否发送主服务器
node1上的报文已经抓到 ..W...9......create database mydb 20:25:25.413189 IP 192.168.100.254.44669 > 192.168.100.1.3306: Flags [.], ack 305, win 490, options [nop,nop,TS val 34625602 ecr 31026359], length 0 E..4..@[email protected]..}...%s.#xS......,..... ..XB..l. node2上没有任何数据 17:53:31.769029 IP 192.168.100.254.39118 > 192.168.100.2.3306: Flags [P.], seq 0:67, ack 83, win 457, options [nop,nop,TS val 34769628 ecr 22421019], length 67 E..w'.@[email protected]....."....C......j...... .....V..?..........@!.......................root..x.....Y.....!.......test. 17:53:31.816663 IP 192.168.100.254.39118 > 192.168.100.2.3306: Flags [.], ack 94, win 457, options [nop,nop,TS val 34769677 ecr 22421022], length 0 E..4'.@[email protected]....."....C......X......
16.而且还实现了负载均衡,而且读写也分离了