Redhat7.0系統利用amoeba對mysql數據進行讀寫分離的操做,MySQL數據庫的主從配置

這個是本實驗的流程說明圖
這裏寫圖片描述
實驗環境:四臺機器
系統環境:rhel7.0
amoeba雙網卡服務器:外網採用nat模式,內網地址:192.168.10.10
master服務器:192.168.10.11
slave-1服務器:192.168.10.12
slave-2服務器:192.168.10.13java

首先要配置mysql的主從複製,這裏採用了一臺master,兩臺slave進行實驗mysql

#master服務器上的相關服務與配置
[root@master ~]# yum -y install mariadb*
[root@master ~]# systemctl restart mariadb
[root@master ~]# systemctl enable mariadb
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
[root@master ~]# firewall-cmd --permanent --add-service=mysql
success
[root@master ~]# firewall-cmd --reload
success
[root@master ~]# mysqladmin -uroot password "123456";
#修改my.cnf文件
#
#在[mysqld]模塊下添加兩行文件
log-bin=master
server-id=11

[root@master ~]# mysql -u root -p123456
MariaDB [(none)]> grant replication slave on *.* to 'admin'@'192.168.10.%' identified by '123456';
######################################################################################################################
#slave-1服務器上的相關服務與配置
[root@slave-1 ~]# yum -y install mariadb*
[root@slave-1 ~]# systemctl restart mariadb
[root@slave-1 ~]# systemctl enable mariadb
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
[root@slave-1 ~]# firewall-cmd --permanent --add-service=mysql
success
[root@slave-1 ~]# firewall-cmd --reload
success
[root@slave-1 ~]# mysqladmin -uroot password "123456";
#修改my.cnf文件
#
#在[mysqld]模塊下添加兩行文件
log-bin=slave-1
server-id=12
[root@slave-1 ~]# systemctl restart mariadb
[root@slave-1 ~]# mysql -uroot -p123456
MariaDB [(none)]> change master to master_host='192.168.10.11',master_user='admin',master_password='123456',master_log_file='master.000001',master_log_pos=245;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> slave start;
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.11
                  Master_User: admin
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 526
        Relay_Master_Log_File: master.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 245
              Relay_Log_Space: 822
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 11
1 row in set (0.00 sec)
####################################################################################################################
#slave-2服務器上的相關服務與配置
[root@slave-2 ~]# yum -y install mariadb*
[root@slave-2 ~]# systemctl restart mariadb
[root@slave-2 ~]# systemctl enable mariadb
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
[root@slave-2 ~]# firewall-cmd --permanent --add-service=mysql
success
[root@slave-2 ~]# firewall-cmd --reload
success
[root@slave-2 ~]# mysqladmin -uroot password "123456";
#修改my.cnf文件
#
#在[mysqld]模塊下添加兩行文件
log-bin=slave-2
server-id=13
[root@slave-2 ~]# systemctl restart mariadb
[root@slave-2 ~]# mysql -uroot -p123456
MariaDB [(none)]> change master to master_host='192.168.10.11',master_user='admin',master_password='123456',master_log_file='master.000001',master_log_pos=245;
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G;
Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.11
                  Master_User: admin
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 526
        Relay_Master_Log_File: master.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 245
              Relay_Log_Space: 822
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 11
1 row in set (0.00 sec)

至此,咱們如今來驗證其是否成功linux

在master服務上web

MariaDB [(none)]> show databases;
+--------------------+
| Database | +--------------------+
| information_schema | | mysql | | performance_schema |
| test | +--------------------+
4 rows in set (0.00 sec)

在兩臺slave服務器上分別執行如下命令sql

MariaDB [(none)]> show databases;
+--------------------+
| Database | +--------------------+
| information_schema | | mysql | | performance_schema |
| test | +--------------------+
4 rows in set (0.00 sec)

在master服務器上建立一個數據庫數據庫

MariaDB [(none)]> create database library;
Query OK, 1 row affected (0.00 sec)

在兩臺slave服務器上分別執行如下命令,理論上一個會出現library這個數據庫的。vim

MariaDB [(none)]> create database library;
Query OK, 1 row affected (0.00 sec)

以上步驟爲mysql的主從複製配置成功,
接下來咱們要利用amoeba來對mysql進行讀寫分離的操做
首先須要準備軟件包
jave包
jdk-6u14-linux-x64.rpm
amoeba包
amoeba-mysql-binary-2.2.0.tar.gz
第一步:用傳輸工具吧兩個包傳進amoeba服務器裏面,ruby

