基于 DOCKER MySQL 主从搭建:
一、根据 Linux 系统版本号安装 docker
EG:
Centos 7:
1.shell>sudo yum update
2.添加 dockeryum
$
[dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/7/ enabled=1
gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg EOF 3.安装 docker 的包
shell>sudo yum install docker-engine
4.将 docker 加到服务里面
shell>sudo systemctl enable docker.service 5.启动 docker
shell> sudo systemctl start docker
二、将 docker 镜像导入 linux:
镜像文件上传到/opt/dtstack 目录下:
1.载入镜像:
Shell>cd /opt/dtstack
Shell>docker load < mysql-5.6-x86_64.tar
[root@centos7 docker]# docker load < mysql-5.6-x86_64.tar
c411e76031a4: Loading layer [==================================================>] 201.9 MB/201.9 MB
abd40b5d012f: Loading layer [==================================================>] 60.97 MB/60.97 MB
7d082aa6bb0c: Loading layer [==================================================>] 263.3 MB/263.3 MB
173e11cd6be1: Loading layer [==================================================>] 6.656 kB/6.656 kB
bdb04743768e: Loading layer [==================================================>]
sudo
tee /etc/yum.repos.d/docker.repo <<-'EOF'
3.072 kB/3.072 kB
9da6836b8f64: Loading layer [==================================================>] 4.872 MB/4.872 MB
d47abb872b5c: Loading layer [==================================================>] 20.3 MB/20.3 MB
008419e2fbfc: Loading layer [==================================================>] 108.9 MB/108.9 MB
95d9d0bff849: Loading layer [==================================================>] 23.04 kB/23.04 kB
71405897d961: Loading layer [==================================================>] 1.68 MB/1.68 MB
58b14b6eb7df: Loading layer [==================================================>] 49.48 MB/49.48 MB
fffc1c2170a5: Loading layer [==================================================>] 94.12 MB/94.12 MB
9d60f33d82f1: Loading layer [==================================================>] 23.7 MB/23.7 MB
2b9b1be70303: Loading layer [==================================================>] 6.656 kB/6.656 kB
a2018f6089d9: Loading layer [==================================================>] 1.122 GB/1.122 GB
9e274ef73e1d: Loading layer [==================================================>] 13.82 kB/13.82 kB
d0ca45b2e986: Loading layer [==================================================>] 6.144 kB/6.144 kB
Loaded image: dtstack_db_01:5.6
2.列出本地已有镜像:
Shell> docker images REPOSITORY TAG SIZE
dtstack_db_01 5.6 1.92 GB
3.传参脚本说明: #!/bin/bash APP_NAME=db_01
DOCKER_IMAGE=dtstack_db_01:5.6
docker_run(){
docker run -dt \
--restart=always --name $APP_NAME \
IMAGE ID 26e6aa6ed6c2
CREATED 22 hours ago
-p 3306:3306 #映射的端口号
-v /etc/localtime:/etc/localtime:ro \
-v /data/mysql/my3306:/data/mysql \
/data/mysql 目录必须存在
-e "dbport=3306" #dbport
-e "db_root_password=root123" -e "db_master_ip=172.16.1.65" -e "db_slave_ip=172.16.1.86" \
宿主机目录挂载到/data/mysql/myxxx 下,
-e "db_tag=master" -e "db_instance=4" \
实例...
-i $DOCKER_IMAGE
} docker_run
4.运行 mysql_docker.sh 脚本 Shell>./mysql_docker.sh
db 角色
选择性部署多实例。EG:单实例 db_instance=1, 3 则是 3 个
5.检查容器运行状态: Shell>docker ps –a CONTAINER ID
STATUS
IMAGE PORTS
COMMAND
"/bin/sh -c '/home..." 34 seconds ago
NAMES
CREATED
dd47a290ae94
Up 34 seconds
dtstack_db_01:5.6
0.0.0.0:3306->3306/tcp, 3307-3311/tcp db_01
6.进行观察日志信息是否搭建成功:
Shell>docker logs dd47a290ae94
170317 19:31:09 mysqld_safe Logging to '/data/mysql/log/alert.log'.
170317 19:31:09 mysqld_safe Starting mysqld daemon with databases from /data/mysql/data create replicaiton log
master change master to
update root password log
SLAVE: 同上述操作: 1.需要更改的: #!/bin/bash APP_NAME=db_01
DOCKER_IMAGE=dtstack_db_01:5.6
docker_run(){ docker run -dt \
--restart=always \
--name $APP_NAME \
-p 3306:3306 \
-v /etc/localtime:/etc/localtime:ro \
-v /data/mysql/my3306:/data/mysql \
-e "dbport=3306" \
-e "db_root_password=root123" \
-e "db_master_ip=172.16.1.69" \
-e "db_slave_ip=172.16.1.86" \
-e "db_tag=slave" #将 tag 标签转换为 slave,其他不需要修改 -e "db_instance=4" \
-i $DOCKER_IMAGE
}
2.启动 mysql_slave.sh 脚本: Shell>./mysql_docker.sh [root@localhost docker]# docker ps -a
CONTAINER ID CREATED NAMES fb45654c2eda Up 6 minutes docker_run
IMAGE STATUS
dtstack_db_01:5.6 0.0.0.0:3306->3306/tcp, 3307-3311/tcp
COMMAND PORTS
6 minutes ago
db_01
"/bin/sh -c '/home..."
三、通过外部连接 mysql:检查主从是否正常:
1.SLAVE:
Shell>mysql -h172.16.1.86 -uroot -proot123 -P3306 -e "start slave;show slave statusG" | grep _Running | awk 'NR!=3'
Warning: Using a password on the command line interface can be insecure.
2.master:
[root@centos7 docker]# mysql -h172.16.1.65 -uroot -proot123 -P3306 -e "start slave;show slave statusG" | grep _Running | awk 'NR!=3'
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Slave_IO_Running: Yes