實驗環境:java
MySQL amoeba:192.168.200.116node
MySQL-master:192.168.200.111mysql
MySQL-slave1:192.168.200.112linux
MySQL-slave2:192.168.200.113算法
MySQL client:192.168.200.100sql
先實現MySQL數據庫的主從複製,方法見上一篇。數據庫
1.安裝Amoeba上安裝java環境(推薦jdk1.5或1.6)vim
[root@localhost ~]# ls
amoeba-mysql-binary-2.2.0.tar.gz initial-setup-ks.cfg nohup.out
anaconda-ks.cfg jdk-6u14-linux-x64.bin yum.sh服務器
[root@localhost ~]# chmod +x jdk-6u14-linux-x64.bin負載均衡
[root@localhost ~]# jdk-6u14-linux-x64.bin
一路空格,輸入yes,回車完成
[root@localhost ~]# mv jdk1.6.0_14/ /usr/local/jdk1.6
[root@localhost ~]# vim /etc/profile
末行添加
export JAVA_HOME=/usr/local/jdk1.6
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export AMOEBA_HOME=/usr/local/amoeba/
export PATH=$PATH:$AMOEBA_HOME/bin
[root@localhost ~]# source /etc/profile
[root@localhost ~]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
[root@localhost ~]# rm -rf /usr/bin/java
[root@localhost ~]# source /etc/profile
[root@localhost ~]# java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)
備註:自帶高版本java問題
[root@localhost ~]# java -version
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)
方法一:卸載高版本
[root@localhost ~]# rpm -qa | grep -i openjdk
java-1.7.0-openjdk-1.7.0.171-2.6.13.2.el7.x86_64
java-1.7.0-openjdk-headless-1.7.0.171-2.6.13.2.el7.x86_64
java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64
java-1.8.0-openjdk-headless-1.8.0.161-2.b14.el7.x86_64
[root@localhost ~]# rpm -e java-1.7.0-openjdk-headless java-1.7.0-openjdk java-1.8.0-openjdk-headless java-1.8.0-openjdk –nodeps
方法二:刪除高版本java程序文件
[root@localhost ~]# which java
/usr/bin/java
[root@localhost ~]# /usr/bin/java -version
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)
[root@localhost ~]#
[root@localhost ~]# rm -rf /usr/bin/java
[root@localhost ~]# source /etc/profile
2.安裝配置Amoeba
[root@localhost ~]# mkdir /usr/local/amoeba
[root@localhost ~]# tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/
[root@localhost ~]# chmod -R 755 /usr/local/amoeba/
[root@localhost ~]# /usr/local/amoeba/bin/amoeba
amoeba start|stop //出現此行,證實配置無誤
3.配置amoeba實現mysql-slave1,slave2讀負載均衡
在三臺MySQL服務器上對amoeba受權
MariaDB [(none)]> grant all on *.* to 'admin'@'192.168.200.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
編寫amoeba.xml配置文件,設置讀寫分離
[root@localhost ~]# cd /usr/local/amoeba/conf/
[root@localhost conf]# cp amoeba.xml amoeba.xml.bak
[root@localhost conf]# vim amoeba.xml
11<property name="port">8066</property> //默認端口8066,無需修改
30<property name="user">amoeba</property> //設置一個用戶,後面客戶端訪問時使用
32<property name="password">123456</property> //設置登陸密碼
115<property name="defaultPool">master</property> //默認的服務器池
117<property name="writePool">master</property> //寫的服務器池
118<property name="readPool">slaves</property> //讀服務器池
編輯dbServers.xml配置文件,設置登陸MySQL服務器的用戶及密碼,MySQL服務器節點的IP地址,負載均衡算法
[root@localhost conf]# cp dbServers.xml dbServers.xml.bak
[root@localhost conf]# vim dbServers.xml
20<property name="port">3306</property> //設置MySQL鏈接端口,默認3306
26<property name="user">admin</property> //設置訪問MySQL服務器所用的用戶名
27<property name="password">123456</property> //設置訪問MySQL服務器所用的密碼
43<dbServer name="master" parent="abstractServer"> //設置MySQL服務器及IP地址
46<property name="ipAddress">192.168.200.111</property>
50<dbServer name="slave1" parent="abstractServer">
53<property name="ipAddress">192.168.200.112</property>
56<dbServer name="slave2" parent="abstractServer">
59<property name="ipAddress">192.168.200.113</property>
63<dbServer name="slaves" virtual="true"> //在amoeba.xml文件中設置的readPool
69<property name="poolNames">slave1,slave2</property> //前面定義的服務器節點名
[root@localhost conf]# nohup /usr/local/amoeba/bin/amoeba start &
[root@localhost ~]# vim /usr/local/amoeba/conf/dbServers.xml
[root@localhost ~]# netstat -anpt | grep java // 查看是否有8066端口
建立測試庫,測試表
MariaDB [(none)]> use crushlinux;
Database changed
MariaDB [crushlinux]> insert into test values(2,'slave1');
Query OK, 1 row affected (0.01 sec)
能夠在slave1和slave2上查看到該表
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| crushlinux |
| db_test |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.01 sec)
在slave1,slave2服務器上中止主從備份,並各自添加內容
MariaDB [crushlinux]> insert into test values(2,'slave1');
Query OK, 1 row affected (0.01 sec)
MariaDB [crushlinux]> insert into test values(3,'slave2');
Query OK, 1 row affected (0.01 sec)
在Client客戶機測試
[root@client ~]# yum -y install mairadb-devel mariadb
2和3輪詢出現
MySQL [(none)]> select * from crushlinux.test;
+------+--------+
| id | name |
+------+--------+
| 3 | slave2 |
+------+--------+
1 row in set (0.02 sec)
MySQL [(none)]> select * from crushlinux.test;
+------+--------+
| id | name |
+------+--------+
| 2 | slave1 |
+------+--------+
1 row in set (0.02 sec)
MySQL [(none)]> insert into crushlinux.test values(4,'client');
Query OK, 1 row affected (0.01 sec)
寫入數據後沒法查到,實現讀寫分離
MySQL [(none)]> select * from crushlinux.test;
+------+--------+
| id | name |
+------+--------+
| 2 | slave1 |
+------+--------+
1 row in set (0.01 sec)
MySQL [(none)]> select * from crushlinux.test;
+------+--------+
| id | name |
+------+--------+
| 3 | slave2 |
+------+--------+
1 row in set (0.02 sec)
開啓主從複製後,能夠查到寫入的內容
MySQL [(none)]> select * from crushlinux.test;
+------+--------+
| id | name |
+------+--------+
| 3 | slave2 |
| 1 | master |
| 4 | client |
+------+--------+
3 rows in set (0.01 sec)
MySQL [(none)]> select * from crushlinux.test;+------+--------+| id | name |+------+--------+| 2 | slave1 || 1 | master || 4 | client |+------+--------+3 rows in set (0.02 sec)