#!/bin/bash
#author zfy
#date: 2019-03-28
#this scripts is to install mysql
#if you don't have a mysql package,it will automatically download the mysql-5.7.22 package
#define a function
ONE_BUTTON_OPERATION()
{
#create directory
cd /home/mysql
if [[ `pwd` == '/home/mysql' ]];then
echo -e "\nthe directory of data and log will be created ......\n"
mkdir data && mkdir log && touch log/mysql_error.log
sleep 0
chown -R mysql:mysql /home/mysql
else
echo invaild path !
exit 1
fi
#initialize mysql
/home/mysql/bin/mysqld --initialize --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data
if [[ $? == 0 ]];then
echo initialize successful
fi
grep -ir 'password is generated for root@localhost: ' log/*|tail -1|awk '{print $NF}'>>./temp_password
echo -e "--------------------------\npassword have been recorded in /home/mysql/temp_password\n--------------------------"
#configuration startup file
cp ./support-files/mysql.server /etc/init.d/mysqld
if [ $? = 0 ];then
sed -i 's/^basedir=/basedir=\/home\/mysql/' /etc/init.d/mysqld
sed -i 's/^datadir=/datadir=\/home\/mysql\/data/' /etc/init.d/mysqld
fi
chmod 755 /etc/init.d/mysqld
cp ./bin/my_print_defaults /usr/bin/
#set path
echo 'export PATH=$PATH:/home/mysql/bin' >> /etc/profile && source /etc/profile
#set my.cnf
if test -w "/etc/my.cnf";then
sleep 0
else
touch /etc/my.cnf
fi
cat >/etc/my.cnf<<EOF
[client]
port = 3306
socket = /home/mysql/lib/mysql.sock
default-character-set = utf8
[mysql]
prompt="MySQL [\d]> "
no-auto-rehash
[mysqld]
port = 3306
socket = /home/mysql/lib/mysql.sock
character-set-server = utf8
basedir = /home/mysql
datadir = /home/mysql/data
pid-file = /home/mysql/lib/mysql.pid
general_log_file = /home/mysql/log/mysql.log
log_error = /home/mysql/log/mysql_error.log
slow_query_log_file = /home/mysql/log/mysql_slow.log
user = mysql
server-id = 21
log_bin = mysql-bin
binlog_format = row
expire_logs_days = 7
EOF
chown -R mysql:mysql /etc/my.cnf
#start mysql
if [ $? = 0 ];then
echo starting mysql ......
/etc/init.d/mysqld start || service mysqld start >/dev/null
if test -n "ps aux|grep mysql|grep -v grep";then
echo mysql has been started successfully!
fi
fi
}
############### shell head #################
CURRENT_USER=$(whoami)
if [ "$CURRENT_USER" != "root" ];then
echo "please switch to root user to run this scripts"
exit
fi
#package
read -ep "Do you have mysql package? [y/n] " ACTION
case $ACTION in
y |Y |yes |YES )
cat << EOF
*********************************************************
( 1 ) Manually enter the path and name of the package:
( 2 ) Let the system search for the package itself ...
( 3 ) exit
*********************************************************
EOF
read -ep "please enter your choice : " MANUALLY_SYSTEM
case $MANUALLY_SYSTEM in
1)
read -ep "OK, please enter the path and name of mysql package (for example: /home/mysql-5.7.tar.gz): " MANUAL_PACKAGE_NAME
#useradd mysql
groups mysql>/dev/null
if [ $? = 0 ];then
sleep 0
else
groupadd mysql
useradd -r -g mysql mysql
if [ $? = 0 ];then
echo has added user mysql
fi
fi
#decompress mysql
cd /home
echo -e "\nmysql package decompressing ......"
if test -d "mysql";then
rm -rf mysql
sleep 0
mkdir mysql
fi
tar xf ${MANUAL_PACKAGE_NAME} -C /home/mysql/ --strip-components 1
if [ $? = 0 ];then
echo -e "\nhas been successfully decompressing"
else
echo decompressing error!
exit 1
fi
sleep 0
ONE_BUTTON_OPERATION
;;
2)
echo -e "\nOK,searching for mysql package ......"
find / -name 'mysql*.tar.gz'|nl
CHOICE_NUMBER=`find / -name 'mysql*.tar.gz'|wc -l`
read -p "which package will you chooise ( 1 | 2 | 3 |...), please choose a number: " CHOOSE_PACKAGE
case $CHOOSE_PACKAGE in
#first package
${CHOICE_NUMBER})
SEARCH_MYSQL_PACKAGE=`find / -name 'mysql*.tar.gz'|sed -n '1p'`
echo -e "\nOK! you have chosen ${SEARCH_MYSQL_PACKAGE} "
#useradd mysql
groups mysql>/dev/null
if [ $? = 0 ];then
sleep 0
else
groupadd mysql
useradd -r -g mysql mysql
if [ $? = 0 ];then
echo has added user mysql
fi
fi
#decompress mysql
cd /home
echo -e "\nmysql package decompressing ......"
if test -d "mysql";then
rm -rf mysql
sleep 0
mkdir mysql
fi
tar xf ${SEARCH_MYSQL_PACKAGE} -C /home/mysql/ --strip-components 1
if [ $? = 0 ];then
echo -e"-------------------------\nhas been successfully decompressing"
else
echo decompressing error!
exit 1
fi
sleep 0
ONE_BUTTON_OPERATION
;;
#second package
${CHOICE_NUMBER})
SEARCH_MYSQL_PACKAGE=`find / -name 'mysql*.tar.gz'|sed -n '2p'`
echo -e "\nOK! you have chosen ${SEARCH_MYSQL_PACKAGE} "
#useradd mysql
groups mysql>/dev/null
if [ $? = 0 ];then
sleep 0
else
groupadd mysql
useradd -r -g mysql mysql
if [ $? = 0 ];then
echo has added user mysql
fi
fi
#decompress mysql
cd /home
echo -e "------------------\nmysql package decompressing ......"
if test -d "mysql";then
rm -rf mysql
sleep 0
mkdir mysql
fi
tar xf ${SEARCH_MYSQL_PACKAGE} -C /home/mysql/ --strip-components 1
if [ $? = 0 ];then
echo -e "\nhas been successfully decompressing"
else
echo decompressing error!
exit 1
fi
sleep 0
ONE_BUTTON_OPERATION
;;
#third package
${CHOICE_NUMBER})
SEARCH_MYSQL_PACKAGE=`find / -name 'mysql*.tar.gz'|sed -n '3p'`
echo -e "\nOK! you have chosen ${SEARCH_MYSQL_PACKAGE} "
#useradd mysql
groups mysql>/dev/null
if [ $? = 0 ];then
sleep 0
else
groupadd mysql
useradd -r -g mysql mysql
if [ $? = 0 ];then
echo has added user mysql
fi
fi
#decompress mysql
cd /home
echo -e "------------------\nmysql package decompressing ......"
if test -d "mysql";then
rm -rf mysql
sleep 0
mkdir mysql
fi
tar xf ${SEARCH_MYSQL_PACKAGE} -C /home/mysql/ --strip-components 1
if [ $? = 0 ];then
echo -e "\nhas been successfully decompressing"
else
echo decompressing error!
exit 1
fi
sleep 0
ONE_BUTTON_OPERATION
;;
*)
echo "invaild choice! or If your package is larger than 3, please choose 1 at the previous level"
;;
esac
;;
3)
exit
;;
*)
echo "invaild choice!"
;;
esac
;;
n |N |no |NO )
echo "To download the mysql5.7.22 package ..."
wget -P /home https://dev.mysql.com/get/archives/mysql-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
#useradd mysql
groups mysql>/dev/null
if [ $? = 0 ];then
sleep 0
else
groupadd mysql
useradd -r -g mysql mysql
if [ $? = 0 ];then
echo -e "\nhas added user mysql"
fi
fi
#decompress mysql
cd /home
echo -e "-------------------\nmysql package decompressing ......\n---------------------"
PACKAGE=`find /home -name 'mysql*.tar.gz'`
if [[ `echo ${PACKAGE}|wc -l` > 1 ]];then
echo "number of mysql versions too many !"
exit 1
fi
sleep 0
if test -d "mysql";then
rm -rf mysql
sleep 0.5
mkdir mysql
fi
tar xf $PACKAGE -C /home/mysql/ --strip-components 1
if [ $? = 0 ];then
echo has been successfully decompressing
else
echo decompressing error!
exit 1
fi
sleep 0
ONE_BUTTON_OPERATION
;;
*)
echo "invaild choice!"
;;
esac
有些不完善的地方,后期会更新完善。。。
若有错误,欢迎留言指正,谢谢!