启动#amoeba start (stop)
检查#ps aux | grep amoeba # netstat -lnp | grep java
3 .测试
测试之前先要保证amoeba-server有访问两个主从服务器test库的权限,在主从mysql上都执行:
grant all on test.* to zw@'192.168.1.%' identified by '123456';
#用户名密码要和前面配置的相同
flush privileges;
测试的时候和我们平时使用一样,amoeba-mysql对我们应用透明,就是个mysql的代理了!
登录mysql使用如下命令(用户名密码和上面配置的要一致):
mysql -u zw -p 123456 -h 192.168.3.69 -P 8066
登录上去后,为了测试读和写必须,先把mysql的主从复制停掉,才能更清楚地看出读写的服务器是哪台
实验结果显示:写只在主上进行,读在从上进行
测试步骤:
还没有停掉从同步之前,创建一个表:
create table t (id int(10) ,name varchar(10),address varchar(20));
在从上执行stop slave;
然后在主从上各插入一条不同数据(供测试读的时候用),
在主上插入:insert into t values('1','z','master');
在从上插入:insert into t values('2','z','slave');
接下来通过登录amoeba-mysql上来测试读写:
[root@Centos2 ~]# mysql -u zw -p -h192.168.3.69 -P 8066
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14556042
Server version: 5.1.45-mysql-amoeba-proxy-1.3.1-BETA Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use test;
Database changed
mysql> select * from zhang; ###第一次执行显示在主上读取的数据!
+------+-------+----------------+
| id | name | address |
+------+-------+----------------+
| 2 | z | slave |
+------+-------+----------------+
1 row in set (0.02 sec)
mysql> insert into t values('3','zz','test'); ###插入一条数据,然后查询
Query OK, 1 row affected (0.01 sec)
mysql> select * from t; ###从上还是没有插入,因为执行了stop slave,;
+------+-------+---------------+
| id | name | address |
+------+-------+---------------+
| 2 | z | slave |
+------+-------+---------------+
而在主上执行select * from t; ## 主上可以看见插入了 说明插入的话在主上执行了而从上不执行从而达到读写分离的效果
+------+-------+---------------+
| id | name | address |
+------+-------+---------------+
| 1 | z | slave |
+------+-------+---------------+
| 3 | zz | test |
+------+-------+---------------+
4.简单主从权重配置
两台数据库服务器,一台主,一台从,按照上面的配置查询都只能是在从上进行,而写又全部在主上进行,如果把配置改成下面:
<property name="poolNames">server1,server2</property>
<property name="poolNames">server1,server2,server2</property>