如何在CentOS 8/RHEL 8上安装Mariadb 10.4服务器

时间:2019-08-20 17:58:23  来源:igfitidea点击:

自从MariaDB从MySQL分离后,已成为RHEL/CentOS中的默认RDBMS数据库。

在本文中,我们将在CentOS 8/RHEL 8上安装Mariadb 10.04服务器。
我们将通过创建一个shell脚本并执行它来完成所有步骤。

在CentOS 8/RHEL 8上安装MariaDB 10.4服务器的步骤

创建一个简单的bash脚本来安装MariaDB服务器。

vi install_mariadb_10.04_centos8.sh

脚本内容如下:

#/bin/bash
#
# Install MariaDB Server version 10.4 on CentOS 8.
# Blog: https://theitroad.local

# Declare a variable for new mariadb root password. If it is not given, then script won‘t run and will show help.
#

_root_new_passwd=${1?Require to give new root password, use -n }

# create new yum mariadb repo file and move to /etc/yum.repos.d dir
cat <<EOF>mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos8-amd64
module_hotfixes=1
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF
sudo mv mariadb.repo /etc/yum.repos.d/

#Install MariaDB Server
sudo dnf install -y MariaDB-server

# Enable as well as Start the mariadb service
sudo systemctl enable --now mariadb

## Resetting root password, removing database called 'test' and any related tables to it
sudo mariadb <<_EOF_
ALTER USER root@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD("$_root_new_passwd");
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%';
FLUSH PRIVILEGES;
FLUSH PRIVILEGES;
_EOF_
###
echo "Installation finished"

脚本的主要功能是:

  1. 创建mariadb yum repo文件。

  2. 通过dnf命令安装mariadb服务器。

  3. 启用mariadb服务,并设置在开机启动时自启动。

  4. 最后,重置root的密码,并删除“test”数据库。

修改脚本文件的权限

chmod +x install_mariadb_10.04_centos8.sh

运行脚本

脚本的第1个参数是MariaDB的root密码。

sh install_mariadb_10.04_centos8.sh MyNewRootPassword

登录MariaDB服务器

运行以下命令在MariaDB服务器中以root用户身份登录

mariadb -u root -p

或者

mysql -u root -p

管理MariaDB服务

检查MariaDB服务的状态。

sudo systemctl status mariadb

启动MariaDB服务。

sudo systemctl start mariadb

停止MariaDB服务。

sudo systemctl stop mariadb

重新启动MariaDB服务。

sudo systemctl restart mariadb

设置MariaDB服务在系统启动时启动服务。

sudo systemctl enable mariadb

禁止MariaDB服务在启动时启动。

sudo systemctl disable mariadb