环境:参看“http://469952080.blog.51cto.com/8311493/1694613”
mysql写服务器A:192.168.1.121
mysql读服务器B:192.168.1.124
Amoeba服务器C:192.168.1.120
1、保证服务器A和服务器B的mysql服务器可以正常的运行
2、服务器A和服务器B分别授权给Amoeba可以访问其对应数据库
3、安装Amoeba
1)java环境安装
Amoeba框架是基于Java SE1.5开发的,建议使用Java SE 1.5版本。目前Amoeba经验证在JavaTM SE 1.5和Java SE 1.6能正常运行。
Java SE 1.6下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u32-downloads-1594644.html
下载完成后执行sh jdk-6u32-linux-i586-rpm.bin开始安装,将会安装到/usr/java/jdk1.6.0_32目录。
4、Amoeba安装
mkdir /usr/local/amoeba
tar xzf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba
配置用户环境变量
vi ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/amoeba/bin
JAVA_HOME=/usr/java/jdk1.6.0_32
export JAVA_HOME
export PATH
source ~/.bash_profile #设置的变量立即生效
5、Amoeba for mysql配置(两台服务器的设置)
配置Amoeba for mysql的读写分离主要涉及两个文件:
1)/usr/local/amoeba/conf/dbServers.xml
#设置连接后台数据库信息
<dbServer name="abstractServer1" abstractive="true">
<factoryConfig>
<!-- mysql port -->
<property name="port">3306</property> #连接后端mysql服务器端口
<!-- mysql schema -->
<property name="schema">jiang</property> #连接的数据库
<!-- mysql user -->
<property name="user">proxyuser</property> #连接的用户
<!-- mysql password -->
<property name="password">jiang</property> #连接的密码
</factoryConfig>
</dbServer>
<dbServer name="abstractServer2" abstractive="true"> #可以根据实际情况添加<dbServer>
<factoryConfig>
<!-- mysql port -->
<property name="port">3306</property>
<!-- mysql schema -->
<property name="schema">jiang</property>
<!-- mysql user -->
<property name="user">proxyuser</property>
<!-- mysql password -->
<property name="password">jiang</property>
</factoryConfig>
</dbServer>
#连接的数据库IP地址
<dbServer name="server1" parent="abstractServer1">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.1.121</property>
</factoryConfig>
</dbServer>
<dbServer name="server2" parent="abstractServer2">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.1.124</property>
</factoryConfig>
</dbServer>
#定义dbpool,可以保护多个数据库,方便在设置读写的数据
<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>
2)/usr/local/amoeba/conf/amoeba.xml
找到入下几项进行修改
<property name="port">8806</property> #设置amobea登陆端口
<property name="user">root</property> #登陆的用户名
<property name="password">jiang</property> #登陆密码
修改如下几项,设置读写数据库,数据下的参数设置,与dbServers.xml有关
<property name="defaultPool">master</property>
<property name="writePool">master</property>
<property name="readPool">slave</property>
6、测试使用的数据库为jiang,其内容如下
192.168.1.121数据库信息
mysql> use jiang;
mysql> select * from personal_info;
+-------+---------------+--------------+----------+---------------------+
| pi_id | pi_name | pi_tel | pi_qq | pi_email |
+-------+---------------+--------------+----------+---------------------+
| 1 | 192.168.1.121 | 1586544556 | 42423423 | [email protected] |
| 2 | mysqlproxy | 111111111111 | 22222222 | [email protected] |
+-------+---------------+--------------+----------+---------------------+
192.168.1.124数据库信息
mysql> use jiang;
mysql> select * from personal_info;
+-------+---------------+------------+----------+--------------------+
| pi_id | pi_name | pi_tel | pi_qq | pi_email |
+-------+---------------+------------+----------+--------------------+
| 1 | 192.168.1.124 | 1586544556 | 42423423 | [email protected] |
+-------+---------------+------------+----------+--------------------+
执行远程连接
[root@amoeba bin]# ./amoeba start & #启动amoeba
mysql -uroot -pjiang -h 192.168.1.120 -P 8806 #远程连接amoeba
#查询数据库信息
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| jiang |
| test |
+--------------------+
mysql> use jiang ;
mysql> select * from personal_info; #读取的数据为192.168.1.124上的数据
+-------+---------------+------------+----------+--------------------+
| pi_id | pi_name | pi_tel | pi_qq | pi_email |
+-------+---------------+------------+----------+--------------------+
| 1 | 192.168.1.124 | 1586544556 | 42423423 | [email protected] |
+-------+---------------+------------+----------+--------------------+
接下来,插入数据
mysql>insert into personal_info values(3,'amoeba','amoebatest',44333333,'[email protected]');
再次查看,还是没有插入的数据,其原因在于select执行的是读操作,所以一直会读取192.168.1.124上的数据,而写数据(insert,update,delete)都在192.168.1.121上。所以查看不了数据
mysql> use jiang;
mysql> select * from personal_info;
+-------+---------------+------------+----------+--------------------+
| pi_id | pi_name | pi_tel | pi_qq | pi_email |
+-------+---------------+------------+----------+--------------------+
| 1 | 192.168.1.124 | 1586544556 | 42423423 | [email protected] |
+-------+---------------+------------+----------+--------------------+
接下来,直接登录192.168.1.121查看数据是否发生了变化。如下所示,就可以看见插入的数据。
mysql> use jiang;
mysql> select * from personal_info;
+-------+---------------+--------------+----------+---------------------+
| pi_id | pi_name | pi_tel | pi_qq | pi_email |
+-------+---------------+--------------+----------+---------------------+
| 1 | 192.168.1.121 | 1586544556 | 42423423 | [email protected] |
| 2 | mysqlproxy | 111111111111 | 22222222 | [email protected] |
| 3 | amoeba | amoebatest | 44333333 | [email protected] |
+-------+---------------+--------------+----------+---------------------+
总结:如上所示,就完成了读写分离,那么在现实环境中,肯定要保障两台数据库的数据同步。