#!/bin/bash

##################################################
#  auth:anger                                    #
#  mail:yxmingh@hotmail.com                      #
##################################################
#  description:                                  #
#      mysql install using yum                   #
##################################################
#  version:0.1                                   #
##################################################
#  take care! script must be tested before used  #
#  TAKE CARE! SCRIPT MUST BE TESTED BEFROE USED  #
##################################################


yum install yum-utils wget expect -y
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
rpm -i mysql80-community-release-el7-3.noarch.rpm
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum-config-manager --enable mysql57-community
yum-config-manager --disable mysql80-community
yum install mysql-community-server -y

# motify my.conf

cat > /etc/my.cnf << EOF
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock

basedir = /var/lib/mysql
datadir = /var/lib/mysql
pid-file = /var/run/mysqld/mysqld.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1

character-set-server = utf8mb4

skip-name-resolve
#skip-networking
back_log = 300

max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M

thread_cache_size = 8

query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M

ft_min_word_len = 4

log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7

log_error = /var/log/mysqld.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /var/log/mysql-slow.log

performance_schema = 0
explicit_defaults_for_timestamp

skip-external-locking

default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1

interactive_timeout = 28800
wait_timeout = 28800

sql_mode = 

[mysqldump]
quick
EOF

Mem=`free -m | awk '/Mem:/{print $2}'`

sed -i "s@max_connections.*@max_connections = $((${Mem}/3))@" /etc/my.cnf
if [ ${Mem} -gt 1500 -a ${Mem} -le 2500 ]; then
    sed -i 's@^thread_cache_size.*@thread_cache_size = 16@' /etc/my.cnf
    sed -i 's@^query_cache_size.*@query_cache_size = 16M@' /etc/my.cnf
    sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 16M@' /etc/my.cnf
    sed -i 's@^key_buffer_size.*@key_buffer_size = 16M@' /etc/my.cnf
    sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 128M@' /etc/my.cnf
    sed -i 's@^tmp_table_size.*@tmp_table_size = 32M@' /etc/my.cnf
    sed -i 's@^table_open_cache.*@table_open_cache = 256@' /etc/my.cnf
elif [ ${Mem} -gt 2500 -a ${Mem} -le 3500 ]; then
    sed -i 's@^thread_cache_size.*@thread_cache_size = 32@' /etc/my.cnf
    sed -i 's@^query_cache_size.*@query_cache_size = 32M@' /etc/my.cnf
    sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 32M@' /etc/my.cnf
    sed -i 's@^key_buffer_size.*@key_buffer_size = 64M@' /etc/my.cnf
    sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 512M@' /etc/my.cnf
    sed -i 's@^tmp_table_size.*@tmp_table_size = 64M@' /etc/my.cnf
    sed -i 's@^table_open_cache.*@table_open_cache = 512@' /etc/my.cnf
elif [ ${Mem} -gt 3500 ]; then
    sed -i 's@^thread_cache_size.*@thread_cache_size = 64@' /etc/my.cnf
    sed -i 's@^query_cache_size.*@query_cache_size = 64M@' /etc/my.cnf
    sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 64M@' /etc/my.cnf
    sed -i 's@^key_buffer_size.*@key_buffer_size = 256M@' /etc/my.cnf
    sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 1024M@' /etc/my.cnf
    sed -i 's@^tmp_table_size.*@tmp_table_size = 128M@' /etc/my.cnf
    sed -i 's@^table_open_cache.*@table_open_cache = 1024@' /etc/my.cnf
fi

# my.conf 片段引用于:https://oneinstack.com/

systemctl enable mysqld
systemctl start mysqld


# find password by python
mysql_pwd_line=`cat /var/log/mysqld.log | grep "password is generated"`

# for python2
# todo : python version check
mysql_pwd="`python -c "print '$mysql_pwd_line'.split(' ')[-1]"`"

# change default password
# password policy
# add root@% 
# modify root@local and root@% password to 'password'
cat > /tmp/mysql_expect << EOF
#!/usr/bin/expect -f

set timeout -1
spawn mysql -uroot -p[lindex \$argv 0]
match_max 100000
expect  "*mysql> "
send -- "ALTER USER root@localhost IDENTIFIED BY 'KJEz#@PAtT5isWKV';"
send -- "\r"
expect  "*mysql> "
send -- "set global validate_password_policy=0;"
send -- "\r"
expect  "*mysql> "
send -- "set global validate_password_length=6;"
send -- "\r"
expect  "*mysql> "
send -- "CREATE USER root@'%' IDENTIFIED BY 'pa$$w0rd';"
send -- "\r"
expect  "*mysql> "
send -- "grant all on *.* to root@'%';"
send -- "\r"
expect  "*mysql> "
send -- "ALTER USER root@localhost IDENTIFIED BY 'pa$$w0rd';"
send -- "\r"
expect  "*mysql> "
send -- "flush privileges;"
send -- "\r"
expect  "*mysql> "
send -- "exit\r"
expect eof
EOF
chmod a+x /tmp/mysql_expect && /tmp/mysql_expect $mysql_pwd

# print info
echo -e '''
root@\e[30;1;1;43mlocalhost\e[0m password is : \e[30;1;1;43mpa$$w0rd\e[0m
root@\e[30;1;1;43m%\e[0m password is : \e[30;1;1;43mpa$$w0rd\e[0m
'''