MySQL 学习笔记 linux下的安装和删除脚本

MySQL linux下的安装和删除脚本

  • 一、删除
  • 二、安装
    • 1、下载tar包
    • 2、脚本安装

一、删除

会删除所有有关mysql的目录和文件,谨慎使用

#! /bin/bash

num_soft=0
num_dir=0
arr_mysql_software=()
arr_mysql_dir=()

tempfile=`mktemp uni_mysql.XXXXXX`
echo $tempfile

rpm -qa|grep -i mysql>$tempfile

cat $tempfile

while read line
do
	if [ "${line}" ]
	then
	    arr_mysql_software[num_soft]=${line}
	    echo -n "${arr_mysql_software[num_soft]}"
	else
	    echo "it's null"
	fi
	let num_soft++
done<$tempfile
echo $num_soft

#--------get mysql dir-------
cat /dev/null>$tempfile

find / -name mysql>$tempfile

cat $tempfile

while read line
do
	if [ ${line} ]
	then
	    arr_mysql_dir[num_dir]=${line}
	    echo ${arr_mysql_dir[num_dir]}
	else
	    echo "mysql dir is null"
	fi
	num_dir=$[$num_dir+1]
done<$tempfile
echo $num_dir

rm -f $tempfile

echo "it's over\n"

echo "mysql software nuber:"
echo "${#arr_mysql_software[@]}"
echo "${#arr_mysql_dir[*]}"

echo "------uninstall------"
echo "------stop mysql service-------"
service mysqld stop|cat>/dev/null
echo $?

echo "------delete mysql software-------"
for (( a = 0; a < num_soft; a++ ))
do
#	echo $a
#	echo ${arr_mysql_software[a]}
	rpm -ev ${arr_mysql_software[a]} --nodeps|cat>/dev/null
done
echo $?

echo "-----delete mysql dir-------"
for (( a=0;a<num_dir;a++ ))
do
#	echo $a
#	echo ${arr_mysql_dir[a]}
	rm -rf ${arr_mysql_dir[a]}
done
echo $?

echo "-----delete mysql cnf------"
#find /etc -name my.cnf|cat
rm -rf /etc/my.cnf
echo $?

find / -name mysql|cat

echo "-----uninstall mysql over--------"

二、安装

1、下载tar包

链接:https://pan.baidu.com/s/1UTnXbvhjX6sTC1abES8h_A
提取码:z4s5

名称 校验码
CRC32 AE1FB0AE
MD5 08A3B385DB2F151598017B63FBCB6C43
SHA-1 144A674B6AF6F6C7F8B1C982A1FF7AC0C9F9D245

2、脚本安装

调试中 仅用于centos 6

#! /bin/bash

echo 
echo \$@:$@
echo \$#:$#
echo \$?:$?
echo

#check parameters number
if [[ $# < 2 ]]
then
        echo 
        echo ----------------------------------------------------
        echo example:$0
        echo -e "\t-f mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz"
        echo -e "\t-u mysql_test"
        echo -e "\t-p root" 
        echo -e "\t-P 3307"
        echo ----------------------------------------------------
        echo
        exit 1
fi

tar_name=
user=mysql
password=123
port=3306

#getopts
while getopts f:u:p:P: opt
do
        case $opt in
        f)
        if [ -f $OPTARG ]
        then
                tar_name=$OPTARG
        else
                echo mysql-version.os.tar.gz not found!!!
                exit 1
        fi
                ;;
        u)
        echo $OPTARG
        user=$OPTARG;;
        p)
        echo $OPTARG
        password=$OPTARG;;
        P)
        echo $OPTARG
        port=$OPTARG;;
        ?)
        echo unrecognized parameter...
        exit 1
        esac
done

echo
echo -------------------------------------
echo exec_file:$0
echo tar_name:$tar_name
echo password:$password
echo port:$port
echo -------------------------------------
echo

######################################################################
# 0:creat a new dir
# 1:dir exsits
makedir() {
        if [[ -d $1 ]]
        then
                echo 1
        else
                mkdir -p $1
                echo 0
        fi
}

mkdir_test() {
test_dir=/test_dir/dir

echo test_dir:`makedir $test_dir`
echo tset_dir:`makedir $test_dir`
mkdir $test_dir                 #file exsits and output error message
mkdir $test_dir>/dev/null       #file exsits and output error message

mkdir $test_dir 2>/dev/null     #file exsits and output error to /dev/null

#echo `rmdir $test_dir`
}

#mkdir_test

#####################################################################
tempfile_test() {
tempfile=`mktemp /err.XXXX`

echo $tempfile

cd $test_dir

tempfile=`mktemp /err.XXXX`

echo $tempfile

tempfile=`mktemp err.XXXX`

echo $tempfile
}

#tempfile_test

#####################################################################

#exit 0

#create all dir variables
app_path=/app_test
file_name=${tar_name%%.tar*}
prefixdir=`echo $tar_name|cut -c 1-12`
basedir=/usr/local/$user
datadir=$basedir/data
bindir=$basedir/bin
socket=/tmp/$user.sock

tempfile=`mktemp /err.XXXX`

group=$user

server_path=/etc/init.d
server_name=${user}d
echo $server_name

#create user and group
if [[ -z `cat /etc/passwd|grep $user` ]]
then
        adduser $user -s /sbin/nologin -M
fi

#if [ -d $app_path ]
#then
#       echo exsits $app_path
#else
#       mkdir $app_path
#fi

makedir $app_path

tar xzvf $tar_name -C $app_path>/dev/null
cd $app_path
mv $file_name $prefixdir
ln -s $app_path/$prefixdir $basedir

sed -i "s#/usr/local/mysql#$basedir#g" $basedir/support-files/mysql.server $bindir/mysqld_safe
sed -i "s/user='mysql'/user='$user'/g" $bindir/mysqld_safe
sed -i "s#lockdir/mysql#lockdir/$user#g" $basedir/support-files/mysql.server

cp $basedir/support-files/mysql.server $server_path/$server_name

echo -e "export PATH='$bindir:\$PATH'">/etc/profile.d/${user}.sh

#create my.cnf
touch $basedir/my.cnf

#create data directory
cat>>$basedir/my.cnf<<EOF
[mysqld]
basedir=$basedir
datadir=$datadir
log_error=$user.err
socket=$socket
pid-file=$user.pid
port=$port
server_id=$port
EOF

chown -R $group.$user $basedir
#chown -R $group.$user $basedir/

$bindir/mysqld --initialize  --basedir=$basedir --datadir=$datadir --port=$port --user=$user 2>$tempfile 1>/dev/null
password_init=`grep password < $tempfile`
password_in=${password_init##*localhost: }

echo origin_password:$password_in
echo $password_in>$tempfile
service $server_name start

#alter user password
$bindir/mysqladmin -uroot -p$password_in password $password 
#$bindir/mysql --connect-expired-password -uroot -p -P $port -S $socket -e "alter user root@'localhost' identified by '$password';flush privileges;"<$tempfile 2>/dev/null
echo $?

#install over and echo all information
rm -rf /$tempfile

source /etc/profile

echo
echo service:$server_name
echo user:$user
echo socket:$socket
echo passwd=$password
echo port=$port
echo 
echo install over

你可能感兴趣的:(mysql)