docker 制作mysql镜像并自动安装脚本

centos7环境下 一键制作mysql docker镜像,并安装

centos7环境下 一键制作mysql docker镜像,并安装

shell脚本内容如下:
#docker自动安装mysql



echo -e "\033[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 默认安装参数: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' \033[0m"
echo -e "\033[32m '端口:3309' \033[0m"
echo -e "\033[32m '映射宿主机路径: /mysql' \033[0m"
echo -e "\033[32m '密码:xxxxxx' \033[0m"
echo -e "\033[32m '容器名称:mysql8' \033[0m"
echo -e "\033[32m '开启binlog' \033[0m"
echo -e "\033[32m '设置连接数1k' \033[0m"
echo -e "\033[32m '设置连接超时断开24h' \033[0m"
echo -e "\033[32m '设置使用4个cpu核心' \033[0m"


### 0判断挂载目录是否存在或被使用
if [ -d "/mysql" ];then
	echo -e "\033[31m '__________________________________  0存在/mysql文件夹,请手动删除后再执行 ,and EXIT' \033[0m"
	exit 1
fi


mkdir -p /mysql
chmod 777 /mysql



### 1下载镜像
docker pull mysql:8.0.27
if [ $? -eq 0 ]
    then 
        echo -e "\033[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1下载镜像 finish >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' \033[0m"
    else 
        echo -e "\033[31m '__________________________________  1下载镜像 bad ,and EXIT' \033[0m"
        exit 1
fi




###______________________________________________________________________________________________________
### 创建 dockerfile


rm -rf ./mymysql.yaml
echo '### dockerfile 自定义mysql容器镜像' >> ./mymysql.yaml
echo 'FROM mysql:8.0.27' >> ./mymysql.yaml
echo '#使用官方mysql镜像最新版' >> ./mymysql.yaml
echo '' >> ./mymysql.yaml
echo 'RUN echo "###" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo '' >> ./mymysql.yaml
echo 'RUN echo "log_bin = 1" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo '#开启binlog日志,存储在默认位置' >> ./mymysql.yaml
echo '' >> ./mymysql.yaml
echo 'RUN echo "max_connections = 1050" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo 'RUN echo "mysqlx_max_connections = 1000" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo '#最大连接数' >> ./mymysql.yaml
echo '' >> ./mymysql.yaml
echo 'RUN echo "wait_timeout=86400" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo 'RUN echo "interactive_timeout=86400" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo '#空闲断开时长,秒' >> ./mymysql.yaml
echo '' >> ./mymysql.yaml
echo 'RUN echo "innodb_read_io_threads=4" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo 'RUN echo "innodb_write_io_threads=4" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo '#设置使用多核cpu,默认使用4核,根据cpu核心数设置' >> ./mymysql.yaml




###______________________________________________________________________________________________________
###使用mymysql.yaml构建镜像mymysql:1


docker build -f ./mymysql.yaml -t "mymysql:1" .
if [ $? -eq 0 ]
    then 
        echo -e "\033[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> mymysql:1 镜像构建成功 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' \033[0m"
    else 
        echo -e "\033[31m '__________________________________  mymysql:1 镜像构建失败,and EXIT' \033[0m"
        exit 1
fi


### 2创建容器
docker create -p 3309:3306 --restart=always --name mysql8 -v /mysql/mysql_08/log:/var/log/mysql -v /mysql/mysql_08/data:/var/lib/mysql -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=xxxxx -it  mymysql:1
if [ $? -eq 0 ]
    then 
        echo -e "\033[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  2创建容器 finish >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' \033[0m"
    else 
        echo -e "\033[31m '__________________________________  2创建容器 bad ,and EXIT' \033[0m"
        exit 1
fi

### 3启动容器
docker start mysql8
if [ $? -eq 0 ]
    then 
        echo -e "\033[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 3启动容器 finish >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' \033[0m"
    else 
        echo -e "\033[31m '__________________________________  3启动容器 bad ,and EXIT' \033[0m"
        exit 1
fi

### 显示容器状态
echo -e "\033[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 显示已运行容器:' \033[0m"
docker ps
posted @ 2022-05-20 15:45  tangshow  阅读(144)  评论(0编辑  收藏  举报