#!/bin/bash
USER='wikiuser'
PASS='werty123!==1'
DBNAME='mediawiki'
USER_PASS='0305'


echo 'Installing software'
sudo apt-get update > /dev/null
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password ${PASS}"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password ${PASS}"
sudo apt-get -y install mysql-server cloud-guest-utils > /dev/null

LOCAL_IP=$(ec2metadata --local-ipv4)

echo "Create db"
sudo mysql --user=root --password=${PASS} -e "CREATE DATABASE IF NOT EXISTS ${DBNAME}"
echo "Create user"
sudo mysql --user=root --password=${PASS} -e "CREATE USER ${USER}@'%' IDENTIFIED BY '${USER_PASS}';"
echo "Grant privileges"
sudo mysql --user=root --password=${PASS} -e "GRANT ALL PRIVILEGES ON ${DBNAME}.* TO ${USER}@'%';"
sudo mysql --user=root --password=${PASS} -e "FLUSH PRIVILEGES;"
#sudo replace "127.0.0.1" ${LOCAL_IP} -- /etc/mysql/my.cnf
sudo replace "127.0.0.1" "0.0.0.0" -- /etc/mysql/my.cnf
sudo service mysql restart
