MySQL5.7一键安装脚本

本文写作说明:
MySQL的配置文件my.cnf参数调整参考来源于iMySQL | 老叶茶馆
此脚本是本博主生产环境中经常使用的进行批量部署mysql服务时采用。脚本比较陋,写此处主要是方便自己使用时及时查阅,同时也希望可以帮助初级的DBA同学,脚本中如有写法不当之处欢迎拍砖,但是也希望不喜勿喷。

脚本文件内容:

#!/bin/bash

[ -f /etc/init.d/functions ]&& . /etc/init.d/functions

###Check if user is root
if [ $UID -ne 0 ]; then
    echo "Error: You must be root to run this script, please use root to install"
    exit 1
fi

clear
echo "========================================================================="
echo "A tool to auto-compile & install MySQL 5.7.24 on Redhat/CentOS Linux "
echo "========================================================================="

#set mysql root password
    echo "==========================="
        mysqlrootpwd="$1"
        if [ "$1" = "" ]; then
                mysqlrootpwd="rootmysql"
        fi

#which MySQL Version do you want to install?
echo "==========================="

    isinstallmysql57="5.7.24"
    echo "Install MySQL 5.7.24,Please input y"
    read -p "(Please input y , n):" 
# Initialize  the installation related content.
    #Delete Old Mysql program
    rpm -qa|grep mysql
    rpm -e mysql

cat >>/etc/security/limits.conf<> /etc/sysctl.conf

echo "============================Install MySQL 5.7.24=================================="

#Backup old my.cnf
#rm -f /etc/my.cnf
if [ -s /etc/my.cnf ]; then
    mv /etc/my.cnf /etc/my.cnf.`date +%Y%m%d%H%M%S`.bak
fi
echo "============================MySQL 5.7.24 installing…………========================="

##define mysql directory configuration variable
Datadir=/data/mysql/data
Binlogdir=/data/mysql/binlog
Logdir=/data/mysql/logs

##yum install  devel and wget mysql
yum install numactl 
##/usr/bin/wget -P /tmp https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
sleep 2
tar xf /tmp/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
ln -s /usr/local/mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql
grep mysql /etc/passwd
RETVAL=$?
if [ $RETVAL -ne 0 ];then
   useradd mysql -s /sbin/nologin -M
     action "mysql user added successfully" /bin/true
  else
     action " $(echo -e "\e[31;47;5m mysql user already exists\e[0m")" /bin/false
fi

if [ ! -d "$Datadir" ]
 then 
   mkdir -p  /data/mysql/data
fi

if [ ! -d "$Binlogdir" ]
 then
   mkdir -p  /data/mysql/binlog
fi

if [ ! -d "$Logdir" ]
 then
   mkdir -p  /data/mysql/logs
fi

chown -R mysql:mysql /data/mysql
chown -R mysql:mysql /usr/local/mysql

#edit /etc/my.cnf
#SERVERID=`ifconfig eth0 | grep "inet addr" | awk '{ print $2}'| awk -F. '{ print $3$4}'`
cat >>/etc/my.cnf< /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
echo "============================MySQL 5.7.24 install completed========================="
ps -eo start,cmd,pid|grep mysql
/usr/local/mysql/bin/mysqladmin -uroot -p"$Pass" password $mysqlrootpwd

[root@localhost ~]# sh install.mysql.sh mysqlroot
[root@localhost ~]# source /etc/profile.d/mysql.sh

说明: mysqlroot 为安装脚本修改MySQL服务默认密码后的新的密码
[root@localhost ~]# mysql -uroot -p'mysqlroot' -e "select now()"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------------+
| now() |
+---------------------+
| 2018-11-09 13:40:41 |
+---------------------+
[root@localhost ~]#

到此处MySQL服务启动成功

转载于:https://blog.51cto.com/wujianwei/2314973

你可能感兴趣的:(MySQL5.7一键安装脚本)