#!/bin/bash
#**************************************************************************
#CreateDate:2020-03-07
#Author"HuoXiaoJun
#Purpose: MySQL installation script
#This installation is only for testing environments
#Binary installation database version:mysql-5.7.25-linux-glibc2.12-x86_64
#**************************************************************************
#Note: there is no default configuration my.cnf file for database versions above 5.7. You need to create a new my.cnf configuration file by yourself here.
#MySQL initialization statements above 8.0.19 are different from those under 5.7. Please refer to:./bin/mysqld --initialize-insecure --user=mysql
echo "#============================================================="
echo "# The automatic execution process of MySQL is as follows #"
echo "#============================================================="
echo "#1.Define database environment variables "
echo "#2.Create directory and users, empower and install"
echo "#3.Decompress database installation package "
echo "#4.Check whether the environment dependency package is installed. "
echo "#When performing the yum installation, if the system does not install the local installation, "
echo "#the system will prompt that the system has been installed."
echo "#5.Execute database initialization status "
echo "#6.Add the startup file of the database and name it MySQL "
echo "#7.Add the configuration file my.cnf of the database "
echo "#8.Authorized database directory "
echo "#9.Startup file authorization of database"
echo "#10.Add database startup and shutdown scripts "
echo "#11.Start MySQL database "
echo "#12.Judge whether the database is started successfully "
echo "#13.Configure MySQL database commands to use environment variables"
echo "#14.Change the password of the database again "
echo "#============================================================= "
echo "Define database environment variables "
echo "#*************************************************************"
mysql_user=mysql
mysql_port=$1
mysql_passwd='Mysql@123'
mysql_version=mysql8.0.24
mysoft_path=/u01/software
mysql_install_path=/u01/mysql/$1
mysql_path=${mysql_install_path}/${mysql_version}
mysql_data_path=${mysql_path}/data
mysql_sata=${mysql_install_path}/statmysql_scripts
mysql_pack=mysql-8.0.24-linux-glibc2.12-x86_64.tar.xz
mysql_pack_tar_name=mysql-8.0.24-linux-glibc2.12-x86_64
echo "*************************************************************"
echo "Create directory and users, empower and install"
#useradd -g ${mysql_user} -r -s /sbin/nologin -M -d /home/${mysql_user} ${mysql_user}
#/usr/sbin/useradd -r -g ${mysql_user} ${mysql_user}Either way, you can add the owner relationship of MySQL users.
mkdir -p ${mysql_install_path}
groupadd -r ${mysql_user}
if [ $? -ne 0 ]
then
echo "This user mysql is not exist!!"
else
echo "This user mysql is exist!!"
fi
#*************************************************************
useradd -g ${mysql_user} -r -s /sbin/nologin -M -d /home/${mysql_user} ${mysql_user}
cd ${mysoft_path}
if [ ! -e ${mysql_pack} ]
then
echo "install pack is not exist!!"
exit 0
fi
echo "************************************************************* "
echo "3.Decompress database installation package "
echo "*************************************************************"
tar -xvf ${mysql_pack} -C ${mysql_install_path} > /dev/null 2>&1
cd ${mysql_install_path} && mv ${mysql_pack_tar_name} ${mysql_version} && mkdir -p ${mysql_data_path}
if [ $? -ne 0 ]
then
echo -e "\033[37;31;5m[The previous operation failed.!!]\033[39;49;0m"
else
echo -e "\033[37;32;5m[The last operation was successful.!!]\033[39;49;0m"
fi
echo "************************************************************* "
echo "4.Check whether the environment dependency package is installed. "
echo "************************************************************* "
echo "When performing the yum installation, if the system does not install the local installation, "
echo "the system will prompt that the system has been installed."。
yum -y install libaio > /dev/null 2>&1
#*************************************************************
echo "************************************************************* "
echo "#5.Execute database initialization status "
echo "************************************************************* "
#There are two initialization methods for databases above version 5.7:
#First, during initialization, there is no password generation, so the password is empty: / mysqld -- initialize execute -- user = MySQL
#Second, in the initialization process, there is a password generation display, which is copied out to record, so that you can log in and use after the installation is successful: / mysqld -- initialize -- console -- user = MySQL
#Here I choose the first initialization method, which makes it easier for the script to run. Anyway, I need to change the password of the database first.
cd ${mysql_path}
./bin/mysqld --initialize-insecure --user=${mysql_user} --basedir=${mysql_path} --datadir=${mysql_data_path}
if [ $? -ne 0 ]
then
echo -e "\033[37;31;5m[MySQL initialization failed...]\033[39;49;0m"
else
echo -e "\033[37;32;5m[MySQL initialization secessful...]\033[39;49;0m"
fi
echo "*************************************************************"
echo "6.Add the startup file of the database and name it MySQL "
cp -p ${mysql_path}/support-files/mysql.server /etc/rc.d/init.d/mysql
echo "************************************************************* "
echo "#7.Add the configuration file my.cnf of the database "
echo "************************************************************* "
cd ${mysql_path}
cat >>my.cnf<<EOF
[client]
port = ${mysql_port}
socket = ${mysql_path}/socket/mysql.sock
[mysql]
prompt="\u@summer \R:\m:\s [\d]> "
no-auto-rehash
[mysqld]
user = mysql
port = ${mysql_port}
basedir = ${mysql_path}
datadir = ${mysql_data_path}
socket = ${mysql_path}/socket/mysql.sock
pid-file = ${mysql_path}/logs/mysqld.pid
character-set-server = utf8mb4
skip_name_resolve = 1
#若你的MySQL数据库主要运行在境外,请务必根据实际情况调整本参数
default_time_zone = "+8:00"
open_files_limit = 65535
back_log = 1024
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instances = 64
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 768
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
slow_query_log = 1
log_timestamps = SYSTEM
slow_query_log_file = ${mysql_path}/logs/slow.log
log-error = ${mysql_path}/logs/error.log
long_query_time = 0.1
log_queries_not_using_indexes =1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1
server-id = 3306
log-bin = ${mysql_path}/logs/mybinlog
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
#注意:MySQL 8.0开始,binlog_expire_logs_seconds选项也存在的话,会忽略expire_logs_days选项
expire_logs_days = 7
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'
binlog_format = row
binlog_checksum = 1
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
transaction_isolation = REPEATABLE-READ
#innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 1434M
innodb_buffer_pool_instances = 4
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:12M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
# 根据您的服务器IOPS能力适当调整
# 一般配普通SSD盘的话,可以调整到 10000 - 20000
# 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_flush_sync = 0
innodb_flush_neighbors = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
innodb_stats_on_metadata = 0
innodb_undo_log_truncate = 1
# some var for MySQL 8
log_error_verbosity = 3
innodb_print_ddl_logs = 1
binlog_expire_logs_seconds = 604800
#innodb_dedicated_server = 0
innodb_status_file = 1
#注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log-error文件增长较快
innodb_status_output = 0
innodb_status_output_locks = 0
#performance_schema
performance_schema = 1
performance_schema_instrument = '%memory%=on'
performance_schema_instrument = '%lock%=on'
#innodb monitor
innodb_monitor_enable="module_innodb"
innodb_monitor_enable="module_server"
innodb_monitor_enable="module_dml"
innodb_monitor_enable="module_ddl"
innodb_monitor_enable="module_trx"
innodb_monitor_enable="module_os"
innodb_monitor_enable="module_purge"
innodb_monitor_enable="module_log"
innodb_monitor_enable="module_lock"
innodb_monitor_enable="module_buffer"
innodb_monitor_enable="module_index"
innodb_monitor_enable="module_ibuf_system"
innodb_monitor_enable="module_buffer_page"
innodb_monitor_enable="module_adaptive_hash"
[mysqldump]
quick
max_allowed_packet = 32M
EOF
mkdir -p ${mysql_path}/logs
mkdir -p ${mysql_path}/undolog
chmod 775 ${mysql_path}/logs
chmod 775 ${mysql_path}/undolog
touch ${mysql_path}/logs/mysqld.pid
touch ${mysql_path}/logs/error.log
touch ${mysql_path}/logs/slow.log
chown -R mysql:mysql ${mysql_path}/logs
chown -R mysql:mysql ${mysql_path}/undolog
echo "*************************************************************"
echo "#8.Authorized database directory "
echo "************************************************************* "
mkdir -p ${mysql_path}/socket
mkdir -p ${mysql_path}/logs
mkdir -p ${mysql_path}/log_bin
chmod +x ${mysql_path}/bin/
chmod +x ${mysql_path}/my.cnf
chown -R ${mysql_user}:${mysql_user} ${mysql_path}
mv /etc/my.cnf /etc/my.cnf${mysql_port}
ln -s ${mysql_path}/my.cnf /etc/my.cnf
echo "*********mody database startupfile ************************************"
mysql_path1="mysql_path=${mysql_path}"
sed -i "46c $mysql_path1" /etc/rc.d/init.d/mysql && echo 'ok'||echo 'no'
mysql_data1="datadir=${mysql_data_path}"
sed -i "47c $mysql_data1" /etc/rc.d/init.d/mysql && echo 'ok'||echo 'no'
mysql_cnf=${mysql_path}/my.cnf
mysql_cnf1="conf=${mysql_cnf}"
sed -i "207c $mysql_cnf1" /etc/rc.d/init.d/mysql && echo 'ok'||echo 'no'
echo "*************************************************************"
echo "#9.Startup file authorization of database"
echo "************************************************************* "
chmod 755 /etc/rc.d/init.d/mysql
chown ${mysql_user}:${mysql_user} /etc/rc.d/init.d/mysql
echo "*************************************************************"
echo "#10.Add database startup and shutdown scripts "
echo "************************************************************* "
mkdir -p ${mysql_sata}
cd ${mysql_sata}
cat >>mysql_start.sh<<EOF
#!/bin/sh
nohup /etc/rc.d/init.d/mysql start >> ${mysql_path}/mysql_start.log 2>&1
exit
EOF
cat >>mysql_shutdown.sh<<EOF
#!/bin/bash
nohup /etc/rc.d/init.d/mysql stop >> ${mysql_path}/mysql_stop.log 2>&1
exit
EOF
echo "*************************************************************"
echo "#11.Start MySQL database "
echo "************************************************************* "
chmod +x mysql_start.sh mysql_shutdown.sh && sh mysql_start.sh
sleep 50
echo "************************************************************* "
echo "#12.Judge whether the database is started successfully "
echo "************************************************************* "
netstat -atunlp |grep ${mysql_port}
if [ $? -ne 0 ]
then
echo -e "\033[37;31;5m[MySQL start failure...]\033[39;49;0m"
else
echo -e "\033[37;32;5m[MySQL start secessful...]\033[39;49;0m"
fi
#*************************************************************
echo "************************************************************* "
echo "#13.Configure MySQL database commands to use environment variables"
echo "************************************************************* "
echo "PATH=$PATH:${mysql_path}/bin" >> /root/.bash_profile
source /root/.bash_profile
echo "*************************************************************"
echo "#14.Change the password of the database again "
echo "************************************************************* "
${mysql_path}/bin/mysqladmin -uroot password ${mysql_passwd} -S ${mysql_path}/socket/mysql.sock
ln -s ${mysql_path}/socket/mysql.sock /tmp/mysql${mysql_port}.sock
exit