首先说明一下amoeba 跟 MySQL proxy在读写分离的使用上面的区别:
在MySQL proxy 6.0版本 上面如果想要读写分离并且 读集群、写集群 机器比较多情况下,用mysql proxy 需要相当大的工作量,目前mysql proxy没有现成的 lua脚本。mysql proxy根本没有配置文件, lua脚本就是它的全部,当然lua是相当方便的。那么同样这种东西需要编写大量的脚本才能完成一 个复杂的配置。而Amoeba只需要进行相关的配置就可以满足需求。


假设有这样的使用场景,有三个数据库节点分别命名为Master、Slave1、Slave2如下:
Amoeba: Amoeba <192.168.14.129>
Master: Master <192.168.14.131> (可读写)
Slaves:Slave1 <192.168.14.132>、Slave2<192.168.14.133> (2个平等的数据库。只读/负载均衡)
在 主从数据库 的复制的部分, 任然需要使用数据库自己的复制机制。 Amoeba 不提供复制功能。
1. 起动数据库的主从复制功能。
a. 修改配置文件
master.cnf
Cnf代码
  1. server-id = 1 #主数据库标志
  2. # 增加 日志文件, 用于验证读写分离
  3. log = /home/mysql/mysql/log/mysql.log
server-id = 1 #主数据库标志
# 增加 日志文件, 用于验证读写分离
log = /home/mysql/mysql/log/mysql.log
slave1.cnf
Cnf代码
  1. server-id = 2
  2. # 增加 日志文件, 用于验证读写分离
  3. log = /home/mysql/mysql/log/mysql.log
server-id = 2
# 增加 日志文件, 用于验证读写分离
log = /home/mysql/mysql/log/mysql.log
slave2.cnf
Cnf代码
  1. server-id = 3
  2. # 增加 日志文件, 用于验证读写分离
  3. log = /home/mysql/mysql/log/mysql.log
server-id = 3
# 增加 日志文件, 用于验证读写分离
log = /home/mysql/mysql/log/mysql.log
b. Master 中 创建两个 只读权限 的用户。 用户名均为:repl_user 密码均为:copy 分别开放给 slave1, slave2
Sql代码
  1. mysql> grant replication slave on *.* to repl_user@192.168.14.132 identified by 'copy';
  2. mysql> grant replication slave on *.* to repl_user@192.168.14.133 identified by 'copy';
mysql> grant replication slave on *.* to repl_user@192.168.14.132 identified by 'copy';
mysql> grant replication slave on *.* to repl_user@192.168.14.133 identified by 'copy';
c. 查看 Master 信息
Sql代码
  1. mysql> show master status;
  2. +------------------+----------+--------------+------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  4. +------------------+----------+--------------+------------------+
  5. | mysql-bin.000017 | 2009 | | |
  6. +------------------+----------+--------------+------------------+
  7. 1 row in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000017 |     2009 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
d. Slave1 ,Slave2 中 启动 Master - Slave 复制功能。
分别执行以下命令
Sql代码
  1. mysql> slave stop;
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> change master to
  4. -> master_host='192.168.14.131',
  5. -> master_user='repl_user',
  6. -> master_password='copy',
  7. -> master_log_file='mysql-bin.000017',
  8. -> master_log_pos=2009;
  9. Query OK, 0 rows affected (0.03 sec)
  10. mysql> start slave;
  11. Query OK, 0 rows affected (0.00 sec)
mysql> slave stop;
Query OK, 0 rows affected (0.02 sec)
mysql> change master to
    -> master_host='192.168.14.131',
    -> master_user='repl_user',
    -> master_password='copy',
    -> master_log_file='mysql-bin.000017',
    -> master_log_pos=2009;
