使用现成的docker容器能够快速搭建mysql服务,且管理起来很方便,具有容灾、可移植等优点,本架构需要维护一张docker容器信息表,以做到ip和port等信息的统一分发与管理
为了避免不必要的错误,建议数据存放路径与我这里保持一致:
[root@JackRoy ~]# mkdir -p /opt/dockers/docker_db_mysql/online
[root@JackRoy ~]# mkdir -p /opt/dockers/docker_db_mysql/mysql_data/conf
[root@JackRoy ~]# vim /opt/dockers/docker_db_mysql/mysql_data/conf/my.cnf
[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[root@JackRoy ~]# mkdir -p /opt/dockers/docker_db_mysql/mysql_data/db
[root@JackRoy ~]# mkdir -p /opt/dockers/docker_db_mysql/mysql_data/init
[root@JackRoy ~]# vim /opt/dockers/docker_db_mysql/mysql_data/init/init.sql
create database common charset utf8;
use common;
grant all on common.* to 'common'@'localhost' identified by 'common_user@1235';
grant all on common.* to 'common'@'%' identified by 'common_user@1235'
这里借用一下同事刘某源(感谢老源~)编写的docker启动命令,直接编辑输入:
[root@JackRoy online]# vim /usr/bin/dockerutils
#!/bin/bash
# Settle issue (e.g.: SELINUX) of when mount host directory, it has no privilege to access.
chcon -Rt svirt_sandbox_file_t $dir > /dev/null 2>&1
container_name=
TOP_PID=$$
trap 'exit 127' TERM
gate_way=`route | grep 'default' | awk '{print $2}'`
code=0
find_name(){
_container_name=`cat $dir/docker-compose.yml | grep container_name | awk -F ':' '{print $2}' | sed 's/^[ ]*//g' | sed 's/[ ]*$//g'`
_container_name=${
_container_name//\"/}
container_name=${_container_name//\'/}
if [ x$container_name == x ]
then
echo -e "\033[31mERROR : please set container_name in your docker-compose.yml file \033[0m"
kill -s TERM $TOP_PID
fi
}
check(){
_host_name=`cat $dir/docker-compose.yml | grep hostname | awk -F ':' '{print $2}' | sed 's/^[ ]*//g' | sed 's/[ ]*$//g'`
_host_name=${_host_name//\"/}
host_name=${_host_name//\'/}
echo "-------------------------------------------------"$host_name
if [ x$host_name == x ]
then
echo -e "\033[31mERROR : hostname not set ! \033[0m"
code=1
fi
_ip=`cat $dir/docker-compose.yml | grep ipv4_address | awk -F ':' '{print $2}' | sed 's/^[ ]*//g' | sed 's/[ ]*$//g'`
_ip=${_ip//\"/}
ip=${_ip//\'/}
if [ $1 = y ] ; then
fail=`ping $ip -c 3 -s 1 -W 1 | grep "100% packet loss" | wc -l`
if [ $fail -eq 0 ]
then
echo -e "\033[31mERROR : address is already in use \033[0m"
code=1
fi
fi
if [ $code -ne 0 ] ; then kill -s TERM $TOP_PID ;fi
}
up(){
check n
docker-compose -f $dir/docker-compose.yml up -d
start
}
reup(){
docker rm -f $container_name
up
}
start(){
docker start $container_name
}
stop(){
docker stop $container_name
}
restart(){
docker stop $container_name
sleep 3
docker start $container_name
}
usage(){
echo -e " Usage : \n \t $0 [ usage ] | < up | reup | start | stop | restart > "
echo -e "\t ============= nat docker ==============="
echo -e "\t up : create container and start app"
echo -e "\t reup : re-create container and start app"
echo -e "\t start : start container and app"
echo -e "\t stop : stop container and app"
echo -e "\t restart : restart container and app"
kill -s TERM $TOP_PID
}
if [ "$1"x = ""x ] || [ "$1"x = "usage" ] ; then
usage
fi
find_name
$@
exit $?
进入/opt/dockers/docker_db_mysql/online目录下:
[root@JackRoy online]# cd /opt/dockers/docker_db_mysql/online
编辑deploy_comm.sh并输入:
#!/bin/bash
dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
. dockerutils $@
编辑docker-compose.yml并输入:
version: '2.2'
services:
mysqlcommon55:
image: "docker.io/mysql:5.5"
container_name: mysql-common
hostname: mysql-common
restart: always
volumes:
- "../mysql_data/db:/var/lib/mysql"
- "../mysql_data/conf/my.cnf:/etc/my.cnf"
- "../mysql_data/init:/docker-entrypoint-initdb.d/"
cpus: "1"
mem_limit: "256M"
environment:
MYSQL_ROOT_PASSWORD: "root@123"
MYSQL_USER: 'common'
MYSQL_PASS: 'common@123'
ports:
- "3307:3306"
networks:
aidata_network:
ipv4_address: 10.111.113.20
networks:
aidata_network:
external: true
在/opt/dockers/docker_db_mysql/online路径下,执行启动命令:
# deploy_comm.sh 支持 reup up stop多种命令
[root@JackRoy online]# sh deploy_comm.sh reup
Error response from daemon: No such container: mysql-common
-------------------------------------------------mysql-common
WARNING: Found orphan containers (jenkins, gitlab) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Pulling mysqlcommon55 (docker.io/mysql:5.5)...
Trying to pull repository docker.io/library/mysql ...
5.5: Pulling from docker.io/library/mysql
743f2d6c1f65: Pull complete
3f0c413ee255: Pull complete
aef1ef8f1aac: Pull complete
f9ee573e34cb: Pull complete
3f237e01f153: Pull complete
03da1e065b16: Pull complete
04087a801070: Pull complete
7efd5395ab31: Pull complete
1b5cc03aaac8: Pull complete
2b7adaec9998: Pull complete
385b8f96a9ba: Pull complete
Digest: sha256:12da85ab88aedfdf39455872fb044f607c32fdc233cd59f1d26769fbf439b045
Status: Downloaded newer image for docker.io/mysql:5.5
Creating mysql-common ... done
mysql-common
执行启动命令以后我们进入docker容器,如果能成功进入并进入mysql那便是执行成功了:
[root@JackRoy online]# docker exec -it mysql-common bash
root@mysql-common:/# mysql -uroot -proot@123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.62 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
Bye
root@mysql-common:/# exit
exit
[root@JackRoy online]#
docker版mysql的安装过程相对简单,朋友们在操作过程遇到什么问题可以留言交流
第一节 基础环境介绍与准备
第二节 docker版gitlab安装和配置
第三节 docker版jenkins安装和配置
第四节 docker版mysql安装和配置
第五节 docker信息明细表设计与实现(含收集脚本)
第六节 docker容器自动化生成脚本
第七节 自动生成docker容器并启动项目