案例描述
案例环境
Master 192.168.1.2 cmake mysql
Slave1 192.1681.5 cmake mysql
Amoeba 192.168.1.7 amoeba jdk-6u14
客户端 192.168.1.8 mysql mysql-connector-java
案例实施
一.主从复制
关闭防火墙
Systemctl stop firewalld
Seteforce 0
1.安装mysql数据库(master和slave1都需要执行)
使用脚本编译安装数据库,需要软件包:cmake-2.8.6.tar和mysql-5.6.36.tar
脚本内容如下:#!/bin/bash
rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
rpm -e mysql-server mysql
yum -y install gcc gcc-c++ ncurses-devel perl autoconf
tar zxvf cmake-2.8.6.tar.gz -C /usr/src/
cd /usr/src/cmake-2.8.6/
./configure && gmake && gmake install
cd
tar zxvf mysql-5.6.36.tar.gz -C /usr/src/
cd /usr/src/mysql-5.6.36/
cmake -DCMAK_INSTALL-PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWUTG_EXTRA_CHARSETS=all
make && make install
groupadd mysql
useradd -M -s /sbin/nologin -g mysql mysql
chown -R mysql:mysql /usr/local/mysql
rm -rf /etc/my.cnf
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
echo “PATH=
P
A
T
H
:
/
u
s
r
/
l
o
c
a
l
/
m
y
s
q
l
/
b
i
n
"
>
>
/
e
t
c
/
p
r
o
f
i
l
e
.
/
e
t
c
/
p
r
o
f
i
l
e
/
u
s
r
/
l
o
c
a
l
/
m
y
s
q
l
/
s
c
r
i
p
t
s
/
m
y
s
q
l
i
n
s
t
a
l
l
d
b
−
−
u
s
e
r
=
m
y
s
q
l
−
−
b
a
s
e
d
i
r
=
/
u
s
r
/
l
o
c
a
l
/
m
y
s
q
l
−
−
d
a
t
a
d
i
r
=
/
u
s
r
/
l
o
c
a
l
/
m
y
s
q
l
/
d
a
t
a
/
u
s
r
/
b
i
n
/
s
y
s
t
e
m
c
t
l
s
t
a
r
t
m
y
s
q
l
d
脚
本
执
行
完
成
后
执
行
e
c
h
o
"
P
A
T
H
=
PATH:/usr/local/mysql/bin" >> /etc/profile . /etc/profile /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data /usr/bin/systemctl start mysqld 脚本执行完成后 执行echo "PATH=
PATH:/usr/local/mysql/bin">>/etc/profile./etc/profile/usr/local/mysql/scripts/mysqlinstalldb−−user=mysql−−basedir=/usr/local/mysql−−datadir=/usr/local/mysql/data/usr/bin/systemctlstartmysqld脚本执行完成后执行echo"PATH=PATH:/usr/local/mysql/bin” >> /etc/profile
. /etc/profile
Mysqladmin -u root password ‘pwd123’//为root用户设置密码
2.配置MYSQL主服务器(master)
Vim /etc/my.cnf //修改内容
server_id = 11
log_bin=master-bin
log-slave-updates = true
Systemctl restart mysql //重启服务
登录MYSQL程序给服务器以授权
Mysql -u root -p
GRANT PEPLICATION SLAVE ON . TO ‘myslave’ @ ‘192.168.1.%’ IDENTIFIED BY ‘pwd123’;
FLUSH PRIVILEGES;
Show master status;如图:
3.配置从服务器(slave1)
配置MYSQL主服务器(master)
Vim /etc/my.cnf //修改内容
server_id = 22
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
Systemctl restart mysql //重启服务
登录mysql,配置同步
Mysql -u root -p
change master to master_host=‘192.168.1.2’,master_user=‘myslave’,master_password=‘pwd123’,master_log_file=‘master-bin.000001’,master_log_pos=410;
start slave;
show slave status\G //如图
查看SLAVE状态,确保两个值为yes 如图
4.验证主从复制效果
在主.从服务器上登录mysql
Mysql -u root -p
Show databases;
两台结果应该相同
(1)在主服务器上新建数据库db_test.
Create database db_test; //如图
(2)在主从服务器上分别查看数据库,显示数据相同,则主从复制成功//如图
二.读写分离
1.在amoeba上安装Java环境
需要软件包jdk-6u14-linux-x64.bin
Chmod +x jdk-6u14-linux-x64.bin
./jdk-6u14-linux-x64.bin
Mv jdk1.6.0_14/ /usr/local/jdk1.6
Vim /etc/profile
增加以下配置
export JAVA_HOME=/usr/local/jdk1.6
export CLASSPATH=
C
L
A
S
S
P
A
T
H
:
CLASSPATH:
CLASSPATH:JAVA_HOME/lib:
J
A
V
A
H
O
M
E
/
j
r
e
/
l
i
b
e
x
p
o
r
t
P
A
T
H
=
JAVA_HOME/jre/lib export PATH=
JAVAHOME/jre/libexportPATH=JAVA_HOME/lib:
J
A
V
A
H
O
M
E
/
j
r
e
/
b
i
n
:
JAVA_HOME/jre/bin:
JAVAHOME/jre/bin:PATH:
H
O
M
E
/
b
i
n
e
x
p
o
r
t
A
M
O
E
B
A
H
O
M
E
=
/
u
s
r
/
l
o
c
a
l
/
a
m
o
e
b
a
/
e
x
p
o
r
t
P
A
T
H
=
HOME/bin export AMOEBA_HOME=/usr/local/amoeba/ export PATH=
HOME/binexportAMOEBAHOME=/usr/local/amoeba/exportPATH=PATH:$AMOEBA_HOME/bin
Sourece /etc/profile
Java -version //如图
java环境已配置成功
2.安装并配置amoeba软件
mkdir /usr/local/amoeba
tar zxf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/
chmod -R 755 /usr/local/amoeba/
/usr/local/amoeba/bin/amoeba
amoeba start|stop //显示此内容表示已安装成功 //如图
3.配置amoeba读写分离
(1)master,slaver1中开放权限给amoeba访问。
grant all on . to test@‘192.168.1.%’ identified by ‘123.com’;
(2)编辑amoeba.Xml配置文件
cd /usr/local/amoeba/
Vim conf/amoeba.xml
(3)编辑dbServers.xml配置文件
Vim conf/dbServer.xml
(3)配置无误后,可以启动amoba软件,其默认端口为tcp 8066
Bin/amoeba start&
Netstat -anpt |grep java
4.测试
(1)在client主机
Yum -y install mysql mysql-connector-java
可以通过代理访问mysql
Mysql -u root -ppwd123 -h 192.168.1.7 -P8066 //如图
(2)在master上创建一个表同步到各服务器上,然后关掉各服务器的slave功能 ,再插入区别语句
Use db_test;
create table zang (id int(10),name varchar(10),address varchar(20));
(3)在主服务器上
Stop slaves;
insert into zang values(‘1’,‘zhang’,‘this_is_master’);
从服务器上统计了表手动插入其它内容
insert into zang values(‘2’,‘zhang’,‘this_is_slave1’);
(4)测试读主机
在测试机上查询
select =* from zang; //如图
(5)在测试机上插入一条语句
insert into zang values(‘4’,‘zhang’,‘write_test’);
但在测试机上查询不到,只有现在master上才能查看到这条语句。
实验成功!