#!/usr/bin/env bash

echo "Installing Dependencies..."
export DEBIAN_FRONTEND="noninteractive";
sudo apt-get update
sudo apt-get install -y debconf-utils vim curl
sudo debconf-set-selections <<< 'mysql-apt-config mysql-apt-config/select-server select mysql-8.0'
wget https://dev.mysql.com/get/mysql-apt-config_0.8.11-1_all.deb
sudo -E dpkg -i mysql-apt-config_0.8.11-1_all.deb
sudo apt-key adv --keyserver keys.gnupg.net --recv-keys 8C718D3B5072E1F5
sudo apt-get update

# Install MySQL 8
echo "Installing MySQL 8..."
sudo debconf-set-selections <<< 'mysql-community-server mysql-community-server/re-root-pass password Onway@123$'
sudo debconf-set-selections <<< 'myvsql-community-server mysql-community-server/root-pass password Onway@123$'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password Onway@123$'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password Onway@123$'
sudo -E apt-get -y install mysql-server

#Stop MYSQL
systemctl stop mysql
echo lower_case_table_names=1 >>  /etc/mysql/mysql.conf.d/mysqld.cnf
echo port=13306 >> /etc/mysql/mysql.conf.d/mysqld.cnf
echo '#skip-grant-tables' >> /etc/mysql/mysql.conf.d/mysqld.cnf
rm -rf /var/lib/mysql
mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
chmod -R  777 /var/lib/mysql
mysqld --defaults-file=/etc/mysql/my.cnf --initialize --user=mysql --console

#echo "Restarting MySQL..."
sudo service mysql restart

