#!/bin/bash

# Update the system
echo "Updating the system..."
sudo yum update -y

# Install the MySQL repository
echo "Installing MySQL repository..."
sudo yum localinstall -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

# Enable the MySQL repository
echo "Enabling MySQL repository..."
sudo yum-config-manager --enable mysql80-community

# Install MySQL server
echo "Installing MySQL server..."
sudo yum install -y mysql-server

# Start MySQL service
echo "Starting MySQL service..."
sudo systemctl start mysqld

# Enable MySQL to start on boot
echo "Enabling MySQL to start on boot..."
sudo systemctl enable mysqld

# Get the temporary MySQL root password
TEMP_PASS=$(sudo grep 'temporary password' /var/log/mysqld.log | tail -n 1 | awk '{print $NF}')
echo "Temporary MySQL root password: $TEMP_PASS"

# Secure MySQL installation (you can modify the commands for automation if desired)
echo "Securing MySQL installation..."
sudo mysql_secure_installation <<EOF

y
$TEMP_PASS
newpassword
newpassword
y
y
y
y
EOF

# Output MySQL status
echo "MySQL installation completed successfully."

# You can log into MySQL by running:
echo "You can log into MySQL with: mysql -u root -p"