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




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

ce "Installing tools"
apt-get install -y gnupg2 debconf-utils binutils

export DEBIAN_FRONTEND=noninteractive
MYSQLPASSWORD=`strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 16 | tr -d '\n'`

ce "Installing percona repository"
cd /tmp
wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb
dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb
percona-release setup ps57
apt-get update
rm -rf percona-release_latest.$(lsb_release -sc)_all.deb

debconf-set-selections <<< "percona-server-server-5.7 percona-server-server/root_password password $MYSQLPASSWORD"
debconf-set-selections <<< "percona-server-server-5.7 percona-server-server/root_password_again password $MYSQLPASSWORD"
echo $MYSQLPASSWORD > /root/.mysql


ce "Installing required packages"
apt-get install -y percona-server-server-5.7

ce "Enabling MySQL on startup"
systemctl enable mysql
systemctl stop mysql
sleep 2 
rm -rf /var/lib/mysql/ib_logfile*

HALFMEM="$((`cat /proc/meminfo |grep MemTotal |awk '{print $2}'` / 2))K"

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


# Server configs
[mysqld]
user                           = mysql
default-storage-engine         = InnoDB
socket                         = /var/run/mysqld/mysqld.sock
pid-file                       = /var/run/mysqld/mysqld.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 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!"
