#!/bin/bash
# Filename: install_mysql57.sh
# Revision: 1.0
# Date: 2019/07/12
# Author: sdhzdtwhm
# Description: centos7 install mysql
# 使用方法:
# ./脚本名称 安装目录 root密码 创建的应用账户 应用账户密码
# ./install_mysql57.sh /data abc123! appuser appuser@123!
# uninstall:rm -rf /data/ /usr/local/bin/mysql* /tmp/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz /tmp/init.sql /etc/init.d/mysqld && userdel -r mysql
base_dir=$1
mysql_root_password=$2
mysql_app_user=$3
mysql_app_password=$4
host_ip=$(python -c "import socket;print([(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])")
down_url=https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
package_name=mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
package_dir=mysql-5.7.26-linux-glibc2.12-x86_64
#1.config_selinux
function config_selinux(){
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
}
#2.down_sofware
function down_software(){
cd /tmp/
curl -s -O $down_url
}
#3.config_mysql
function config_mysql(){
mkdir -p $base_dir
tar -zxf /tmp/$package_name -C $base_dir
ln -s $base_dir/$package_dir $base_dir/mysql
ln -s $base_dir/$package_dir/bin/mysql /usr/local/bin/mysql
ln -s $base_dir/$package_dir/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
ln -s $base_dir/$package_dir/bin/mysqldump /usr/local/bin/mysqldump
ln -s $base_dir/$package_dir/bin/mysqld /usr/local/bin/mysqld
ln -s $base_dir/$package_dir/bin/mysql_secure_installation /usr/local/bin/mysql_secure_installation
#adduser
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
#mkdir
/bin/mkdir -p $base_dir/mysql/data
/bin/mkdir -p $base_dir/mysql/logs
#touch
touch $base_dir/mysql/logs/mysql-error.log
/bin/chown -R mysql:mysql $base_dir/$package_dir
}
#4.config_mycnf
function config_mycnf(){
if [ -f /etc/my.cnf ]; then
mv /etc/my.cnf /etc/my.cnf_bak
fi
cat > $base_dir/mysql/my.cnf << EOF
[mysqld]
server-id =`echo $host_ip |cut -d "." -f 4`
report-host=$host_ip
port = 3306
user = mysql
basedir = $base_dir/mysql
datadir = $base_dir/mysql/data
tmpdir = $base_dir/mysql/
socket = /tmp/mysql.sock
pid-file = $base_dir/mysql/mysql.pid
skip-external-locking
skip-name-resolve
wait-timeout = 28800
connect_timeout = 20
interactive_timeout =28800
#binlog
log_bin_trust_function_creators = 1
log_bin = $base_dir/mysql/logs/mysql-bin
binlog_format = row
expire-logs-days = 7
binlog_cache_size = 2M
#master_master_config
#auto_increment_offset = 1
#auto_increment_increment = 2
#error_log
log_error = $base_dir/mysql/logs/mysql-error.log
#slow_log
slow_query_log = 1
slow_query_log_file = $base_dir/mysql/logs/mysql-slow.log
long_query_time = 5
#relay_log
relay_log = $base_dir/mysql/logs/mysql-relay-bin
relay_log_recovery = 1
#innodb
innodb_write_io_threads = 32
innodb_read_io_threads = 32
innodb_buffer_pool_size = 1G
innodb_file_per_table = 1
innodb_log_file_size = 50M
innodb_log_buffer_size = 64M
innodb_flush_log_at_trx_commit = 1
max_connections = 1024
max_connect_errors = 1000
lower_case_table_names = 1
key_buffer_size = 64M
table_open_cache = 6144
table_definition_cache = 4096
sort_buffer_size = 512K
read_buffer_size = 512K
join_buffer_size = 512K
tmp_table_size = 64M
max_heap_table_size = 64M
max_allowed_packet = 1024M
sql_mode = STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
explicit_defaults_for_timestamp=true
[mysqldump]
quick
max_allowed_packet = 1024M
[mysqld_safe]
open_files_limit = 65535
EOF
}
#5.init_mysql
function init_mysql(){
mysqld --initialize-insecure --user=mysql --datadir=$base_dir/mysql/data --basedir=$base_dir/mysql
}
#6.config_service
function config_service(){
if [ -f /etc/my.cnf ]; then
mv /etc/init.d/mysqld /etc/init.d/mysqld_bak
fi
cp $base_dir/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -i "46s#basedir=#basedir=$base_dir/mysql#g" /etc/init.d/mysqld
sed -i "47s#datadir=#datadir=$base_dir/mysql/data#g" /etc/init.d/mysqld
service mysqld start
chkconfig mysqld on
}
#7.config_mysql_user
function config_mysql_user(){
cat > /tmp/init.sql <<EOF
use mysql;
update user set authentication_string = password('$mysql_root_password'), password_expired = 'N', password_last_changed = now() where user = 'root';
grant all privileges on *.* to $mysql_app_user@"%" identified by "$mysql_app_password";
delete from mysql.user where user = '';
FLUSH PRIVILEGES;
EOF
/usr/local/bin/mysql -e "source /tmp/init.sql"
}
#8.main
function main(){
config_selinux;
down_software;
config_mysql;
if [ $? = 0 ];then
config_mycnf;
if [ $? = 0 ];then
init_mysql;
if [ $? = 0 ];then
config_service;
if [ $? = 0 ];then
config_mysql_user;
else
echo "config_mysql_user is error!";
fi
else
echo "init_mysql is error!";
fi
else
echo "config_mycnf is error!";
fi
else
echo "config_mysql is error!";
fi
echo "install mysql is success!"
}
main