Query OK, 0 rows affected (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
2. Amoeba 读写分离的配置
a. Master , Slave1 ,Slave2 中开放权限给 Amoeba 访问。
在 Master , Slave1 , Slave2 中分别执行
Sql代码
  1. mysql> grant all on test.* to test_user@192.168.14.129 indentified by '1234';
mysql> grant all on test.* to test_user@192.168.14.129 indentified by '1234';
Amoeba 访问三个数据库的账号密码相同。
b. 修改 Amoeba 的配置文件
配置文件详细说明请查看 官方文档: http://docs.hexnova.com/amoeba/rw-splitting.html
dbServer.xml
Xml代码
  1. <?xml version="1.0" encoding="gbk"?>
  2. <!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">
  3. <amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">
  4. <!--
  5. Each dbServer needs to be configured into a Pool,
  6. If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration:
  7. add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig
  8. such as 'multiPool' dbServer
  9. -->
  10. <!-- 数据库连接配置的公共部分 -->
  11. <dbServer name="abstractServer" abstractive="true">
  12. <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
  13. <property name="manager">${defaultManager}</property>
  14. <property name="sendBufferSize">64</property>
  15. <property name="receiveBufferSize">128</property>
  16. <!-- mysql port -->
  17. <property name="port">3306</property>
  18. <!-- mysql schema -->
  19. <property name="schema">test</property>
  20. <!-- mysql user -->
  21. <property name="user">test_user</property>
  22. <!-- mysql password -->
  23. <property name="password">1234</property>
  24. </factoryConfig>
  25. <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
  26. <property name="maxActive">500</property>
  27. <property name="maxIdle">500</property>
  28. <property name="minIdle">10</property>
  29. <property name="minEvictableIdleTimeMillis">600000</property>
  30. <property name="timeBetweenEvictionRunsMillis">600000</property>
  31. <property name="testOnBorrow">true</property>
  32. <property name="testWhileIdle">true</property>
  33. </poolConfig>
  34. </dbServer>
  35. <!-- Master ,Slave1, Slave2 的独立部分,也就只有 IP 了 -->
  36. <dbServer name="master" parent="abstractServer">
  37. <factoryConfig>
  38. <!-- mysql ip -->
  39. <property name="ipAddress">192.168.14.131</property>
  40. </factoryConfig>
  41. </dbServer>
  42. <dbServer name="slave1" parent="abstractServer">
  43. <factoryConfig>
  44. <!-- mysql ip -->
  45. <property name="ipAddress">192.168.14.132</property>
  46. </factoryConfig>
  47. </dbServer>
  48. <dbServer name="slave2" parent="abstractServer">
  49. <factoryConfig>
  50. <!-- mysql ip -->
  51. <property name="ipAddress">192.168.14.133</property>
  52. </factoryConfig>
  53. </dbServer>
  54. <!-- 数据库池,虚拟服务器,实现读取的负载均衡 -->
  55. <dbServer name="slaves" virtual="true">
  56. <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
  57. <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
  58. <property name="loadbalance">1</property>
  59. <!-- Separated by commas,such as: server1,server2,server1 -->
  60. <property name="poolNames">slave1,slave2</property>
  61. </poolConfig>
  62. </dbServer>
  63. </amoeba:dbServers>
<?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="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">test_user</property>
			
			<!--  mysql password -->
			<property name="password">1234</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="testWhileIdle">true</property>
		</poolConfig>
	</dbServer>
	<!-- Master ,Slave1, Slave2 的独立部分,也就只有 IP 了 -->
	<dbServer name="master"  parent="abstractServer">
		<factoryConfig>
			<!-- mysql ip -->
			<property name="ipAddress">192.168.14.131</property>
		</factoryConfig>
	</dbServer>
	<dbServer name="slave1"  parent="abstractServer">
		<factoryConfig>
			<!-- mysql ip -->
			<property name="ipAddress">192.168.14.132</property>
		</factoryConfig>
	</dbServer>
	<dbServer name="slave2"  parent="abstractServer">
		<factoryConfig>
			<!-- mysql ip -->
			<property name="ipAddress">192.168.14.133</property>
		</factoryConfig>
	</dbServer>	
	
	<!-- 数据库池,虚拟服务器,实现读取的负载均衡 -->
	<dbServer name="slaves" 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">slave1,slave2</property>
		</poolConfig>
	</dbServer>
		
</amoeba:dbServers>
amoeba.xml
Xml代码
  1. <?xml version="1.0" encoding="gbk"?>
  2. <!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
  3. <amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">
  4. <proxy>
  5. <!-- service class must implements com.meidusa.amoeba.service.Service -->
  6. <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">
  7. <!-- Amoeba 端口号 -->
  8. <property name="port">8066</property>
  9. <!-- bind ipAddress -->
  10. <!--
  11. <property name="ipAddress">127.0.0.1</property>
  12. -->
  13. <property name="manager">${clientConnectioneManager}</property>
  14. <property name="connectionFactory">
  15. <bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">
  16. <property name="sendBufferSize">128</property>
  17. <property name="receiveBufferSize">64</property>
  18. </bean>
  19. </property>
  20. <property name="authenticator">
  21. <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
  22. <!-- Amoeba 账号,密码 -->
  23. <property name="user">root</property>
  24. <property name="password">root</property>
  25. <property name="filter">
  26. <bean class="com.meidusa.amoeba.server.IPAccessController">
  27. <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
  28. </bean>
  29. </property>
  30. </bean>
  31. </property>
  32. </service>
  33. <!-- server class must implements com.meidusa.amoeba.service.Service -->
  34. <service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer">
  35. <!-- port -->
  36. <!-- default value: random number
  37. <property name="port">9066</property>
  38. -->
  39. <!-- bind ipAddress -->
  40. <property name="ipAddress">127.0.0.1</property>
  41. <property name="daemon">true</property>
  42. <property name="manager">${clientConnectioneManager}</property>
  43. <property name="connectionFactory">
  44. <bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean>
  45. </property>
  46. </service>
  47. <runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">
  48. <!-- proxy server net IO Read thread size -->
  49. <property name="readThreadPoolSize">20</property>
  50. <!-- proxy server client process thread size -->
  51. <property name="clientSideThreadPoolSize">30</property>
  52. <!-- mysql server data packet process thread size -->
  53. <property name="serverSideThreadPoolSize">30</property>
  54. <!-- per connection cache prepared statement size -->
  55. <property name="statementCacheSize">500</property>
  56. <!-- query timeout( default: 60 second , TimeUnit:second) -->
  57. <property name="queryTimeout">60</property>
  58. </runtime>
  59. </proxy>
  60. <!--
  61. Each ConnectionManager will start as thread
  62. manager responsible for the Connection IO read , Death Detection
  63. -->
  64. <connectionManagerList>
  65. <connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
  66. <property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property>
  67. <!--
  68. default value is avaliable Processors
  69. <property name="processors">5</property>
  70. -->
  71. </connectionManager>
  72. <connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
  73. <property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>
  74. <!--
  75. default value is avaliable Processors
  76. <property name="processors">5</property>
  77. -->
  78. </connectionManager>
  79. </connectionManagerList>
  80. <!-- default using file loader -->
  81. <dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">
  82. <property name="configFile">${amoeba.home}/conf/dbServers.xml</property>
  83. </dbServerLoader>
  84. <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
  85. <property name="ruleLoader">
  86. <bean class="com.meidusa.amoeba.route.TableRuleFileLoader">
  87. <property name="ruleFile">${amoeba.home}/conf/rule.xml</property>
  88. <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>
  89. </bean>
  90. </property>
  91. <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
  92. <property name="LRUMapSize">1500</property>
  93. <!-- 默认数据库,主数据库 -->
  94. <property name="defaultPool">master</property>
  95. <!-- 写数据库 -->
  96. <property name="writePool">master</property>
  97. <!-- 读数据库,dbServer.xml 中配置的 虚拟数据库,数据库池 -->
  98. <property name="readPool">slaves</property>
  99. <property name="needParse">true</property>
  100. </queryRouter>
  101. </amoeba:configuration>