当前位置:   article > 正文

shell脚本安装MySQL(Docker版)_install_mysql.sh

install_mysql.sh

install_mysql.sh

#!/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    
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185

示例

bash install_mysql.sh install 8.0.29
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/513698
推荐阅读
相关标签
  

闽ICP备14008679号