#!/bin/bash
# @Author: HanWei
# @Date: 2020-09-09 10:23:42
# @Last Modified by: HanWei
# @Last Modified time: 2020-09-09 15:28:44
# @E-mail: han_wei_95@163.com
#
#!/bin/bash
echo "脚本使用说明:
1.安装前请提前打开本文件修改基础信息配置部分
2.如果数据库端口不是默认的3306端口,请全局替换3306为你想要的端口
3.提前将二进制安装文件放到root目录下
4.记录好设置的密码
5.安装完成后默认不配置mysql,myssqldump等命令到bash,请自行配置
6.准备名称为mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz的二进制包
7.其他版本安装需要全局替换报名(不包含扩展名)"
#基础信息配置
#1.安装目录
install_DIR=/usr/local/mysql
#2.数据目录
data_DIR=/data/mysql/data
#3.日志目录
log_DIR=/data/mysql/logs
#4.备份目录
bak_DIR=/data/backup
mkdir -p /data/backup
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
if [ -d $data_DIR ]; then
echo "数据目录已经存在,选择是否备份"
read -p "数据目录已经存在,选择是否备份[y/n]": keys
case "$keys" in
[yY][eE][sS]|y|Y)
echo "开始备份........"
mv $data_DIR $bak_DIR/data_`date +%Y%m%d%H%M%S`
echo "数据目录已经备份到${bak_DIR}......."
ls -ld $bak_DIR/data_*
keys="y"
;;
[nN][oO]|N|n )
echo "不备份........"
rm -rf $data_DIR
keys="n"
;;
*)
echo "输入有误,即将退出......."
exit 1
esac
else
echo "mysql datadir $data_DIR is not exist,this is ok!"
fi
os_version=`uname -r|cut -d . -f 6`
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
echo "############################# 检查mysql端口使用情况#######################"
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
}
function InstallMySQL()
{
echo -e "\n"
echo "############################# 开始安装mysql########################"
if [ -s /etc/selinux/config ]; then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
fi
setenforce 0
groupadd mysql -g 512
useradd -u 512 -g mysql -s /sbin/nologin -d /home/mysql mysql
#mysql directory configuration
if [ -d /root/mysql-5.7.31-linux-glibc2.12-x86_64 ]; then
rm -rf /root/mysql-5.7.31-linux-glibc2.12-x86_64
fi
echo -e "正在解压二进制文件..........\n请耐心等待安装完成..........."
tar -zxvf /root/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
if [ -d $install_DIR ]; then
echo "备份安装目录"
mv $install_DIR $bak_DIR/mysql_`date +%Y%m%d%H%M%S`
echo "已备份至 $bak_DIR 目录"
fi
mv /root/mysql-5.7.31-linux-glibc2.12-x86_64 $install_DIR
chown -R mysql:mysql $install_DIR
mkdir -p $data_DIR
chown -R mysql:mysql $data_DIR
echo "directory $data_DIR created succeed!"
if [ -d $log_DIR ]; then
mkdir -p $log_DIR
mv $log_DIR $bak_DIR/logs_`date +%Y%m%d%H%M%S`
chown -R mysql:mysql $log_DIR
echo "directory $log_DIR created succeed!"
else
mkdir -p $log_DIR
chown -R mysql:mysql $log_DIR
echo "directory $log_DIR created succeed!"
fi
mv /etc/my.cnf /etc/my.cnf.`date +%Y%m%d%H%M%S`
#mysql配置文件
echo "正在生成my.cnf "
cat >>/etc/my.cnf <<EOF
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
user = mysql
socket = /tmp/mysql.sock
pid-file = $install_DIR/mysql.pid
basedir= $install_DIR
datadir = $data_DIR
# 禁用local-infile选项
local-infile = 0
# 禁用symbolic-links选项
skip_symbolic_links = yes
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
# 用于索引的缓冲区大小
key_buffer_size = 512M
max_allowed_packet = 100G
# 表缓存(最大不要超过2048)
table_open_cache = 384
# 每个线程排序的缓冲大小
sort_buffer_size = 1024K
net_buffer_length = 4K
# 读入缓冲区大小
read_buffer_size = 1024K
# 随机读取缓冲区大小
read_rnd_buffer_size = 256K
myisam_sort_buffer_size = 8M
# 线程池大小
thread_cache_size = 128
# 查询缓存,不开启请设为0
query_cache_size = 256M
# 临时表缓存大小
tmp_table_size = 512M
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
explicit_defaults_for_timestamp = true
#skip-name-resolve
# 连接数设置
max_connections = 1000
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
slow_query_log=1
slow-query-log-file=$log_DIR/mysql-slow.log
long_query_time=3
#log_queries_not_using_indexes=on
early-plugin-load = ""
innodb_data_home_dir = $data_DIR
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = $data_DIR
innodb_buffer_pool_size = 128M
# Innodb日志缓冲区大小
innodb_log_file_size = 64M
# Innodb日志缓冲区大小
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 1
innodb_write_io_threads = 1
[mysqldump]
quick
max_allowed_packet = 500M
[mysql]
no-auto-rehash
[myisamchk]
# 用于索引的缓冲区大小
key_buffer_size = 32M
# 每个线程排序的缓冲大小
sort_buffer_size = 1024K
# 读入缓冲区大小
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
log-error=$log_DIR/error.log
EOF
touch $log_DIR/error.log
touch $log_DIR/mysql-slow.log
chown -R mysql.mysql $data_DIR/../
echo "正在初始化数据库................."
$install_DIR/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql --datadir=$data_DIR --basedir=$install_DIR --initialize-insecure
cp $install_DIR/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 2345 mysqld on
/etc/init.d/mysqld start
/etc/init.d/mysqld status
cat >> /etc/profile <<EOF
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${install_DIR}/lib
EOF
source /etc/profile
$install_DIR/bin/mysqladmin -u root password $mysqlrootpwd
/etc/init.d/mysqld restart
echo "============================MySQL 5.7.31 install completed========================="
echo -e "\n"
}
function CheckInstall_result()
{
echo "=====================================检查安装结果 ==================================="
ismysql=""
echo "Checking..."
if [ -s $install_DIR/bin/mysql ] && [ -s $install_DIR/bin/mysqld_safe ] && [ -s /etc/my.cnf ] && [ `netstat -ntl| awk '{ print $4}' |grep '3306'|awk -F: '{ print $4}'`="3306" ] ; then
echo "MySQL: OK"
ismysql="ok"
else
echo "Error: $install_DIR 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"
mysqlrootpwd="MANAGER"
echo -e "Please input the root password for mysql:"
read -p "(Default password: MANAGER):" mysqlrootpwd
if [ "$mysqlrootpwd" = "" ]; then
mysqlrootpwd="MANAGER"
fi
echo "MySQL root password:$mysqlrootpwd"
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 "########### 数据库安装脚本: MySQL on Redhat/CentOS 6 or 7 Linux ################ "
cd /root
check_install_mysql_environment
if_select_install
InstallMySQL
CheckInstall_result