如何在 Ubuntu 和 CentOS 上安装和配置 MariaDB

安装与配置 MariaDB:一份详尽指南

本文将引导您完成 MariaDB 的安装与配置过程。MariaDB 是一款广受欢迎的开源关系型数据库管理系统 (RDBMS),它是 MySQL 的一个社区驱动的分支,同时提供商业支持选项。尽管与 MySQL 高度兼容,MariaDB 仍存在一些差异。

本文将详细介绍在 Ubuntu 20.x 和 CentOS 7.x/8.x 系统上安装和设置 MariaDB 的步骤。此外,我们还将探讨一些优化 MariaDB 性能和安全性的最佳实践。

Ubuntu 系统上的 MariaDB 安装

在 Ubuntu 20.x 上,您可以直接通过其默认软件仓库获取 MariaDB。我们将使用 apt 包管理器完成安装。

首先,请更新 apt 软件包索引:

sudo apt update

更新索引后,运行以下命令安装 MariaDB 服务器及其依赖:

sudo apt install mariadb-server

在安装过程中,您可能需要输入 ‘Y’ 来确认操作。

CentOS 7.x 系统上的 MariaDB 安装

在 CentOS 7.x 中,默认仓库提供的 MariaDB 版本为 5.x。为获取最新版本,我们需要配置额外的 yum 仓库。

MariaDB 官方提供了一个简单的脚本 mariadb_repo_setup 来完成此操作。在 CentOS 7.x 系统上执行以下命令:

sudo yum install wget
wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
chmod +x mariadb_repo_setup
sudo ./mariadb_repo_setup

此脚本将自动配置 yum 仓库,以便安装 MariaDB 的最新版本(当前为 10.x)。

或者,您可以手动配置 yum 仓库。创建一个新的 repo 文件:

sudo vi /etc/yum.repos.d/MariaDB.repo

将以下内容添加到文件中并保存:

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.5/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

现在,使用以下命令安装 MariaDB 服务器:

sudo yum install MariaDB-server

在安装过程中,输入 ‘y’ 确认。

CentOS 8.x 系统上的 MariaDB 安装

对于 CentOS 8.x,默认仓库提供的 MariaDB 版本为 10.3 或更高版本。 您可以使用 dnf 命令直接安装:

sudo dnf install mariadb-server

如果需要安装最新的可用版本,可以参考上述 CentOS 7.x 的手动安装方法。

启动 MariaDB 服务

在 Ubuntu 系统上,安装完成后,MariaDB 服务会自动运行。但在 CentOS 系统上,您需要手动启动并启用该服务。

无论您使用 Ubuntu 还是 CentOS,以下命令都可用于启动 MariaDB 服务,并设置开机自启动,然后验证服务状态:

sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service
sudo systemctl status mariadb.service

您可能会看到类似下面的输出:


● mariadb.service - MariaDB 10.5.8 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/mariadb.service.d
           └─migrated-from-my.cnf-settings.conf
   Active: active (running) since Thu 2020-12-31 13:20:04 IST; 13s ago
     Docs: man:mariadbd(8)
           https://mariadb.com/kb/en/library/systemd/
 Main PID: 13521 (mariadbd)
   Status: "Taking your SQL requests now..."
   CGroup: /system.slice/mariadb.service
           └─13521 /usr/sbin/mariadbd

Dec 31 13:20:04 centos7vm mariadbd[13521]: 2020-12-31 13:20:04 0 [Note] InnoDB: 10.5.8 started; log sequence number 45118; transaction id 20
Dec 31 13:20:04 centos7vm mariadbd[13521]: 2020-12-31 13:20:04 0 [Note] Plugin 'FEEDBACK' is disabled.
Dec 31 13:20:04 centos7vm mariadbd[13521]: 2020-12-31 13:20:04 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
Dec 31 13:20:04 centos7vm mariadbd[13521]: 2020-12-31 13:20:04 0 [Note] InnoDB: Buffer pool(s) load completed at 201231 13:20:04
Dec 31 13:20:04 centos7vm mariadbd[13521]: 2020-12-31 13:20:04 0 [Note] Server socket created on IP: '::'.
Dec 31 13:20:04 centos7vm mariadbd[13521]: 2020-12-31 13:20:04 0 [Note] Reading of all Master_info entries succeeded
Dec 31 13:20:04 centos7vm mariadbd[13521]: 2020-12-31 13:20:04 0 [Note] Added new Master_info '' to hash table
Dec 31 13:20:04 centos7vm mariadbd[13521]: 2020-12-31 13:20:04 0 [Note] /usr/sbin/mariadbd: ready for connections.
Dec 31 13:20:04 centos7vm mariadbd[13521]: Version: '10.5.8-MariaDB'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MariaDB Server
Dec 31 13:20:04 centos7vm systemd[1]: Started MariaDB 10.5.8 database server.

MariaDB 安全加固

为了保护您的 MariaDB 安装,您应采取以下步骤:设置 root 密码、禁用远程 root 登录、删除测试数据库和匿名用户,并重新加载权限。

使用以下命令执行安全加固:

sudo mysql_secure_installation

