一键安装MySQL8.0

#!/bin/bash
#1.上传MySQL-8.0.21.zip包
#2.执行下文所述的脚本
######数据库目录/data/mysql/data############
######数据目录/data/mysql/data##############
######默认账号:root 密码:MQHWnP28#########
######端口号默认3306其余参数按需自行修改############

function check_install_mysql_environment()
{
echo "################检查本机安装mysql的基本条件########################"
echo "Checking  user :"
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install"
    exit 1
else
    echo "user is root, this is ok!"
fi

echo "checking  os version"
if [ `uname -s`="linux" ]; then
    echo "os  is linux,this is ok!"
else
    echo "os  isnot linux,this is fail!"
    exit 1
fi

#limits.conf
cp /etc/security/limits.conf /etc/security/limits.conf.old
cat >>/etc/security/limits.conf<<EOF
*  soft  nofile  65535
*  hard  nofile  65535

*  soft  nproc  4096
*  hard  nproc  16384

*  soft  stack  1024
*  hard  stack  10240
EOF

#/etc/profile
cp /etc/profile /etc/profile.old
cat >>/etc/profile<<EOF
if [ $USER = "mysql" ]; then
        if [ $SHELL = "/bin/ksh" ]; then
              ulimit -p 16384
              ulimit -n 65535
        else
              ulimit -u 16384 -n 65535
        fi
fi
EOF
source /etc/profile

os_version=`uname -r|cut -d . -f 4`
if [ ${os_version}="el7" ] || [${os_version}="el6" ]; then
   echo "os version is el6 or el7, this is ok!"
else
   echo "os version isnot el6 or el7, this is fail!"
   exit 1
fi
port=`netstat -ntl| awk ‘{ print $4}‘ |grep ‘3306‘|awk  -F: ‘{ print $4}‘`
if [[ ${port} = "3306" ]]; then
   echo "mysql port 3306 is exist, please uninstall existed mysql or modify script , this is fail!"   
   exit 1
else
    echo "msyql port is not 3306! this is ok!"
fi
}