[root@amoeba ~]# ll
total 83024
-rw-r--r--. 1 root root  3161433 Nov 20 21:13 amoeba-mysql-binary-2.2.0.tar.gz
-rw-------. 1 root root      898 Oct  4 21:15 anaconda-ks.cfg
-rw-r--r--. 1 root root 81849212 Nov 20 21:13 jdk-6u14-linux-x64.bin

第二步:在全部mysql服務器上執行如下命令服務器

grant all privileges on *.* to 'user'@'%' identified by '123456';

第三步:添加Java環境並加入到系統變量中ide

檢查系統中是否有Java的環境,若是有,請刪除

[root@amoeba ~]# ll /usr/bin/java
[root@amoeba ~]# ll /usr/bin/javac
[root@amoeba ~]# rm -rf /usr/bin/java
[root@amoeba ~]# rm -rf /usr/bin/javac
[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
[root@amoeba ~]# echo '
> export JAVA_HOME=/usr/local/jdk1.6 > export CLASSPATH=$CLASSPATH:/$JAVA_HOME/lib:$JAVA_HOME:/jre/lib
> export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin:$PATH:$PATH/bin
> export AMOEBA_HOME=/usr/local/amoeba/
> export PATH=$PATH:$AMOEBA_HOME/bin' >> /etc/profile

第四步:配置amoeba環境

[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/

第四步:更新系統環境變量而且驗證amoeba是否成功

[root@amoeba ~]# source /etc/profile
[root@amoeba ~]# /usr/local/amoeba/bin/amoeba
amoeba start|stop

第五步:編輯amoeba配置文件
首先先編輯
vim /usr/local/amoeba/conf/amoeba.xml

#在30行修改用戶密碼
 30                                         <property name="user">amoeba</property>
 31
 32                                         <property name="password">123456</property>
#在114行進行修改
114                 <property name="LRUMapSize">1500</property>
115                 <property name="defaultPool">master</property>
116
117                 <!---->
118                 <property name="writePool">master</property>
119                 <property name="readPool">slave</property>

vim /usr/local/amoeba/conf/dbServers.xml

#在26行修改其配置 
 26                         <property name="user">user</property>
 27
 28                         <!-- mysql password -->
 29                         <property name="password">123456</property>
#在45行修改其配置
 45         <dbServer name="master" parent="abstractServer">
 46                 <factoryConfig>
 47                         <!-- mysql ip -->
 48                         <property name="ipAddress">192.168.10.11</property>
 49                 </factoryConfig>
 50         </dbServer>
 51
 52         <dbServer name="slave-1" parent="abstractServer">
 53                 <factoryConfig>
 54                         <!-- mysql ip -->
 55                         <property name="ipAddress">192.168.10.12</property>
 56                 </factoryConfig>
 57         </dbServer>
 58
 59         <dbServer name="slave-2" parent="abstractServer">
 60                 <factoryConfig>
 61                         <!-- mysql ip -->
 62                         <property name="ipAddress">192.168.10.13</property>
 63                 </factoryConfig>
 64         </dbServer>
 65
 66         <dbServer name="slave" virtual="true">
 67                 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
 68                         <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
 69                         <property name="loadbalance">1</property>
 70
 71                         <!-- Separated by commas,such as: server1,server2,server1 -->
 72                         <property name="poolNames">slave-1,slave-2</property>
 73                 </poolConfig>
 74         </dbServer>

最後啓動amoeba

[root@amoeba ~]# /usr/local/amoeba/bin/amoeba start

檢驗
在客戶端安裝mysql-gui-tools-5.0.2226203566
安裝成功以後打開數據庫連接界面
其中 server host請輸入amoeba服務器的外網卡地址,用戶名爲amoeba,密碼爲123456,端口爲8066
這裏寫圖片描述

鏈接成功
(若是鏈接不成功,請把全部的服務器的防火牆關閉)
這裏寫圖片描述
打開一個mysql的終端,建立一個數據庫;
這裏寫圖片描述
而後在master,slave-1,slave-2數據庫服務器上show數據庫,都發現會出現amoeba這個數據庫
這裏寫圖片描述

相關文章
相關標籤/搜索