###### 自动安装数据库脚本root密码MANAGER将脚本和安装包放在/root目录即可###############
######数据库目录/data/mysql############
######数据目录/data/mysql/data############
######慢日志目录/data/slowlog############
######端口号默认3306其余参数按需自行修改############
##################
##################
#!/bin/bash
# Check if user is root
if [ $(id -u) != "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.6.36 on Redhat/CentOS Linux "
echo "========================================================================="
cur_dir=$(pwd)
#set mysql root password
echo "==========================="
mysqlrootpwd="MANAGER"
echo -e "Please input the root password of mysql:"
read -p "(Default password: MANAGER):" mysqlrootpwd
if [ "$mysqlrootpwd" = "" ]; then
mysqlrootpwd="MANAGER"
fi
echo "==========================="
echo "MySQL root password:$mysqlrootpwd"
echo "==========================="
#which MySQL Version do you want to install?
echo "==========================="
isinstallmysql56="n"
echo "Install MySQL 5.6.36,Please input y"
read -p "(Please input y , n):" isinstallmysql56
case "$isinstallmysql56" in
y|Y|Yes|YES|yes|yES|yEs|YeS|yeS)
echo "You will install MySQL 5.6.36"
isinstallmysql56="y"
;;
*)
echo "INPUT error,You will exit install MySQL 5.6.36"
isinstallmysql56="n"
exit
esac
get_char()
{
SAVEDSTTY=`stty -g`
stty -echo
stty cbreak
#dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty $SAVEDSTTY
}
echo ""
echo "Press any key to start...or Press Ctrl+c to cancel"
char=`get_char`
# Initialize the installation related content.
function InitInstall()
{
cat /etc/issue
uname -a
MemTotal=`free -m | grep Mem | awk ‘{print $2}‘`
echo -e "\n Memory is: ${MemTotal} MB "
#Set timezone
#rm -rf /etc/localtime
#ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#Delete Old mariadb program
MARIA=`rpm -qa|grep mariadb`
yum -y remove $MARIA
#Disable SeLinux
if [ -s /etc/selinux/config ]; then
sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g‘ /etc/selinux/config
fi
setenforce 0
}
#Installation of depend on and optimization options.
function InstallDependsAndOpt()
{
cd $cur_dir
cat >>/etc/security/limits.conf<<EOF
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF
echo "fs.file-max=65535" >> /etc/sysctl.conf
}
#Install MySQL
function InstallMySQL56()
{
echo "============================Install MySQL 5.6.36=================================="
cd $cur_dir
#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.6.36 installing…………========================="
#mysql directory configuration
mkdir -p /data/
tar xvf /root/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
mv /root/mysql-5.6.36-linux-glibc2.5-x86_64 /data/mysql
groupadd mysql -g 512
useradd -u 512 -g mysql -s /sbin/nologin -d /home/mysql mysql
touch /data/mysql/mysqld.pid
touch /data/mysql/error.log
mkdir -p /data/slowlog
chown -R mysql:mysql /data/mysql
chown -R mysql:mysql /data/slowlog
#edit /etc/my.cnf
SERVERID=`ifconfig eno1 | grep "inet " | awk ‘{ print $2}‘| awk -F. ‘{ print $3$4}‘`
cat >>/etc/my.cnf<<EOF
[client]
port=3306
socket=/tmp/mysql.sock
[mysql]
no-auto-rehash
default-character-set=utf8
[mysqld]
federated
default-storage-engine=INNODB
port=3306
character-set-server=utf8
socket=/tmp/mysql.sock
basedir=/data/mysql
datadir=/data/mysql/data
explicit_defaults_for_timestamp=true
long_query_time=1
slow_query_log =1
slow_query_log_file=/data/slowlog/slow.log
log-bin=mysql-bin
server-id=$SERVERID
innodb_buffer_pool_size=512M
innodb_data_file_path=ibdata1:10M:autoextend
sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER
log-bin-trust-function-creators=1
[mysqld_safe]
log-error=/data/mysql/error.log
EOF
/data/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/data --basedir=/data/mysql
cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 2345 mysqld on
cat >> /etc/ld.so.conf.d/mysql-x86_64.conf<<EOF
/usr/local/mysql/lib
EOF
ldconfig
if [ -d "/proc/vz" ];then
ulimit -s unlimited
fi
/etc/init.d/mysqld start
/data/mysql/bin/mysqladmin -u root password $mysqlrootpwd
cat > /tmp/mysql_sec_script<<EOF
use mysql;
delete from mysql.user where user!=‘root‘ or host!=‘localhost‘;
grant all privileges on *.* to ‘vvnt‘@‘192.168.%‘ identified by ‘[email protected]‘;
drop database test;
flush privileges;
EOF
/data/mysql/bin/mysql -u root -p$mysqlrootpwd -h localhost < /tmp/mysql_sec_script
rm -f /tmp/mysql_sec_script
#/etc/init.d/mysqld restart
echo ‘PATH=/data/mysql/bin/:$PATH‘ >>/etc/profile
source /etc/profile
echo "============================MySQL 5.6.36 install completed========================="
}
function CheckInstall()
{
echo "===================================== Check install ==================================="
clear
ismysql=""
echo "Checking..."
if [ -s /data/mysql/bin/mysql ] && [ -s /data/mysql/bin/mysqld_safe ] && [ -s /etc/my.cnf ]; then
echo "MySQL: OK"
ismysql="ok"
else
echo "Error: /data/mysql not found!!!MySQL install failed."
fi
if [ "$ismysql" = "ok" ]; then
echo "Install MySQL 5.6.36 completed! enjoy it."
echo "========================================================================="
netstat -ntl
else
echo "Sorry,Failed to install MySQL!"
echo "You can tail /root/mysql-install.log from your server."
fi
}
#The installation log
InitInstall 2>&1 | tee /root/mysql-install.log
InstallDependsAndOpt 2>&1 | tee -a /root/mysql-install.log
InstallMySQL56 > /dev/null
CheckInstall 2>&1 | tee -a /root/mysql-install.log
#deletion of installation packages and script files
find /root -type f -name ‘mysql*‘ -exec rm -rf {} \;
原文地址:https://www.cnblogs.com/qiming0322/p/10133163.html