#Install MySQL
function InstallMySQL()
{
echo -e "\n"
echo "#############################  MySQL  installing..........########################"
#/etc/selinux/config
if [ -s /etc/selinux/config ]; then
sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g‘ /etc/selinux/config
fi
setenforce 0
#firewalld
systemctl stop firewalld
#mysql-user
groupadd mysql -g 512
useradd -u 512 -g mysql -s /sbin/nologin -d /home/mysql mysql
#mysql directory configuration
mysql1=‘mysql-community-common‘
mysql2=‘mysql-community-libs‘
mysql3=‘mysql-community-libs-compat‘
mysql4=‘ mysql-community-devel‘
mysql5=‘mysql-community-client‘
msyql6=‘ mysql-community-server‘
version=‘8.0.21‘
suffix=‘-1.el7.x86_64.rpm‘
Dir=‘/data/mysql‘

#卸载自带mysql==
for i in $mysql1 $mysql2 $mysql3 $mysql4 $mysql5 $mysql6 ‘mariadb-devel‘ ‘mariadb‘ ‘mariadb-libs‘ ‘mariadb-server‘
do
rpm -e --nodeps ${i}
done


#rpm安装mysql
unzip /root/MySQL-${version}.zip > /dev/null
for i in $mysql1 $mysql2 $mysql3 $mysql4 $mysql5 $mysql6
do
cd /root/MySQL
rpm -ivh ${i}-${version}${suffix}
done

if [ -d /data/mysql ]; then
mv $Dir ${Dir}_`date +%Y%m%d%H%M%S`
mkdir -p $Dir
chown -R mysql:mysql $Dir
echo "directory  ${Dir} created succeed!"
else
mkdir -p $Dir
chown -R mysql:mysql $Dir
echo "directory  ${Dir} created succeed!"
fi

/usr/sbin/mysqld --initialize-insecure --lower-case-table-names=1 --basedir=${Dir} --datadir=${Dir}/data
${Dir}/data/mysql_ssl_rsa_setup --datadir=${Dir}/data

chmod 644 ${Dir}/data/private_key.pem
chmod 644 ${Dir}/data/server-key.pem
chown -R mysql:mysql ${Dir}/data
#edit /etc/my.cnf
SERVERID=1000
os_version=`uname -r|cut -d . -f 4`

if [ ${os_version}="el6" ]; then
SERVERID=`ifconfig eth0|grep ‘inet‘|grep -v ‘inet6‘|awk ‘{print $2}‘|awk -F. ‘{print $4}‘`
elif [ ${os_version}="el7" ]; then
SERVERID=`ifconfig ens33|grep ‘inet‘|grep -v ‘inet6‘|awk ‘{print $2}‘|awk -F. ‘{print $4}‘`
else
   SERVERID=1000
fi

#Backup old my.cnf    rm -f /etc/my.cnf
ipaddr=`ifconfig ens33|grep ‘inet‘|grep -v ‘inet6‘|awk ‘{print $2}‘|awk -F. ‘{print $4}‘`
if [ -s /etc/my.cnf ]; then
    mv /etc/my.cnf /etc/my.cnf.bak
    touch /etc/my.cnf
else
    touch /etc/my.cnf
fi


cat >>/etc/my.cnf<<EOF
[mysqld]
server-id=1
datadir=${Dir}/data
max_connections=2000
init-connect=‘SET NAMES utf8mb4‘
character-set-server=utf8mb4
port            = 3306
socket          =/var/lib/mysql/mysql.sock
skip-external-locking
explicit_defaults_for_timestamp=true
ssl_ca  =${Dir}/data/ca.pem
ssl_cert=${Dir}/data/server-cert.pem
ssl_key =${Dir}/data/server-key.pem

gtid_mode = ON
enforce_gtid_consistency = 1

transaction_isolation = READ-COMMITTED
max_allowed_packet = 1073741824
table_open_cache = 1024
sort_buffer_size = 30M
read_buffer_size = 20M
read_rnd_buffer_size = 20M
join_buffer_size = 20M
innodb_sort_buffer_size = 33554432
innodb_read_io_threads=1   
innodb_write_io_threads=1
skip_name_resolve
innodb_use_native_aio = 1
innodb_buffer_pool_size = 1G
innodb_file_per_table = 1
event_scheduler = 1
innodb_data_home_dir =${Dir}/data
lower_case_table_names=1
slow_query_log=on
slow_query_log_file=slowquery.log
long_query_time=30
log-bin=mysql-bin
log-bin-index = mysql-bin.index
binlog_format=row
sync-binlog=1
innodb_log_file_size = 512M
innodb_log_buffer_size = 512M
log_bin_trust_function_creators = 1
#validate_password.policy=0
log-error=${Dir}/data/mysqld.log
pid-file=${Dir}/data/mysqld.pid
lower_case_table_names = 1

EOF



service mysqld start
ln -s ${Dir}/data/mysql/sock /var/lib/mysql/mysql.sock 

cat > ${Dir}/mysql_sec_script<<EOF
ALTER USER ‘root‘@‘localhost‘ IDENTIFIED WITH mysql_native_password BY ‘MQHWnP28‘;
use mysql;
update user set user.host=‘%‘ where user.user=‘root‘;
flush privileges;
EOF

mysql -u root  < ${Dir}/mysql_sec_script
rm -f ${Dir}/mysql_sec_script
service mysqld restart

echo "============================MySQL ${version} install completed========================="
echo -e "\n"
}


function CheckInstall_result()
{
echo "===================================== Check install ==================================="
ismysql=""
echo "Checking..."
ports=`netstat -ntl| awk ‘{ print $4}‘ |grep ‘3306‘|awk  -F: ‘{ print $4}‘|head -1`
if [ -s /usr/bin/mysql ] && [ -s /etc/my.cnf ] && [ ‘3306‘ = ${ports} ] ; then
  echo "MySQL: OK"
  ismysql="ok"
else
  echo "Error: /usr/local/mysql not found!!! MySQL install failed."
fi

if [ "$ismysql" = "ok" ]; then
netstat -ntl
ps -ef|grep mysql
echo "=================checked successed!checking result MySQL  completed! ================"
else
echo "Sorry,Failed to install MySQL!"
echo "You can tail /root/mysql-install.log from your server."
fi
}

function if_select_install()
{
echo -e "\n"
echo -e "=========do you want to install mysql? ========"
isinstallmysql="n"
echo "Install MySQL,Please input y"
read -p "(Please input y or  n):" isinstallmysql
    case "$isinstallmysql" in
    [yY][eE][sS]|y|Y)
    echo "You will install MySQL........"
    isinstallmysql="y"
    ;;
    [nN][oO]|N|n )  
    echo "you will exit install MySQL........"  
    isinstallmysql="n"
    exit 1
    ;;  
    *)
    echo "INPUT error,You will exit install MySQL......."
    isinstallmysql="n"
    exit 1
    esac
}

#The installation flow path
echo "###########  A tool to auto-compile & install MySQL on Redhat/CentOS 6 or 7  Linux ################ "
cd /root
check_install_mysql_environment
if_select_install
InstallMySQL
CheckInstall_result

一键安装MySQL8.0

上一篇:JavaSE:RandomAccessFile 类


下一篇:mysql主从延迟判断,监控和问题处理