#!/bin/bash
#Nifty: Install Percona Server 5.7 from Percona repository. CentOS 7
function ce() {
  echo $(tput setaf 4)$@$(tput sgr0)
}

NUMCPU=`cat /proc/cpuinfo  |grep "cpu MHz" |wc -l`
ce "Updating the system packages..."
yum update -y 

ce "Installing EPEL"
yum install epel-release yum-utils -y

ce "Installing Percona repository"
rpm -Uvh https://repo.percona.com/yum/percona-release-latest.noarch.rpm

percona-release setup ps57

ce "Installing required packages"
yum -y install Percona-Server-server-57 

ce "Enabling MySQL on startup"
systemctl enable mysqld

HALFMEM="$((`cat /proc/meminfo |grep MemTotal |awk '{print $2}'` / 2))K"
MYSQLPASSWORD=`strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 16 | tr -d '\n'`

ce "Wiping MySQL data"
mkdir -p /var/lib/mysql 
rm -rf /var/lib/mysql/*
mysqld --initialize >> /var/log/mysqld.log 2>&1
chown -R mysql:mysql /var/lib/mysql
sleep 2
TEMPMYSQLPASSWORD=$(grep "A temporary password is generated for root" /var/log/mysqld.log | awk '{print $11;}' | tail -1)

ce "Setting up MySQL (my.cnf)"
# Tune MySQL
cat <<EOF > /etc/my.cnf
# Client configs
[mysql]
port                           = 3306
socket                         = /var/lib/mysql/mysql.sock

# Server configs
[mysqld]
user                           = mysql
default-storage-engine         = InnoDB
socket                         = /var/lib/mysql/mysql.sock
pid-file                       = /var/lib/mysql/mysql.pid
datadir                        = /var/lib/mysql

# MyISAM
key-buffer-size                = 32M

# Safety things
max-allowed-packet             = 16M
max-connect-errors             = 1000000
skip-name-resolve

# Cache and limitations
tmp-table-size                 = 32M
max-heap-table-size            = 32M
query-cache-type               = 0
query-cache-size               = 0
max-connections                = 500
thread-cache-size              = 50
open-files-limit               = 20000
table-definition-cache         = 4096
table-open-cache               = 5000

# InnoDB Settings (Buffer pool default: 50% memory)
innodb-flush-method            = O_DIRECT
innodb-log-files-in-group      = 2
innodb-log-file-size           = 128M
innodb-flush-log-at-trx-commit = 2
innodb-file-per-table          = 1
innodb-buffer-pool-size        = $HALFMEM

# Ignore lost+found
ignore-db-dir                  = lost+found
EOF

ce "Set OOM Kill value for MySQL to -500"
mkdir -p /etc/systemd/system/mysqld.service.d
cat <<EOF > /etc/systemd/system/mysqld.service.d/override.conf
[Service]
OOMScoreAdjust=-500
EOF
systemctl daemon-reload

ce "Starting MySQL"
systemctl start mysqld


ce "Prepping MySQL"
mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQLPASSWORD';" -uroot -p"$TEMPMYSQLPASSWORD" --connect-expired-password
mysql -e "UPDATE mysql.user SET authentication_string=PASSWORD('$MYSQLPASSWORD') WHERE User='root';" -uroot -p"$MYSQLPASSWORD" 
mysql -e "FLUSH PRIVILEGES;" -uroot -p"$MYSQLPASSWORD"
mysql -e "DROP DATABASE IF EXISTS test;"  -uroot -p"$MYSQLPASSWORD"
mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';" -uroot -p"$MYSQLPASSWORD"
echo $MYSQLPASSWORD > /root/.mysql
chmod 400 /root/.mysql

ce "Setting up MySQL backup"
curl https://nifty.sh/install/universal/mysqlbackup.sh |sh

echo -en "\n\n\n"
ce "All done!"
