红联Linux门户
Linux帮助

MySQL和Amoeba实现同步读写分离centos7

发布时间:2018-04-03 14:56:00来源:Linux社区作者:南柯大梦
三台mysql数据库:主数据库服务器:192.168.80.100
从数据库服务器1:192.168.80.120
从数据库服务器2:192.168.80.180
一台amoeba调度器:192.168.80.190
一台mariadb客户端: 192.168.80.170
 

1、配置MySQL主从环境准备

 
配置NTP服务器-----时间同步
[[email protected] ~]# yum install -y ntp //安装时间同步程序
[[email protected] ~]# service ntpd start
[[email protected] ~]# ntpdate -u cn.pool.ntp.org /.同步网络时间
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
[[email protected] ~]# vi /etc/ntp.conf //配置时间同步
在合适位置新增以下三行:
restrict 192.168.80.0 mask 255.255.255.0 nomodify
server 127.127.1.0
fudge 127.127.1.0 stratum 8
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
在从服务器上:
[[email protected] ~]# yum install -y ntp
[[email protected] ~]# ntpdate 192.168.80.100
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
[[email protected] ~]# echo '/30 * /usr/sbin/ntpdate 192.168.80.100' >> /var/spool/cron/root //设置计划任务,每隔30分钟同步一次
[[email protected] ~]# crontab -l
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 

2、配置mysql服务器主从复制

在主mysql服务器上:192.168.80.100
[[email protected] ~]# vi /etc/my.cnf
 
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
[[email protected] ~]# service mysqld restart
[[email protected] ~]# mysql -u root -p //进入数据库
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
mysql> GRANT REPLICATION SLAVE ON . TO 'myslave'@'192.168.80.%' IDENTIFIED BY '123456';
//为所有从服务器授权所有数据库
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
mysql> FLUSH PRIVILEGES; //刷新权限
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
mysql> show master status G
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
注意position 的值和file
 
在从服务器1上设置:192.168.80.120
[[email protected] ~]# vi /etc/my.cnf
修改并新增以下内容:
server-id = 22
relay_log=relay-log-bin
relay_log_index=slave-relay-bin.index
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# mysql -u root -p
mysql> change master to master_host='192.168.80.100',master_user='myslave',master_password='123456',master_log_file='master_bin.000001',master_log_pos=802;
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
mysql> start slave;
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
mysql> show slave status G
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
在从服务器2上设置:
 
[[email protected] ~]# vi /etc/my.cnf
修改并新增以下内容:
server-id = 33
relay_log=relay-log-bin
relay_log_index=slave-relay-bin.index
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
[[email protected] ~]# service mysqld restart
[[email protected] ~]# mysql -u root -p
mysql> change master to master_host='192.168.80.100',master_user='myslave',master_password='123456',master_log_file='master_bin.000001',master_log_pos=802;
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
mysql> start slave;
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
mysql> show slave status G
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
测试,在主服务器上创建一个空库,看一下从服务器上有木有
在主服务上
mysql> create database ab;
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
在从服务器1上:
mysql> show databases;
 
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
在服务器2上:
mysql> show databases;
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
此时主从同步成功了。
 

3、搭建amoeba前端服务器:192.168.80.190

[[email protected] ~]# yum install -y ntpdate 
[[email protected] ~]# ntpdate 192.168.80.100
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
[[email protected] ~]# echo '/30 * /usr/sbin/ntpdate 192.168.80.100' >> /var/spool/cron/root
[[email protected] ~]# crontab -l
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
[[email protected] ~]# tar xf jdk-8u144-linux-x64.tar.gz -C /opt/
[[email protected] ~]# cd /opt/
[[email protected] opt]# cp -rv jdk1.8.0_144/ /usr/local/java
[[email protected] opt]# vi /etc/profile //搭建Java环境
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
[[email protected] opt]# source /etc/profile
[[email protected] opt]# java -version
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
搭建好了Java环境
 
[[email protected] ~]# unzip amoeba-mysql-3.0.5-RC-distribution.zip -d /opt/
[[email protected] ~]# cd /opt/
[[email protected] opt]# mv amoeba-mysql-3.0.5-RC/ /usr/local/amoeba
[[email protected] opt]# chmod -R 755 /usr/local/amoeba/
[[email protected] opt]# vi /usr/local/amoeba/jvm.properties
新增:
JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k"
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
[[email protected] opt]# vi /etc/init.d/amoeba //编辑系统服务脚本
 
#!/bin/bash
#chkconfig: 35 62 62
export JAVA_HOME=/usr/local/java
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
NAME=Amoeba
AMOEBA_BIN=/usr/local/amoeba/bin/launcher&
SHUTDOWN_BIN=/usr/local/amoeba/bin/shutdown
PIDFILE=/usr/local/amoeba/Amoeba-MySQL.pid
SCRIPTNAME=/etc/init.d/amoeba
 
case "$1" in
start)
echo -n "Starting $NAME... "
$AMOEBA_BIN
echo " done"
;;
stop)
echo -n "Stoping $NAME... "
$SHUTDOWN_BIN
echo " done"
;;
restart)
$SHUTDOWN_BIN
sleep 1
$AMOEBA_BIN
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}"
exit 1
;;
Esac
 
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
[[email protected] opt]# chmod +x /etc/init.d/amoeba //给予执行权限
[[email protected] opt]# chkconfig --add amoeba //添加到系统管理中
[[email protected] opt]# service amoeba start //开启amoeba服务
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离

4、配置读写分离

 
----在三台mysql数据库中为amoeba授权-------
mysql> GRANT ALL ON . TO [email protected]'192.168.80.%' IDENTIFIED BY '123123';
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
mysql> FLUSH PRIVILEGES;
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
--------在amoeba前端服务器上----
[[email protected] amoeba]# cd /usr/local/amoeba
[[email protected] amoeba]# vi conf/amoeba.xml
-----设置客户端连接amoeba前端服务器时使用的用户名和密码----28行
<property name="user">amoeba</property>
<property name="password">123456</property>
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
----同时把默认的server1改成master,把默认的servers改成 slaves-----83行
<property name="defaultPool">master</property>
<property name="writePool">master</property>
<property name="readPool">slaves</property>
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
[[email protected] conf]# vi dbServers.xml
 
<property name="user">test</property>
<property name="password">123123</property> //指定数据库的登录用户名和密码
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
修改以下内容
 
<dbServer name="master" parent="abstractServer">
<property name="ipAddress">192.168.80.100</property>
<dbServer name="slave1" parent="abstractServer">
<property name="ipAddress">192.168.80.120</property>
<dbServer name="slave2" parent="abstractServer">
<property name="ipAddress">192.168.80.180/property>
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
<dbServer name="slaves" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
<property name="poolNames">slave1,slave2</property>
</poolConfig>
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
[[email protected] amoeba]# service amoeba restart
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
[[email protected] amoeba]# netstat -anpt | grep java
 
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 

5、搭建mariadb客户端:192.168.80.170

[[email protected] ~]# yum install -y mysql
[[email protected] ~]# mysql -u amoeba -p123456 -h 192.168.80.190 -P8066
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
在客户端上使用命令在test创建一个表:
MySQL [(none)]> use test;
MySQL [test]> create table zhang (id int(10),name varchar(10),address varchar(20));
MySQL [test]> show tables;
 
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
在从服务器上
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
在其中一台从服务器上创建一个空库
 
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
在主服务器上查看所有库
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
没有新建的那个库
 
在客户端上查看:
CentOS 7.4下MySQL+Amoeba实现主从同步读写分离
 
由此所见,从服务器上建库,客户端可以读取,说明读写分离成功。