1、MySQL 基於Amoeba實現讀寫分離java
一、在主機Amoeba上安裝java環境mysql
114:將下面兩個rz進來linux
[root@localhost ~]# chmod +x jdk-6u14-linux-x64.binsql
[root@localhost ~]# ./jdk-6u14-linux-x64.binvim
[root@localhost ~]# ls
amoeba-mysql-binary-2.2.0.tar.gz jdk1.6.0_14 公共 視頻 文檔 音樂
anaconda-ks.cfg jdk-6u14-linux-x64.bin 模板 圖片 下載 桌面
[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/lib:$JAVA_HOME/jre/bin:$HOME/binssh
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$PATH:$AMOEBA_HOME/bintcp
[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)ide
二、解壓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
三、配置Amoeba讀寫分離,兩個Slave讀負載均衡
(1)在1十一、1十二、113服務器中配置Amoeba的訪問受權
MariaDB [(none)]> grant all on *.* to 'test'@'192.168.200.%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
(2)114:編輯amoeba.xml配置文件
[root@localhost ~]# cd /usr/local/amoeba/conf/
[root@localhost conf]# cp amoeba.xml amoeba.xml.bak
[root@localhost conf]# vim amoeba.xml
<property name="user">amoeba</property>
<property name="password">123</property>
<property name="defaultPool">master</property>
<property name="writePool">master</property>
<property name="readPool">slaves</property>
(3)編輯dbServers.xml配置文件
[root@localhost conf]# cp dbServers.xml dbServers.xml.bak
[root@localhost conf]# vim dbServers.xml
<!-- mysql user -->
<property name="user">test</property>
<!-- mysql password --!>
<property name="password">123</property>
四、配置無誤以後,啓動Amoeba軟件,默認端口是TCP協議8066
[root@localhost conf]# nohup /usr/local/amoeba/bin/amoeba start &
[root@localhost ~]# netstat -lnpt //查看一下有沒有8066端口
五、*若是沒有執行如下操做:
[root@localhost conf]# ps aux | grep amoeba
root 39406 0.3 1.9 1070924 40536 pts/0 Sl 16:28 0:04 /usr/local/jdk1.6/bin/java -server -Xms256m -Xmx256m -Xss128k -Damoeba.home=/usr/local/amoeba -Dclassworlds.conf=/usr/local/amoeba/bin/amoeba.classworlds -classpath /usr/local/amoeba/lib/classworlds-1.0.jar org.codehaus.classworlds.Launcher start
root 39859 0.0 0.0 112656 972 pts/0 R+ 16:49 0:00 grep --color=auto amoeba
[root@localhost conf]# kill -9 39406
[root@localhost conf]# ps aux | grep amoeba
root 39869 0.0 0.0 112656 972 pts/0 R+ 16:49 0:00 grep --color=auto amoeba
[1]+ 已殺死 nohup /usr/local/amoeba/bin/amoeba start
[root@localhost conf]# /usr/local/amoeba/bin/amoeba start
再複製一個ssh渠道:[root@localhost ~]# netstat -lnpt
[root@localhost ~]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1455/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2124/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1688/master
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 37882/sshd: root@pt
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 39696/sshd: root@pt
tcp6 0 0 :::8066 :::* LISTEN 39957/java // 已經有了
[root@localhost conf]# nohup /usr/local/amoeba/bin/amoeba start &
[root@localhost conf]# cat nohup.out //可能反應有點慢,可是會出現8066的端口
六、將115做爲的客戶機打開:安裝mariadb
[root@localhost ~]# mysql -u amoeba -p123 -h 192.168.200.114 -P 8066 //登錄成功
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1781793025
Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 MariaDB Server
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
七、作讀寫分離的測試:
(1)在主服務器
MariaDB [(none)]> show databases;
MariaDB [(none)]> use lty
Database changed
MariaDB [lty]> create table student(id int(10),name varchar(10),address varchar(20));
Query OK, 0 rows affected (0.02 sec)
1十二、113也會有這個庫
將1十二、113主從複製的功能暫時先關閉
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.00 sec)
(2)111上插入一個數據:
MariaDB [lty]> insert into student values('2','lty','jinan');
Query OK, 1 row affected (0.01 sec)
(3)112上插入一個數據:
MariaDB [(none)]> use lty
Database changed
MariaDB [lty]> insert into student values('3','jhc','rizhao');
Query OK, 1 row affected (0.01 sec)
(4)113上插入一個數據:
MariaDB [(none)]> use lty
Database changed
MariaDB [lty]> insert into student values('6','xiaoxingxing','waixing');
Query OK, 1 row affected, 1 warning (0.01 sec)
(5)在客戶機上作測試:
[root@localhost ~]# mysql -uamoeba -p123 -h 192.168.200.114 -P 8066
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1781793025
Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 MariaDB Server
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [lty]> select * from student;
+------+------+---------+
| id | name | address |
+------+------+---------+
| 3 | jhc | rizhao |
+------+------+---------+
1 row in set (0.01 sec)
MySQL [lty]> select * from student;
+------+------------+---------+
| id | name | address |
+------+------------+---------+
| 6 | xiaoxingxi | waixing |
+------+------------+---------+
1 row in set (0.01 sec)
111:只寫
1十二、113:只讀
114:amoeba
115:客戶機