GLIBC 版本可以指定初始化密码
touch auto_mysql_5.7.sh && chmod +x auto_mysql_5.7.sh && vim auto_mysql_5.7.sh
shell 脚本执行流程
检查并关闭 selinux
检查是否已安装过 MariaDB,安装过则卸载
判断是否存在 mysql 用户,存在则删除
检查是否存在 netstat 命令,没有则安装
检查端口是否被占用,被占用则退出
解压文件 mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
判断是否存在 mysql 用户,不存在则创建
初始化 mysql 数据库
判断 /etc/my.cnf、sock、pid、log 文件是否存在,不存在则创建
配置 /etc/my.cnf 文件
配置为 systemctl 系统服务
配置环境变量
以 mysqld_safe 安全模式启动
安全模式启动后设置初始化密码
设置初始化密码成功后关闭 mysqld_safe 安全模式
删除旧的 sock、pid、log 文件,并重新创建
以 systemctl 系统服务启动 mysql
检查防火墙是否已启动,如果启动则开放端口
#!/bin/bash #脚本用法,脚本名字后面加上参数install/check/rm,作用分别为安装mysql、检查服务器是否已安装mysql、删除已安装的mysql(包括数据目录) #不加参数默认执行install, 比如安装mysql执行: /bin/bash auto_mysql_5.7.sh 或者 /bin/bash auto_mysql_5.7.sh install #MYSQL数据目录,指定目录后脚本会自动检查并创建目录,最好放在数据盘分区 DATADIR=/data/mysql/data #MYSQL安装目录 DATABASE=/usr/local/mysql #mysql默认端口 MYSQL_PORT=3306 #MYSQL管理员root默认密码 MYSQL_PASSWORD="CWCcwy123!" #一些mysql文件 LOG_ERROR=/var/log/mysqld_error.log PID_FILE=/tmp/mysqld SOCKET_FILE=/tmp/mysql.sock #MySQL压缩包所在目录 FILE_PATH=/root/mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz check_err() { if [ $? -ne 0 ]; then echo "ERROR: $1" exit 1 fi } install() { #检查并关闭selinux CHECK_SELINUX=`getenforce` if [[ $CHECK_SELINUX != "Disabled" ]];then #临时关闭selinux setenforce 0 #配置文件关闭selinux,下次服务器重启后生效 sed -i '/^SELINUX=/s@.*@SELINUX=disabled@' /etc/selinux/config fi #检查是否已安装过MariaDB rpm -qa | grep -i mariadb | xargs rpm -e --nodeps #判断是否存在mysql用户 if id -u mysql >/dev/null 2>&1; then userdel -r mysql fi #检查是否存在netstat命令,没有则安装 type netstat &> /dev/null if [ $? -ne 0 ]; then yum install net-tools -y fi #检查进程是否已启动 netstat -tlnp | grep ":${MYSQL_PORT} " if [ $? -eq 0 ];then echo "${MYSQL_PORT}端口已启动,请检查!" exit 1 fi [ ! -d $DATABASE ] && mkdir -p $DATABASE check_err "$DATABASE is exist!" [ ! -d $DATADIR ] && mkdir -p $DATADIR check_err "$DATADIR is exist!" # 注: --strip-components 1 解压至下一级目录,若为2则解压至下下级目录 tar -xvzf ${FILE_PATH} --strip-components 1 -C ${DATABASE} [ `id -u mysql` ] || useradd -r -s /sbin/nologin mysql check_err "USER mysql is exist!" chown -R mysql:mysql $DATADIR chown -R mysql:mysql $DATABASE #初始化mysql数据库 $DATABASE/bin/mysqld --initialize-insecure --user=mysql --basedir=${DATABASE} --datadir=${DATADIR} check_err "初始化mysql失败!" if [ ! -f "/etc/my.cnf" ];then touch /etc/my.cnf fi if [ ! -f "${SOCKET_FILE}" ];then touch ${SOCKET_FILE} chown -R mysql:mysql ${SOCKET_FILE} chmod 755 ${SOCKET_FILE} fi if [ ! -f "${LOG_ERROR}" ];then touch ${LOG_ERROR} chown -R mysql:mysql ${LOG_ERROR} chmod 755 ${LOG_ERROR} fi [ ! -d "${PID_FILE}" ] && mkdir -p ${PID_FILE} if [ ! -f ${PID_FILE}/mysqld.pid ];then touch ${PID_FILE}/mysqld.pid chown -R mysql:mysql ${PID_FILE} chown -R mysql:mysql ${PID_FILE}/mysqld.pid chmod 755 ${PID_FILE}/mysqld.pid fi cat > /etc/my.cnf << EOF [mysqld] server-id=1 datadir=${DATADIR} basedir=${DATABASE} socket=${SOCKET_FILE} user=mysql port=${MYSQL_PORT} lower_case_table_names=1 max_allowed_packet=400M log_bin_trust_function_creators=1 max_connections=1000 sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' innodb_buffer_pool_size=1G innodb_log_file_size=256M innodb_flush_log_at_trx_commit=1 innodb_flush_method=O_DIRECT innodb_autoinc_lock_mode=2 innodb_flush_log_at_trx_commit=0 #binlog expire_logs_days=7 log-bin=mysql-bin binlog-format=ROW #指定默认引擎 disabled_storage_engines=MyISAM,BLACKHOLE,FEDERATED,CSV,ARCHIVE default_storage_engine=innodb symbolic-links=0 init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake [mysql_safe] log-error=${LOG_ERROR} pid-file=${PID_FILE}/mysqld.pid [client] default-character-set=utf8 [mysql] default-character-set=utf8 EOF #配置为系统服务并开机启动 [ -f /usr/lib/systemd/system/mysql.service ] && rm -rf /usr/lib/systemd/system/mysql.service touch /usr/lib/systemd/system/mysql.service && chmod +x /usr/lib/systemd/system/mysql.service cat > /usr/lib/systemd/system/mysql.service << EOF [Unit] Description=MySQL Server Documentation=man:mysqld(8) Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html After=network.target After=syslog.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql ExecStart=$DATABASE/bin/mysqld --defaults-file=/etc/my.cnf LimitNOFILE = 50000 EOF #重载配置文件 systemctl daemon-reload if ! grep -q mysql /etc/profile; then sed -i "/unset i/iPATH=\$PATH:$DATABASE/bin" /etc/profile; fi source /etc/profile #以安全模式启动 $DATABASE/bin/mysqld_safe --datadir="$DATADIR" --pid-file="${PID_FILE}/mysqld.pid" --socket="${SOCKET_FILE}" --log-error="${LOG_ERROR}" >/dev/null & #每2s检查一次监听端口是否启动,总共检查15次共30秒,超时未启动则退出 CHECK_RESULT=1 CHECK_COUNT=15 #while [ ${CHECK_RESULT} -ne 0 -a ${CHECK_COUNT} -gt 0 ];do while [ ${CHECK_RESULT} -ne 0 ];do if [ ${CHECK_COUNT} -gt 0 ];then echo "等待监听端口启动..." sleep 2 netstat -tlnp | grep ":${MYSQL_PORT} " CHECK_RESULT=$? CHECK_COUNT=$((${CHECK_COUNT}-1)) else echo "ERROR: 启动${MYSQL_PORT}端口超时,请检查!" exit 1 fi done echo "安全模式启动成功!" #安全模式启动后设置初始化密码 $DATABASE/bin/mysqladmin -uroot password ${MYSQL_PASSWORD} #设置初始化密码后关闭安全模式 mysqld_pid=`cat "${PID_FILE}/mysqld.pid"` echo "mysqld_pid: $mysqld_pid" if (kill -0 $mysqld_pid 2>/dev/null) then echo "Shutting down MySQL Safe Mode......" kill $mysqld_pid fi #每2s检查一次 mysqld_safe 是否关闭,总共检查15次共30秒,超时未启动则退出 CHECK_RESULT=1 CHECK_COUNT=15 while [ ${CHECK_RESULT} -ne 0 ];do if [ ${CHECK_COUNT} -gt 0 ];then echo "等待 mysqld_safe 进程关闭..." sleep 2 CHECK_RESULT=`ps aux | grep mysqld_safe | grep -v "grep" | wc -l` CHECK_COUNT=$((${CHECK_COUNT}-1)) else echo "ERROR: 等待 mysqld_safe 进程关闭超时,请检查!" exit 1 fi done echo "安全模式关闭成功!" rm -f "${SOCKET_FILE}" if [ ! -f "${SOCKET_FILE}" ];then touch ${SOCKET_FILE} chown -R mysql:mysql ${SOCKET_FILE} chmod 755 ${SOCKET_FILE} fi rm -f "${LOG_ERROR}" if [ ! -f "${LOG_ERROR}" ];then touch ${LOG_ERROR} chown -R mysql:mysql ${LOG_ERROR} chmod 755 ${LOG_ERROR} fi rm -f "${PID_FILE}/mysqld.pid" [ ! -d "${PID_FILE}" ] && mkdir -p ${PID_FILE} if [ ! -f ${PID_FILE}/mysqld.pid ];then touch ${PID_FILE}/mysqld.pid chown -R mysql:mysql ${PID_FILE} chown -R mysql:mysql ${PID_FILE}/mysqld.pid chmod 755 ${PID_FILE}/mysqld.pid fi #以系统服务启动 systemctl enable mysql systemctl start mysql check_err "启动mysql失败!" #检查防火墙是否已启动,如果启动则开放端口 service firewalld status > /dev/null if [ $? -eq 0 ];then firewall-cmd --add-port=${MYSQL_PORT}/tcp --permanent firewall-cmd --reload fi #每2s检查一次监听端口是否启动,总共检查15次共30秒,超时未启动则退出 CHECK_RESULT=1 CHECK_COUNT=15 #while [ ${CHECK_RESULT} -ne 0 -a ${CHECK_COUNT} -gt 0 ];do while [ ${CHECK_RESULT} -ne 0 ];do if [ ${CHECK_COUNT} -gt 0 ];then echo "等待监听端口启动..." sleep 2 netstat -tlnp | grep ":${MYSQL_PORT} " CHECK_RESULT=$? CHECK_COUNT=$((${CHECK_COUNT}-1)) else echo "ERROR: 启动${MYSQL_PORT}端口超时,请检查!" exit 1 fi done echo "启动成功!" source /etc/profile } usage () { echo " " echo "Please input check to check datadir." echo "Please input rm to delete datadir." echo "Please input install to install mysql." echo " " } INPUT_ACTIVE=$1 ACTIVE=${INPUT_ACTIVE:=install} case ${ACTIVE} in check) [ -d $DATABASE ] && echo "$DATABASE is exist!" [ -d $DATADIR ] && echo "$DATADIR is exist!" [ -f /etc/my.cnf ] && echo "my.cnf is exist!" exit 0 ;; rm) systemctl stop mysql [ -d $DATABASE ] && rm -rf $DATABASE [ -d $DATADIR ] && rm -rf $DATADIR [ -f /etc/my.cnf ] && rm -f /etc/my.cnf #判断是否存在mysql用户 if id -u mysql >/dev/null 2>&1; then echo "user mysql exists" userdel -r mysql fi [ -f ${PID_FILE}/mysqld.pid ] && rm -f ${PID_FILE}/mysqld.pid [ -f ${LOG_ERROR} ] && rm -f ${LOG_ERROR} [ -f ${SOCKET_FILE} ] && rm -f ${SOCKET_FILE} ;; install) install ;; *) usage ;; esac