#!/bin/bash


OLD_MYSQL=(mysql MySQL mariadb)
for item in ${OLD_MYSQL[@]}
do
  findVar=$(rpm -qa | grep -w ${item} | awk '{print$1}')
  echo $findVar
  rpm -e --nodeps $findVar
done


cat >> /etc/yum.repos.d/MariaDB.repo <<EOF
# MariaDB 10.5 CentOS repository list - created 2021-01-06 06:01 UTC
# http://downloads.mariadb.org/mariadb/repositories/
# 已经替换为国内的中科大的yum源，选择10.5版本
[mariadb]
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.5/centos7-amd64
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

MariaDBVersion=$(yum list MariaDB-server --showduplicates | grep -w MariaDB | sort -r | awk 'NR==1{print$2}' | cut -d "-" -f1)
echo "MariaDB的最新版本为：$MariaDBVersion"
rpm --import https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB

sudo yum install -y MariaDB-server-$MariaDBVersion MariaDB-client-$MariaDBVersion

echo "
n
y
v2ryStr@ngPa.ss
v2ryStr@ngPa.ss
y
y


" | mysql_secure_installation

# 配置MariaDB-server的相关属性
mv /etc/my.cnf.d/server.conf /etc/my.cnf.d/server.conf_back

cat >/etc/my.cnf.d/server.conf <<EOF
[mysqld]
binlog_format = row
secure_file_priv=''
log-bin=/vdb1/data/mariadb/mysql-bin
expire_logs_days=5
lower_case_table_names=1
server_id = 123
skip-character-set-client-handshake=true
character-set-client-handshake = FALSE
character_set_server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
datadir=/data/mariadb
log-error=/data/mariadb/mariadb.log
slow_query_log=on
slow_query_log_file=/data/mariadb/slow_query_log.log
long_query_time=1
EOF
systemctl restart mariadb

# mysql -u root -p
# 创建相应的用户名及密码
# create user username@localhost identified by 'password';
#授予外网登陆权限
# grant all privileges on *.* to username@'%' identified by 'password';