#! /bin/bash

set -e

function green_echo() {
    echo -e "\033[32m$1\033[0m"
}

green_echo "拉取 mysql5.7 镜像..."
docker pull mysql:5.7

green_echo "准备挂载目录，并配置 mysql..."
sudo mkdir -p /data/mysql57/log
sudo mkdir -p /data/mysql57/data
sudo mkdir -p /data/mysql57/conf

sudo sh -c "cat << 'EOF' > /data/mysql57/conf/my.cnf
[mysqld]
default_time_zone = '+8:00'
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
lower_case_table_names=1
EOF"

green_echo "运行容器..."
docker run -p 13306:3306 --name mysql57 \
-v /data/mysql57/log:/var/log/mysql \
-v /data/mysql57/data:/var/lib/mysql \
-v /data/mysql57/conf:/etc/mysql \
--restart=always \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7

green_echo "mysql 5.7 安装完成！"
