#!/usr/bin/env bash

yum remove -y mariadb-libs-1:5.5.56-2.el7.x86_64
yum remove -y mariadb-libs.x86_64 

rpm -qa | grep -i mysql | xargs yum remove -y 

rm -rf /var/log/mysqld.log
cp -rf /var/lib/mysql /home/fly/mysql
rm -rf /var/lib/mysql
rm -rf /etc/my.cnf*

yum install -y tcl-devel
yum install -y expect

cd ./mysql8/
yum install -y mysql-community-common-8.0.33-1.el7.x86_64.rpm
yum install -y mysql-community-client-plugins-8.0.33-1.el7.x86_64.rpm
yum install -y mysql-community-libs-8.0.33-1.el7.x86_64.rpm
yum install -y mysql-community-libs-compat-8.0.33-1.el7.x86_64.rpm
yum install -y mysql-community-devel-8.0.33-1.el7.x86_64.rpm
yum install -y mysql-community-client-8.0.33-1.el7.x86_64.rpm
yum install -y mysql-community-icu-data-files-8.0.33-1.el7.x86_64.rpm
yum install -y mysql-community-server-8.0.33-1.el7.x86_64.rpm

echo "sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES" >> /etc/my.cnf
echo "event_scheduler=1" >> /etc/my.cnf

systemctl enable mysqld
systemctl restart mysqld

passwd=`grep 'A temporary password' /var/log/mysqld.log | awk '{print $NF}'`
/usr/bin/expect <<EOF
spawn mysql -uroot  -p
expect "Enter password:"
send "$passwd\r"
expect "mysql>"
send "source /dxpark/tools/mysql8_init.sql\r"
send "quit\r"
expect eof
EOF

mysql -hlocalhost -P3306 -u park -ppark123 -e "
drop database if exists parkos;  
CREATE DATABASE IF NOT EXISTS parkos default charset utf8mb4 COLLATE utf8mb4_general_ci;;
quit" 
mysql -hlocalhost -P3306 -u park -ppark123 parkos</dxpark/tools/parkos.sql
