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.13

首先要配置mysql的主从复制,这里采用了一台master,两台slave进行实验

#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)

至此,我们现在来验证其是否成功

在master服务上

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

在两台slave服务器上分别执行以下命令

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这个数据库的。

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服务器里面,

[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环境并加入到系统变量中

检查系统中是否有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这个数据库
这里写图片描述

posted @ 2017-11-25 16:30  sujc-blogs  阅读(117)  评论(0编辑  收藏  举报