#!/usr/bin/env bash

TEMP_PATH=$(mktemp -d)
NAME=mysql
VERSION="5.7.29"
mysql_root_pass=${1:-"Admin@8848"}

sed -n -e '1,/^exit 0$/!p' $0 > ${TEMP_PATH}/${NAME}-${VERSION}.tar.gz 2>/dev/null

cd ${TEMP_PATH}

tar axvf ${NAME}-${VERSION}.tar.gz >/dev/null

yum -y localinstall ./*.rpm

rm -rf ${TEMP_PATH}

[[ -d "/data/mysql/db" ]]  || install -o mysql -g mysql -d /data/mysql/db
[[ -d "/data/mysql/log" ]] || install -o mysql -g mysql -d /data/mysql/log

cat >/etc/my.cnf<<EOF
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = UTF8

[mysqld]
user = mysql
port = 3306
bind-address = 0.0.0.0
character-set-server = UTF8
collation-server = utf8_general_ci
datadir = /data/mysql/db
socket = /tmp/mysql.sock
pid-file = /var/run/mysqld/mysqld.pid
skip-name-resolve
sql_mode = NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
symbolic-links = 0

default-authentication-plugin = mysql_native_password

sort_buffer_size = 4194304
max_allowed_packet = 1073741824
group_concat_max_len = 102400
back_log = 3000
tmp_table_size = 16777216

# Logging configuration.
log_error = /data/mysql/log/error.log
# Slow query log configuration.
slow_query_log = 1
slow_query_log_file = /data/mysql/log/mysql-slow.log
long_query_time = 3
log_timestamps = system
log_bin_trust_function_creators = True

server_id = $(hostname -i | awk -F. '{print $NF}')
log_bin = mysql-bin
binlog_format = ROW
expire_logs_days = 3
max_binlog_size = 512M
max_relay_log_size =512M
slave_net_timeout = 3600

sync_binlog = 0

init-connect = 'SET NAMES utf8'
character-set-server = utf8

default-time-zone = '+08:00'
log-bin-trust-function-creators = true
sysdate-is-now = 1
auto-increment-increment = 1

explicit_defaults_for_timestamp = true

max_connections        = 8848
max_connect_errors     = 1000
table_open_cache       = 2048

#max_binlog_cache_size = 10737418240

lower_case_table_names = 0

skip-external-locking

default_storage_engine             = InnoDB
default_tmp_storage_engine         = InnoDB
internal_tmp_disk_storage_engine   = InnoDB
innodb_file_per_table              = ON
innodb_buffer_pool_size            = $(awk '/MemTotal/ { printf "%.0f \n", $2/1024*0.75 }' /proc/meminfo)
innodb_flush_log_at_trx_commit     = 2
innodb_log_files_in_group          = 2
innodb_flush_method                = O_DIRECT
innodb_log_buffer_size             = 256M
innodb_read_io_threads             = 4
innodb_write_io_threads            = 4
innodb_buffer_pool_instances       = 8
innodb_autoinc_lock_mode           = 2

interactive_timeout = 28800
wait_timeout        = 28800
net_write_timeout   = 60


# Muti-Thread slave
slave_parallel_type                         = LOGICAL_CLOCK
slave_parallel_workers                      = 8
relay_log_recovery                          = ON
master_info_repository                      = TABLE
relay_log_info_repository                   = TABLE

gtid_mode                = ON
enforce_gtid_consistency = ON
master_verify_checksum   = ON
sync_master_info         = 1


# for performance_schema
performance_schema                                                      = ON
performance_schema_consumer_global_instrumentation                      = ON
performance_schema_consumer_thread_instrumentation                      = ON
performance_schema_consumer_events_stages_current                       = ON
performance_schema_consumer_events_stages_history                       = ON
performance_schema_consumer_events_stages_history_long                  = OFF
performance_schema_consumer_statements_digest                           = ON
performance_schema_consumer_events_statements_current                   = ON
performance_schema_consumer_events_statements_history                   = ON
performance_schema_consumer_events_statements_history_long              = OFF
performance_schema_consumer_events_waits_current                        = On
performance_schema_consumer_events_waits_history                        = ON
performance_schema_consumer_events_waits_history_long                   = OFF
performance-schema-instrument                                           = 'memory/%=COUNTED'
EOF

/usr/sbin/mysqld --defaults-file=/etc/my.cnf --initialize-insecure

systemctl start mysqld && systemctl enable mysqld 2>/dev/null

sleep 2

/usr/bin/mysql -S /tmp/mysql.sock -e "SET PASSWORD = PASSWORD(\"${mysql_root_pass}\");" --skip-password -uroot

cat >~/.my.cnf<<EOF
[client]
user=root
password="${mysql_root_pass}"
socket = /tmp/mysql.sock

[mysqldump]
user=root
password="${mysql_root_pass}"
socket = /tmp/mysql.sock
EOF

mysql <<EOF
GRANT ALL ON *.* TO root@'%' IDENTIFIED BY "${mysql_root_pass}" WITH GRANT OPTION;
EOF

exit 0
