#!/usr/bin/env bash

echo 
echo "*** configure MySQL ***"
echo 

yum -y install mariadb-server

systemctl enable  mariadb
systemctl restart mariadb

/usr/bin/mysqladmin -u root password 'passw0rd'

#echo "DELETE FROM user WHERE Host = 'thirdlane.local' and user = 'root';" | /usr/bin/mysql --user=root --password=passw0rd mysql

# add account for service scripts
#echo "GRANT ALL PRIVILEGES ON *.* TO 'localroot'@'localhost';" | /usr/bin/mysql --user=root --password=passw0rd mysql

echo mysql -uroot -ppassw0rd pbxconf >  /usr/local/sbin/mydb
chmod +x /usr/local/sbin/mydb

# create pid dir
mkdir /var/run/mariadb; chown mysql. -R /var/run/mariadb
echo "d /var/run/mariadb   710 mysql mysql" > /etc/tmpfiles.d/mariadb.conf

# save original MySQL config and replace it with customized

mv /etc/my.cnf /etc/my.cnf.orig

cat >> /etc/my.cnf <<-EOF
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
pid-file=/run/mariadb/mariadb.pid
user=mysql
symbolic-links=0

sql_mode=NO_ENGINE_SUBSTITUTION

max_allowed_packet = 32M
max_connections = 200
query_cache_size=64M
thread_cache_size=32
tmp_table_size=64M
max_heap_table_size=16M
sort_buffer_size=2M
table_cache=1024
key_buffer_size=32M
wait_timeout=2678400
myisam_max_sort_file_size=4294967296

innodb_buffer_pool_size=32M
innodb_file_per_table=1

sort_buffer_size = 5M
read_rnd_buffer_size = 1M

innodb_default_row_format=dynamic
innodb_strict_mode=OFF
innodb_page_size=32k

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
socket=/var/lib/mysql/mysql.sock

EOF

#sed -i 's|^\[mysqld\]|[mysqld]\n\nsql_mode=NO_ENGINE_SUBSTITUTION\n|' /etc/my.cnf

systemctl stop mariadb
rm -f /var/lib/mysql/ibdata* /var/lib/mysql/ib_logfile*
sed -i /lib/systemd/system/mariadb.service -e "s/^PrivateTmp=.*$/PrivateTmp=false/"
systemctl daemon-reload
systemctl start mariadb

