环境:阿里云服务器CentOS 8.4版本,docker20.10版本,mysql5.7版本,此处读写分离使用amoeba软件实现(amoeba版本:2.20tar包)。部署1主1从架构。
一,云服务器安装操作系统,此处为CentOS 8.4。
1,云服务器主机安装操作系统(https://www.aliyun.com),登录阿里云界面直接安装即可。
二,CentOS 8.4 安装docker
1,安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io
2,安装docker并启动,设置开机自启
yum install docker-ce docker-ce-cli
systemctl start docker
docker --version
systemctl enable docker //设置开机自启
三,docker下载mysql镜像(此处拉取MySQL5.7版本),配置mysql文件并创建和启动MySQL容器
1,docker pull mysql:5.7 //拉去MySQL镜像
2,创建主服务器文件夹:mkdir /home/mysql
mkdir /home/mysql/conf.d
mkdir /home/mysql/data/
3,创建从服务器文件夹:mkdir /home/mysql2
mkdir /home/mysql2/conf.d
mkdir /home/mysql2/data/
4,创建主服务器my.cnf配置文件
vim /home/mysql/my.cnf
1 [mysqld]
2 user=mysql
3 character-set-server=utf8
4 default_authentication_plugin=mysql_native_password
5 secure_file_priv=/var/lib/mysql
6 expire_logs_days=7
7 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO ,NO_ENGINE_SUBSTITUTION
8 max_connections=1000
9 log-bin=mysql-bin
10
11 log-slave-updates=true //允许从服务器同步
12 server-id=10 //标识符,每台服务器标识符不能相同
13
14 [client]
15 default-character-set=utf8
16
17 [mysql]
18 default-character-set=utf8
5,创建从服务器my.cnf配置文件
vim /home/mysql2/my.cnf
1 [mysqld]
2 user=mysql
3 character-set-server=utf8
4 default_authentication_plugin=mysql_native_password
5 secure_file_priv=/var/lib/mysql
6 expire_logs_days=7
7sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO ,NO_ENGINE_SUBSTITUTION
8 max_connections=1000
9
10 server-id=20 //注意区分主服务器id
11 relay-log=relay-log-bin //指定从服务器日志文件名
12 relay-log-index=slave-relay-bin.index //指定从服务器索引文件名
13
14 [client]
15 default-character-set=utf8
16
17 [mysql]
18 default-character-set=utf8
6,启动主库容器(挂载外部目录,端口映射成33307,密码设置为123456)
docker run -di -v /home/mysql/data/:/var/lib/mysql -v /home/mysql/conf.d:/etc/mysql/conf.d -v /home/mysql/my.cnf:/etc/mysql/my.cnf -p 33307:3306 --name mysql-master -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
7,启动从库容器(挂载外部目录,端口映射成33306,密码设置为123456)
docker run -di -v /home/mysql2/data/:/var/lib/mysql -v /home/mysql2/conf.d:/etc/mysql/conf.d -v /home/mysql2/my.cnf:/etc/mysql/my.cnf -p 33306:3306 --name mysql-slave -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
8,docker ps //查询容器启动是否成功,(如启动失败,执行docker logs查看错误日志,排查原因)
四,登录主库和从库,配置主从复制
1,连接主库:
docker exec -it d92761440128 /bin/bash //登录主库容器
mysql -uroot -p123456 //登录MySQL主库
2,创建test用户,并设置密码:
create user 'test'@'%' identified by '123';
3,授权用户:
grant all privileges on *.* to 'test'@'%' ; //此处授权为允许所有网段的ip访问
4,刷新权限
flush privileges;
5,查看主服务器状态
show master status;
6,查看主容器库ip
docker inspect --format={{.NetworkSettings.IPAddress}} d92761440128
7,连接从库:
docker exec -it 72a27a9e5621 /bin/bash //登录从库容器
mysql -uroot -p123456 //登录MySQL从库
8,配置主库信息
change master to master_host='172.17.0.2',master_port=3306,master_user='test',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=970;
/* 注解
change master to
master_host='MySQL主服务器IP地址',
master_port=MySQL容器服务的端口3306(注意:此端口不是宿主机端口)
master_user='之前在MySQL主服务器上面创建的用户名',
master_password='之前创建的密码',
master_log_file='MySQL主服务器状态中的二进制文件名',
master_log_pos='MySQL主服务器状态中的position值';
*/
9,启用从库
start slave;
10,查看从库状态(Slave_IO_Running: Yes Slave_SQL_Running: Yes 这两个参数为yes,则启动成功)
show slave status\G;
五,登录主库,测试主从复制同步
#在主库上创建数据库test1
create database test1;
use test1;
#创建表
create table tom (id int not null,name varchar(100)not null ,age tinyint);
#插入数据
insert tom (id,name,age) values(1,'xxx',20),(2,'yyy',7),(3,'zzz',23);
#在从库上查看是否同步成功
#查看数据库
show database;
use test1;
#查看表
show tables;
#查看数据
select * from test1;
六,部署amoeba,搭建读写分离架构
1,安装jdk(此处为jdk1.6版本)
yum -y erase java-*
1)chmod +x jdk-6u14-linux-x64.bin
2)./jdk-6u14-linux-x64.bin
3)mv jdk1.6.0_14/ /usr/local/jdk1.6
4)vim /etc/profile
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:$HOME/bin
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$PATH:$AMOEBA_HOME/bin
source /etc/profile && java -version
2,安装amoeba
mkdir /usr/local/amoeba //创建amoeba目录
tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/ //解压amoeba安装包
chmod -R 755 /usr/local/amoeba/
3,在主MySQL数据库新建授权用户
docker exec -it d92761440128 /bin/bash
mysql -uroot -p123456 -h 172.17.0.2
mysql> grant all on *.* to haha@'172.17.0.%' identified by '123';
4,配置amoeba配置文件
1)vim /usr/local/amoeba/conf/amoeba.xml
30 <property name="user">hehe</property>
31
32 <property name="password">123</property>
115 <property name="defaultPool">slaves</property>
116
117 <property name="writePool">master</property>
118 <property name="readPool">slaves</property>
2)vim /usr/local/amoeba/conf/dbServers.xml
25 <!-- mysql user -->
26 <property name="user">haha</property>
27
28 <property name="password">123</property>
44 <dbServer name="master" parent="abstractServer">
45 <factoryConfig>
46 <!-- mysql ip -->
47 <property name="ipAddress">172.17.0.2</property>
48 </factoryConfig>
49 </dbServer>
50
51 <dbServer name="slave" parent="abstractServer">
52 <factoryConfig>
53 <!-- mysql ip -->
54 <property name="ipAddress">172.17.0.3</property>
55 </factoryConfig>
56 </dbServer>
58 <dbServer name="slaves" virtual="true">
64 <property name="poolNames">slave</property>
65 </poolConfig>
5,启动amoeba
amoeba start &
此处启动后报错”Unable to load native library: libnsl.so.1: cannot open shared object file:“
解决方案:yum install libnsl (安装依赖库)后 即可解决
netstat -utpln | grep 8066 //查看端口启动后amoeba启动成功。