m-shell-scripts/scripts_shell/install_mysql_5.6old.sh

94 lines
2.4 KiB
Bash
Raw Permalink Normal View History

2025-03-19 17:38:46 +08:00
#!/bin/bash
basedir="/usr/local/mysql/" # 必须是一个不存在的目录
datadir="/home/mysql/" # 必须是一个不存在的目录
install_package="mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz"
package=`echo $install_package | sed 's/.tar.gz//g'`
name="mysql" # service $name start 需要给一个可识别的名字
install_port=3306 # 大于0的整数
# 检查
[ "$basedir" == "" ] || [ "$datadir" == "" ] && echo -e "\033[31mERROR: BASEDIR OR DATADIR IS NULL\033[0m" && exit
ls $install_package | grep "5.6" &> /dev/null
[ $? -ne 0 ] && echo -e "\033[31mERROR: VERSION MUST 5.6\033[0m" && exit
[ -d $basedir ] || [ -d $datadir ] && echo -e "\033[31mERROR: PATH IS EXIST\033[0m" && exit
[ "$name" == "" ] && echo -e "\033[31mERROR: SEVICE NAME IS NULL\033[0m" && exit
[ -f "/etc/init.d/$name" ] && echo -e "\033[31mERROR: SERVICE NAME IS EXIST\033[0m" && exit
if [ $install_port -gt 0 ]
then
echo "check success! install"
else
echo -e "\033[31mERROR: PORT ERROR \033[0m" && exit
fi
id mysql &>/dev/null
[ $? -ne 0 ] && useradd -s /sbin/nologin mysql
# 安装
tar xf $install_package
mkdir $datadir -p
mv $package $basedir
cd $basedir/scripts/
chown mysql:mysql $basedir -R
chown mysql:mysql $datadir -R
yum install autoconf libaio-devel -y
./mysql_install_db --user=mysql --datadir=$datadir --basedir=$basedir
# 生成启动脚本
cat << EOF > /etc/init.d/$name
#!/bin/bash
BASEDIR=$basedir
DATADIR=$datadir
CNF=\$BASEDIR/my.cnf
PASSWD=""
PORT=$install_port
case "\$1" in
"start")
cd \$BASEDIR
./bin/mysqld_safe --defaults-file=\$CNF --user=mysql &
;;
"stop")
\$BASEDIR/bin/mysqladmin -p\$PASSWD -h'127.0.0.1' -P\$PORT shutdown
;;
"connect")
if [ "\$PASSWD" == "" ]
then
\$BASEDIR/bin/mysql -h127.0.0.1 -P\$PORT
else
\$BASEDIR/bin/mysql -p\$PASSWD -h127.0.0.1 -P\$PORT
fi
;;
*)
echo "input start/stop/connect"
;;
esac
EOF
chmod +x /etc/init.d/$name
# 重写配置文件
cat << EOF > $basedir/my.cnf
[mysqld]
datadir=$datadir
basedir=$basedir
port=$install_port
socket=$datadir/mysql.sock
symbolic-links=0
server-id=1
log-bin=binlog
max_connections=200
# lower_case_table_names = 1
innodb_print_all_deadlocks = 1
slow_query_log
long_query_time=1
log_warnings = 0
[mysqld_safe]
log-error=$datadir/mariadb.log
pid-file=$datadir/mariadb.pid
EOF
echo -e "\033[32mINSTALL SUCCESS"
echo "INPUT service $name start | stop | connect"
echo -e "\033[33mMUST MODIFY /etc/init.d/$name \$PORT \$PASSWD\033[0m"