按照默认提示操作即可,除非您有特殊需求。您可能会看到类似以下输出:


NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

由于我们使用了系统身份验证,因此我们没有为 MariaDB 设置单独的 root 密码。如有需要,您可以随时设置单独的 root 密码。

验证 MariaDB 安装

要验证 MariaDB 安装,运行以下命令(输入您在执行 mysql_secure_installation 时设置的密码,或者使用您的系统 root 凭据):

sudo mysqladmin -u root -p version

输出示例:


mysqladmin  Ver 9.1 Distrib 10.5.8-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Server version          10.5.8-MariaDB
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 53 min 17 sec

Threads: 2  Questions: 77  Slow queries: 0  Opens: 20  Open tables: 14  Queries per second avg: 0.024

您可以创建一个新的管理员帐户来代替 root。以下命令演示了如何创建名为 ‘admin’ 的管理员帐户:

sudo mariadb
GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit

请将 'password' 替换为您希望为管理员账户设置的密码。

现在,可以使用新的管理员账户验证访问权限:

mysqladmin -u admin -p version

输入您在上一步中设置的密码。

操作系统优化

安装并保护 MariaDB 后,为了获得最佳性能,您需要调整操作系统和数据库配置。具体的调整将取决于您的系统配置、使用类型、用户数量等因素。

Linux 内核设置 – IO 调度程序

MariaDB 推荐的 IO 调度器是 noopdeadline。您可以使用以下命令检查当前的调度器:

sudo cat /sys/block/sda/queue/scheduler

您可以使用以下命令临时更改调度器:

sudo echo noop > /sys/block/sda/queue/scheduler

要使其持久化,您需要在 GRUB 的配置文件(例如 /etc/default/grub)中配置,重建 GRUB,并重启系统:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash elevator=noop"

资源限制 – 打开文件限制

Linux 会限制每个进程可以打开的文件描述符的数量。对于数据库系统,这个限制很容易被超过,影响性能。默认情况下,这个限制可能是 1024。

要增加限制,您可以添加以下内容到 /etc/security/limits.conf 文件:

mysql soft nofile 65535
mysql hard nofile 65535

需要重启系统使 mysql 用户生效。 您可以使用以下命令检查:

ulimit -Sn
ulimit -Hn

资源限制 – 核心文件大小

类似地,Linux 也会限制核心文件的大小。默认情况下,软限制为 0,这将有效地禁用核心文件的生成。 要启用核心文件生成,您可以修改 `/etc/security/limits.conf`:

mysql soft core unlimited
mysql hard core unlimited

在系统重启后,可以使用 `ulimit` 命令查看新设置:

ulimit -Sc
ulimit -Hc

配置交换性(Swappiness)

Linux 中的 Swappiness 值决定了系统将页面从内存交换到磁盘的可能性。默认值通常设置为 60,可以通过以下方式查看:

sysctl vm.swappiness

较低的值意味着较低的交换可能性。对于仅运行 MariaDB 的数据库服务器,建议将此值设置为 0 或 1 以避免使用交换。请谨慎设置 swappiness 为 0,因为在高内存使用或 I/O 负载的情况下,内核可能会导致内存不足 (OOM) 进程终止。

为了持久化更改,将 vm.swappiness = 1 添加到 /etc/sysctl.conf 文件中:

vm.swappiness = 1

您可以使用以下命令立即更改此设置:

sysctl -w vm.swappiness=1

文件系统优化

对于 MariaDB,推荐的文件系统是 ext4、XFS 和 Btrfs。您应该禁用文件访问时间的记录,以提高性能。您可以使用 noatime 选项挂载文件系统,或者将其添加到 /etc/fstab 文件中的挂载选项。

数据库优化

MariaDB 提供了许多可调整的参数,您可以根据需要进行自定义。

MariaDB 的主要配置文件是 my.cnf。在 Ubuntu 上,此文件位于:

/etc/mysql/my.cnf

在 CentOS 上,它位于:

/etc/my.cnf

您可以参考官方文档,了解配置文件的详细信息。

此外,根据您使用的引擎类型(MyISAM, InnoDB 或 XtraDB)需要进行不同的优化。InnoDB 的 `innodb_buffer_pool_size` 应设置为大约 80% 的可用内存。

其他一些重要的可调参数包括:

innodb_log_file_size
innodb_flush_method
innodb_thread_sleep_delay
innodb_adaptive_max_sleep_delay
innodb_buffer_pool_instances
innodb_buffer_pool_size
innodb_max_dirty_pages_pct_lwm
innodb_read_ahead_threshold
innodb_thread_concurrency

请参考 官方文档 获取更详细的信息。您也可以查看 这份指南 了解所有可用的优化选项。

如果您对学习 SQL 和 NoSQL 感兴趣,请参考相关资源。

总结

MariaDB 是一款流行的开源关系数据库管理系统,并拥有活跃的社区支持。

您可以参考其文档 以了解更多关于 SQL 基础、迁移、MariaDB 管理、高可用性、性能优化、存储引擎等主题的信息。 如果您未使用集群,您可以禁用二进制日志记录。