#!/bin/bash

#Store the password
mkdir -p /data/password


# 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

version=$(cat /etc/lsb-release)
if [[ $version =~ "22.04" ]]; then
	echo "ubuntu_version is 22.04"
    echo "mysql-apt-config mysql-apt-config/select-server select mysql-5.7" |  debconf-set-selections
    echo "mysql-community-server mysql-community-server/root-pass password $mysql_root_password" |  debconf-set-selections
    echo "mysql-community-server mysql-community-server/re-root-pass password $mysql_root_password" |  debconf-set-selections
    wget 165.73.252.160/mysql-apt-config_0.8.12-1_all.deb
    dpkg -i mysql-apt-config_0.8.12-1_all.deb
    apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B7B3B788A8D3785C
    apt-get update
    apt install -y mysql-server=5.7.42-1ubuntu18.04 mysql-client=5.7.42-1ubuntu18.04 mysql-community-server=5.7.42-1ubuntu18.04

elif [[ $version =~ "20.04" ]]; then
	echo "ubuntu_version is 20.04"
    echo "mysql-apt-config mysql-apt-config/select-server select mysql-5.7" |  debconf-set-selections
    echo "mysql-community-server mysql-community-server/root-pass password $mysql_root_password" |  debconf-set-selections
    echo "mysql-community-server mysql-community-server/re-root-pass password $mysql_root_password" |  debconf-set-selections
    wget 165.73.252.160/mysql-apt-config_0.8.12-1_all.deb
    dpkg -i mysql-apt-config_0.8.12-1_all.deb
    apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B7B3B788A8D3785C
    apt-get update
    apt install -y mysql-server=5.7.42-1ubuntu18.04 mysql-client=5.7.42-1ubuntu18.04 mysql-community-server=5.7.42-1ubuntu18.04
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 165.73.252.160/mysql57-community-release-el7-11.noarch.rpm
    rpm -ivh mysql57-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

# 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 -uroot -p${mysql_root_password} -e "CREATE USER 'admin'@'localhost' IDENTIFIED WITH mysql_native_password BY '$mysql_admin_password';"
mysql -uroot -p${mysql_root_password} -e "CREATE USER 'admin'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY '$mysql_admin_password';"
mysql -uroot -p${mysql_root_password} -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';"
mysql -uroot -p${mysql_root_password}  -e "FLUSH PRIVILEGES;"
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
