赞
踩
#!/bin/bash <<INFO AUTHOR:运维@小兵 DATE:2021-06-26 DESCRIBE:Docker方式安装MySQL SYSTEM:CentOS7/RedHat7 WARNING: MODIFY: INFO set -e #环境检查[ERROR] Check_Env() { echo "[INFO] Begin Check Env..." ! command -v ss &> /dev/null && echo "[ERROR] ss Command Not Exist" && exit 1 if ss -nutlp | grep -w ":${MYSQL_PORT}" &> /dev/null;then echo "[ERROR] ${MYSQL_PORT} Is Used" && exit 1 fi [[ ! -d ${MYSQL_DATA_DIR} ]] && mkdir -p ${MYSQL_DATA_DIR} if ! command -v docker &> /dev/null;then echo "[ERROR] Please Install Docker" && exit 1 fi } #配置docker Config_Docker() { mkdir -p /etc/docker if [[ -f /etc/docker/daemon.json ]];then [[ ! -f /etc/docker/daemon.json.bak ]] && cp /etc/docker/daemon.json{,.bak} fi tee /etc/docker/daemon.json << EOF { "registry-mirrors": ["https://8auvmfwy.mirror.aliyuncs.com"], "storage-driver":"overlay2", "log-driver": "json-file", "log-opts": { "max-size": "50m", "max-file":"3" } } EOF systemctl restart docker } #拉取镜像 Pull_Image() { echo "[INFO] Begin Pull Image ${IMAGE_ADDR}..." Config_Docker docker pull ${IMAGE_ADDR} } #准备MySQL的配置文件 Prepare_Mysql_Conf() { echo "[INFO] Begin Prepare MySQL ${MYSQL_VERSION} Conf..." mkdir -p ${MYSQL_DATA_DIR}/conf if [[ ${MYSQL_VERSION} == "5.7.32" ]];then cat > ${MYSQL_DATA_DIR}/conf/my.cnf << EOF [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql log-error =/var/lib/mysql/mysqld.log plugin-load = validate_password.so validate-password = OFF character_set_client_handshake = FALSE character_set_server = utf8mb4 collation-server = utf8mb4_bin init_connect='SET NAMES utf8mb4 COLLATE UTF8MB4_BIN' max_connections = 3000 wait_timeout = 3600 transaction_isolation = READ-COMMITTED innodb_log_buffer_size = 167772160 innodb_log_file_size = 1024M innodb_buffer_pool_size = 2G innodb_strict_mode = 0 innodb_flush_log_at_trx_commit = 1 binlog_format = row sync_binlog = 1 max_heap_table_size = 512M max_allowed_packet = 512M lower_case_table_names = 0 skip_name_resolve EOF elif [[ ${MYSQL_VERSION} == "8.0.29" ]];then cat > ${MYSQL_DATA_DIR}/conf/my.cnf << EOF [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql log-error =/var/lib/mysql/mysqld.log default_time_zone='+08:00' default_authentication_plugin='mysql_native_password' character_set_client_handshake = FALSE character_set_server = utf8mb4 #character_set_system = utf8 collation_server = utf8mb4_bin init_connect='SET NAMES utf8mb4 COLLATE UTF8MB4_BIN' secure-file-priv= NULL max_connections = 3000 wait_timeout = 3600 transaction_isolation = READ-COMMITTED innodb_log_buffer_size = 167772160 innodb_log_file_size = 1024M innodb_strict_mode = 0 innodb_flush_log_at_trx_commit = 1 binlog_format = row sync_binlog = 1 max_heap_table_size = 512M max_allowed_packet = 512M lower_case_table_names = 0 skip_name_resolve # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Custom config should go here !includedir /etc/mysql/conf.d/ EOF fi } #安装MySQL容器 Install_Mysql() { Check_Env Pull_Image Prepare_Mysql_Conf echo "[INFO] Begin Install Mysql ${MYSQL_VERSION}..." mkdir -p ${MYSQL_DATA_DIR}/{data,conf} docker run -d --name=${MYSQL_CONTAINER_NAME} --restart unless-stopped \ -v ${MYSQL_DATA_DIR}/data:/var/lib/mysql \ -v ${MYSQL_DATA_DIR}/conf/my.cnf:/etc/mysql/my.cnf \ -v /etc/localtime:/etc/localtime \ -p ${MYSQL_PORT}:3306 \ -e MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD}" \ ${IMAGE_ADDR} \ --character-set-server=utf8mb4 sleep 25 if ${MYSQL_CMD} -e 'show databases;' &> /dev/null;then echo "[INFO] Install Mysql ${MYSQL_VERSION} Success" else echo "[ERROR] Install Mysql ${MYSQL_VERSION} Fail" && exit 1 fi } #卸载MySQL Uninstall_Mysql() { read -p "数据无价,请确认是否执行卸载,(yes/no)": select case ${select} in yes) if docker ps | egrep -w "\b${MYSQL_CONTAINER_NAME}$" &> /dev/null;then docker rm -f ${MYSQL_CONTAINER_NAME} rm -rf ${MYSQL_DATA_DIR} echo "[INFO] ${MYSQL_CONTAINER_NAME} Container uninstall Success" && exit 0 else echo "WARN:${MYSQL_CONTAINER_NAME} Container Is Without" && exit 0 fi ;; no) exit 0;; *) echo "[ERROR] Input Is Invalid" && exit 1 esac } ######################主程序###################### [[ $# -ne 2 ]] && echo "[ERROR] Invalid Param!!! eg:bash $0 (install/uninstall) 数据库版本" && exit 1 MYSQL_VERSION="$2" MYSQL_IP=`ip addr |awk '/inet /' |sed -n '2p' |awk -F' ' '{print $2}' |awk -F'/' '{print $1}'` MYSQL_PORT="3306" MYSQL_DATA_DIR="/data/mysql" MYSQL_CONTAINER_NAME="mysql" MYSQL_ROOT_PASSWORD="devops" IMAGE_ADDR="mysql:${MYSQL_VERSION}" MYSQL_CMD="mysql -h${MYSQL_IP} -P${MYSQL_PORT} -uroot -p${MYSQL_ROOT_PASSWORD}" case $1 in install) Install_Mysql;; uninstall) Uninstall_Mysql;; *) echo "[ERROR] Invalid Param!!! eg:bash $0 install or uninstall" && exit 1 esac
bash install_mysql.sh install 8.0.29
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。