MySql教程

SHELL编程系列之二进制方式自动化安装MySQL5.6脚本

本文主要是介绍SHELL编程系列之二进制方式自动化安装MySQL5.6脚本,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文件名:mysql-install_5.6.49-x86_64-for-centos7.sh

#!/bin/bash
#
#**********************************
# Author:      wanhonron
# Date:        2021-07-01
# FileName:    mysql_install_5.6.49_x86_64_for.centos.sh
# Url:         http://www.wade.com
# Copyright(C):2021All rights reserved
#**********************************
. /etc/init.d/functions

#
# definition shell variables
#
current_dir=$(pwd)
mysql_package='mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz'
full_mysql_package=${current_dir}/${mysql_package}
mysql_data_dir="/home/mysql.data"

color='echo -e \E[01;31m'
end='\E[0m'
mysql_root_password=WDEARIA

#
# definition function of check mysql environment
#

check_mysql_env (){
# estimate  user as root
if [ $UID -ne 0 ];then
action "Current user is not root,installed is fail !" false
exit 4
fi

# estimate mysql data directory
if [ ! -d ${mysql_data_dir} ];then
echo "Mysql data dir is not existed,Will be going to build."
mkdir -p  ${mysql_data_dir}
action "MysqlDataDir is created." true
fi


cd ${current_dir}

# estimate mysql package and install directory
if [ ! -e ${mysql_package} ];then
$color "The lack of file: ${mysql_package}" $end
$color "Please put on related software in the dir: ${current_dir}" $end
exit
elif [ -e /usr/local/mysql ];then
action "Mysql database is existed,Installed is fail!" false
exit
else
return
fi

if [ -f ${full_mysql_package} ];then
echo "Installed package file is existed."
else
echo "Installed package file is not existed."
exit 4
fi
}

#
# definition function of install mysql
#

install_mysql (){
$color"Begin to install mysql database..."$end

# install related enironment
yum -y install libaio numactl-libs libaio autoconf  &> /dev/null
$color"Installing environment,Please wait."$end

cd ${current_dir}

# extracting mysql  package files
tar -zxvf ${mysql_package} -C /usr/local/src &> /dev/null
$color"Extracting files. Please wait."$end


mysql_dir=$(echo ${mysql_package} | sed -nr 's/^(.*[0-9]).*/\1/p')
ln -s /usr/local/src/${mysql_dir}/ /usr/local/mysql
chown -R root:root /usr/local/mysql/

id mysql &> /dev/null || { useradd -s /sbin/nologin -r mysql ; action "Create user:mysql.";}
chown -R mysql:mysql ${mysql_data_dir}

# add mysql environment variable
echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh

source /etc/profile.d/mysql.sh

# configurate mysql config file
cat > /etc/my.cnf <<-EOF
[mysqld]
#server-id=1
#log-bin
basedir=/usr/local/mysql
datadir=${mysql_data_dir}
socket=${mysql_data_dir}/mysql.sock
log-error=${mysql_data_dir}/mysql.log
pid-file=${mysql_data_dir}/mysql.pid
lower-case-table-names=1
port=3306
character_set_server=utf8
skip-name-resolve

[client]
socket=${mysql_data_dir}/mysql.sock
EOF

cd /usr/local/mysql/
./scripts/mysql_install_db --datadir=${mysql_data_dir} --user=mysql --bootstrap --explicit_defaults_for_timestamp=ON &> /dev/null
#mysqld --initialize --user=mysql --datadir=${mysql_data_dir}
cd /usr/local/mysql/bin
./mysqld_safe --defaults=/etc/my.cnf --socket=/home/mysql.data --user=root &
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start &> /dev/null
[ $? -ne 0 ] && { $color "Mysql start is fail,  database installed is fail, exit."$end;exit; }
#service mysqld restart
#mysqladmin -uroot password ${mysql_root_password}
#mysql_oldpassword=$(awk '/A temporary password/{print $NF}' ${mysql_data_dir}/mysql.log)
#mysqladmin -uroot -p${mysql_oldpassword} password ${mysql_root_password} &> /dev/null
action "Mysql database installed is completed." true
}

check_mysql_env
install_mysql

这篇关于SHELL编程系列之二进制方式自动化安装MySQL5.6脚本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!