#!/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"
    curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup |  bash -s -- --mariadb-server-version=11.1
    apt-get update 
	apt-get install -y mariadb-server mariadb-client 

elif [[ $version =~ "20.04" ]]; then
	echo "ubuntu_version is 20.04"
	curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup |  bash -s -- --mariadb-server-version=11.1
    apt-get update 
	apt-get install -y mariadb-server mariadb-client 
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"
cat << EOF > /etc/yum.repos.d/MariaDB.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/11.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF
	yum install -y MariaDB-server MariaDB-client
	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 BY '$mysql_admin_password';"
mysql -e "CREATE USER 'admin'@'127.0.0.1' IDENTIFIED 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 BY '$mysql_root_password';"
fi

if [[ $release_version =~ "centos" && $release_version =~ "7" ]]; then

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

mysql -uroot -e "ALTER USER 'root'@'localhost' IDENTIFIED by '$mysql_root_password';"

# 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 BY '$mysql_admin_password';"
mysql -uroot -p${mysql_root_password}  -e "CREATE USER 'admin'@'127.0.0.1' IDENTIFIED 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

# 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/mariadb.conf.d/50-server.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.d/server.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
