#!/bin/bash

#Store the password
mkdir -p /data/password

version=$(cat /etc/lsb-release)
if [[ $version =~ "22.04" ]]; then
	echo "ubuntu_version is 22.04"
	apt-get update 
	apt-get install -y mysql-server

elif [[ $version =~ "20.04" ]]; then
	echo "ubuntu_version is 20.04"
    apt-get update 
	apt-get install -y mysql-server
else
	echo "not support version"
fi

release_version=$(cat /etc/os-release)
if [[ $release_version =~ "centos" && $release_version =~ "7" ]]; then
	echo "centos_version is 7"
    wget http://speedtest-c001.cloudclusters.io/mysql80-community-release-el7-11.noarch.rpm
    rpm -ivh mysql80-community-release-el7-11.noarch.rpm
	yum clean all
	yum makecache
	yum -y install  mysql-community-server 
	systemctl start mysqld
else
	echo "not support version"
fi


# Generate a random password
generate_root_password() {
    mysql_root_password=$(openssl rand -base64 12 | tr -d '/+=')
    mysql_root_password="${mysql_root_password}!4"
    echo "$mysql_root_password"
}
# Generate a password and output it
mysql_root_password=$(generate_root_password)
echo "The randomly generated MySQL root password is：$mysql_root_password" >> /data/password/password.txt

generate_admin_password() {
    mysql_admin_password=$(openssl rand -base64 12 | tr -d '/+=')
    mysql_admin_password="${mysql_admin_password}!7"
    echo "$mysql_admin_password"
}

mysql_admin_password=$(generate_admin_password)
echo "The randomly generated MySQL admin password is：$mysql_admin_password" >> /data/password/password.txt

# config mysql
if [[ $version =~ "20.04" || $version =~ "22.04" ]]; then
# Create a new user and set a password and grant the user the appropriate permissions
mysql -e "CREATE USER 'admin'@'localhost' IDENTIFIED WITH mysql_native_password BY '$mysql_admin_password';"
mysql -e "CREATE USER 'admin'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY '$mysql_admin_password';"
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';"
mysql  -e "FLUSH PRIVILEGES;"
# update mysql root password
mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$mysql_root_password';"
fi

if [[ $release_version =~ "centos" && $release_version =~ "7" ]]; then
# Extract the root password field of MySQL from the log file
password=$(grep 'A temporary password is generated for root@localhost' /var/log/mysqld.log | awk '{print $NF}')

# Output password
echo "centOS 7 installs the root password generated by MySQL: $password" >> /data/password/password.txt
echo "centOS 7 installs the admin password generated by MySQL is：$mysql_admin_password" >> /data/password/password.txt

mysql --connect-expired-password -uroot -p${password}  -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '$password';"

# Create a new user and set a password and grant the user the appropriate permissions
mysql -uroot -p${password}  -e "CREATE USER 'admin'@'localhost' IDENTIFIED WITH mysql_native_password BY '$mysql_admin_password';"
mysql -uroot -p${password}  -e "CREATE USER 'admin'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY '$mysql_admin_password';"
mysql -uroot -p${password}  -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';"
mysql -uroot -p${password}  -e "FLUSH PRIVILEGES;"
fi


# Optimize MySQL 

total_memory=$(free --giga | awk '/^Mem:/{print $2}')

echo "Total Memory: $total_memory"

CPU_LIMIT=$(lscpu | grep '^CPU(s):' | awk '{print $2}')

echo "Cpu Core: $CPU_LIMIT"

if [[ $version =~ "20.04" || $version =~ "22.04" ]]; then

set_innodb_buffer_pool_size() {
    CONFIGFILE="/etc/mysql/mysql.conf.d/mysqld.cnf"
    if [[ $(grep "$1" $CONFIGFILE) == *$1* ]]; then
        echo
    else
        case $total_memory in
        1 | 2 | 3)
            echo "$1 = 256MB" >> $CONFIGFILE
            ;;
        4 | 5 | 6 | 7)
            echo "$1 = 512MB" >> $CONFIGFILE
            ;;
        8 | 9 | 10 | 11 | 12 | 13 | 14 | 15)
            echo "$1 = 1GB" >> $CONFIGFILE
            ;;
        16 | *)
            echo "$1 = 2GB" >> $CONFIGFILE
            ;;
        esac
    fi
}


# set max_connections
 set_max_connections() {
        case $CPU_LIMIT in
         1|2)
            echo "$1 = 500" >> $CONFIGFILE
         ;;
         3)
            echo "$1 = 1000" >> $CONFIGFILE
         ;;
         4|5)
            echo "$1 = 2000" >> $CONFIGFILE
         ;;
         6|*)
            echo "$1 = 4000" >> $CONFIGFILE
         ;;
        esac
}


set_innodb_buffer_pool_size innodb_buffer_pool_size
set_max_connections max_connections
fi

if [[ $release_version =~ "centos" && $release_version =~ "7" ]]; then
set_innodb_buffer_pool_size() {
    CONFIGFILE="/etc/my.cnf"
    if [[ $(grep "$1" $CONFIGFILE) == *$1* ]]; then
        echo
    else
        case $total_memory in
        1 | 2 | 3)
            echo "$1 = 256MB" >> $CONFIGFILE
            ;;
        4 | 5 | 6 | 7)
            echo "$1 = 512MB" >> $CONFIGFILE
            ;;
        8 | 9 | 10 | 11 | 12 | 13 | 14 | 15)
            echo "$1 = 1GB" >> $CONFIGFILE
            ;;
        16 | *)
            echo "$1 = 2GB" >> $CONFIGFILE
            ;;
        esac
    fi
}


# set max_connections
 set_max_connections() {
        case $CPU_LIMIT in
         1|2)
            echo "$1 = 500" >> $CONFIGFILE
         ;;
         3)
            echo "$1 = 1000" >> $CONFIGFILE
         ;;
         4|5)
            echo "$1 = 2000" >> $CONFIGFILE
         ;;
         6|*)
            echo "$1 = 4000" >> $CONFIGFILE
         ;;
        esac
}
set_innodb_buffer_pool_size innodb_buffer_pool_size
set_max_connections max_connections

systemctl restart mysqld
fi
