一、实验环境
1、准备五台主机,其中三台已经实现了Mysql的主从复制(主从复制不再演示,从安装Amoeba开始),一台安装Amoeba,一台用作试验机
mysql-master:192.168.200.111
mysql-slave1:192.168.200.112
mysql-slave2:192.168.200.113
Amoeba:192.168.200.114
Client:192.168.200.115
2、所有主机关闭安全机制和防火墙。
[root@localhost ~]# systemctl stop firewalld
[root@localhost~]# iptables -F
[root@localhost~]# setenforce 0
二、在主机Amoeba上安装java环境(192.168.200.114)
1、上传需要的安装包
[root@amoeba ~]# ls
amoeba-mysql-binary-2.2.0.tar.gz jdk-6u14-linux-x64.bin
anaconda-ks.cfg
2、执行.bin文件(就相当于解压.tar包)
[root@amoeba ~]# chmod +x jdk-6u14-linux-x64.bin
[root@amoeba~]# ./jdk-6u14-linux-x64.bin
[root@amoeba~]# mv jdk1.6.0_14/ /usr/local/jdk1.6
3、设置开机启动项,添加内容
[root@amoeba ~]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.6 //声明一个全局变量JAVA_HOME,安装路径为/usr/local/jdk1.6
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib //更新类的搜索路径
export PATH=$PATH:$JAVA_HOME/lib:$JAVA_HOME/jre/bin:$HOME/bin
export AMOEBA_HOME=/usr/local/amoeba //amoeba的解压路径
export PATH=$PATH:$AMOEBA_HOME/bin //amoeba的命令
4、jdk安装完成后,java的版本与jdk的版本不同,删除高版本的java程序文件,重新执行一下/etc/profile
[root@amoeba ~]# java -version
openjdk version"1.8.0_222"OpenJDK Runtime Environment (build1.8.0_222-b10)
OpenJDK64-Bit Server VM (build 25.222-b10, mixed mode)
[root@amoeba~]# which java/usr/bin/java
[root@amoeba~]# rm -rf /usr/bin/java
[root@amoeba~]# source /etc/profile
[root@amoeba~]# java -version
java version"1.6.0_14"Java(TM) SE Runtime Environment (build1.6.0_14-b08)
Java HotSpot(TM)64-Bit Server VM (build 14.0-b16, mixed mode)
三、安装并配置Amoeba(192.168.200.114)
1、解压安装包
[root@amoeba ~]# mkdir /usr/local/amoeba
[root@amoeba~]# tar -xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/[root@amoeba~]# chmod -R 755 /usr/local/amoeba/
2、配置Amoeba的读写分离,两个Slave读负载均衡
在Master、Slave1、Slave2服务器中配置Amoeba的访问授权
[root@mysql-m ~]# mysql -uroot -p123123 //三台主机都要执行
MariaDB [(none)]> grant all on *.* to 'test'@'192.168.200.%' identified by '123123'; //对所有库下的所有表,授权的用户名为test,允许访问的网段为200网段,密码为123123.
Query OK,0 rows affected (0.01sec)
MariaDB [(none)]>flush privileges;
Query OK,0 rows affected (0.00 sec)
4、编辑amoeba.xml配置文件
[root@amoeba ~]# cd /usr/local/amoeba/[root@amoeba amoeba]# ls
benchmark changelogs.txt lib README.html
bin conf LICENSE.txt
[root@amoeba amoeba]# cd conf/[root@amoeba conf]# ls
access_list.conf dbServers.xml log4j.xml
amoeba.dtd function.dtd rule.dtd
amoeba.xml functionMap.xml ruleFunctionMap.xml
dbserver.dtd log4j.dtd rule.xml
[root@amoeba conf]# cp amoeba.xml amoeba.xml.bak
[root@amoeba conf]# vim amoeba.xml30 amoeba //客户端必须拿这个用户名和密码登录amoeba
32 123123
115 master //默认
118 master //写的池,交给master
119 slaves //读的池,交给slaves组
5、编辑dbServer.xml配置文件
[root@amoeba conf]# cp dbServers.xml dbServers.xml.bak
[root@amoeba~]# vim /usr/local/amoeba/conf/dbServers.xml19
20 3306
21
22
23 test
24
25
26 test
27
28
29 123123
45
46
47
48 192.168.200.111
49
50
51
52
53
54
55 192.168.200.112
56
57
58
59
60
61 192.168.200.113
62
63
66 //声明组
67
68
69 1 //组内成员的效果为轮询
70
71
72 slave1,slave2
73
74
6、配置无误后,启动Amoeba软件,默认端口为TCP协议8066
nohup:执行命令不依赖于任何终端
[root@amoeba ~]# nohup /usr/local/amoeba/bin/amoeba start & //将命令放到后台运行,不依赖于任何终端
[root@amoeba~]# netstat -lnpt //查看端口8066
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1366/sshd
tcp0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1632/master
tcp60 0 :::8066 :::* LISTEN 21943/java
tcp60 0 127.0.0.1:18195 :::* LISTEN 21943/java
tcp60 0 :::22 :::* LISTEN 1366/sshd
tcp60 0 ::1:25 :::* LISTEN 1632/master
四、client客户机访问测试(192.168.200.115)
安装mariadb
1、连接数据库测试
[root@client ~]# mysql -u amoeba -p123123 -h 192.168.200.114 -P 8066Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection idis 74675057Server version:5.1.45-mysql-amoeba-proxy-2.2.0MariaDB Server
Copyright (c)2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type'help;' or '\h' for help. Type '\c'to clear the current input statement.
MySQL [(none)]>
2、测试读写分离
master主机上
MariaDB [(none)]>create database db_test;
Query OK,1 row affected (0.00sec)
MariaDB [(none)]>use db_test;
Database changed
MariaDB [db_test]> create table student (id int(10),name varchar(10),address varchar(20));
Query OK,0 rows affected (0.01 sec)
建完库之后在两台slave机上查看,能看到新建的库说明主从复制没有问题
3、将两台slave主机上的主从服务给关掉
salve1:
MariaDB [(none)]>stop salve;-------------------------------------------------------------------------------slave2:
MariaDB [(none)]> stop salve;
编写测试
master:
MariaDB [db_test]> insert into student values('1','crushlinux','this_is_master');
Query OK,1 row affected (0.01sec)---------------------------------------------------------------------------slave1:
MariaDB [db_test]> insert into student values('2','crushlinux','this_is_slave1');
Query OK,1 row affected (0.00sec)--------------------------------------------------------------------------slave2:
Database changed
MariaDB [db_test]> insert into student values('3','crushlinux','this_is_slave2');
Query OK,1 row affected (0.01 sec)
从client客户机上访问,访问到的是两台salve机的轮询
MySQL [db_test]> select * fromstudent;+------+------------+----------------+
| id | name | address |
+------+------------+----------------+
| 2 | crushlinux | this_is_slave1 |
+------+------------+----------------+
1 row in set (0.00sec)
MySQL [db_test]> select * fromstudent;+------+------------+----------------+
| id | name | address |
+------+------------+----------------+
| 3 | crushlinux | this_is_slave2 |
+------+------------+----------------+
1 row in set (0.01 sec)
4、客户机上添加一个数据,访问,还是读到两台slave机上的
MySQL [db_test]> insert into student values('4','crushlinux','this_is_client');
Query OK,1 row affected (0.01sec)
MySQL [db_test]> select * fromstudent;+------+------------+----------------+
| id | name | address |
+------+------------+----------------+
| 2 | crushlinux | this_is_slave1 |
+------+------------+----------------+
1 row in set (0.01sec)
MySQL [db_test]> select * fromstudent;+------+------------+----------------+
| id | name | address |
+------+------------+----------------+
| 3 | crushlinux | this_is_slave2 |
+------+------------+----------------+
1 row in set (0.02 sec)
在client机上写数据时,写交给master主机来处理,两台slave主机没有变化,因为此时主从服务已经停了,所以两台slave机上不会访问到master主机上写入的内容
5、开启主从复制测试
slave1:
MariaDB [db_test]>start slave;
Query OK,0 rows affected (0.00sec)-----------------------------------------------------------------------slave2:
MariaDB [db_test]>start slave;
Query OK,0 rows affected (0.01sec)==========================================client:
MySQL [db_test]> select * fromstudent;+------+------------+----------------+
| id | name | address |
+------+------------+----------------+
| 3 | crushlinux | this_is_slave2 |
| 1 | crushlinux | this_is_master |
| 4 | crushlinux | this_is_client |
+------+------------+----------------+
3 rows in set (0.01sec)
MySQL [db_test]> select * fromstudent;+------+------------+----------------+
| id | name | address |
+------+------------+----------------+
| 2 | crushlinux | this_is_slave1 |
| 1 | crushlinux | this_is_master |
| 4 | crushlinux | this_is_client |
+------+------------+----------------+
3 rows in set (0.01 sec)
主从复制开启后,两台slave机上可以读取到master主机上写入的数据,client在访问测试的时候就能读到主机上的数据,因为两台slave机是轮询效果,所以能访问到1、2、4或1、3、4