本页内容

  • 先决条件
  • 添加 MariaDB 仓库
  • 安装并配置 MariaDB
  • 验证 MariaDB 版本
  • 关于 MariaDB 的常见问题 什么是 MariaDB?MariaDB 与 MySQL 有何不同?MariaDB 是否可以免费使用?MariaDB 的主要特性有哪些?MariaDB 能否作为 MySQL 的替代品直接替换?MariaDB 如何存储数据?MariaDB 是否支持复制?哪些编程语言可以与 MariaDB 交互?MariaDB 如何保障安全性?MariaDB 是否适用于大型企业?MariaDB 的性能如何?MariaDB 是否支持存储过程?我可以将现有的 MySQL 数据库迁移到 MariaDB 吗?在哪里可以找到 MariaDB 的文档和社区支持?MariaDB 的最新版本是什么,在哪里可以下载?
  • 结论
  • 什么是 MariaDB?
  • MariaDB 与 MySQL 有何不同?
  • MariaDB 可以免费使用吗?
  • MariaDB 的主要特性有哪些?
  • MariaDB 能否作为 MySQL 的替代品直接替换?
  • MariaDB 是如何存储数据的?
  • MariaDB 是否支持复制?
  • 哪些编程语言可以与 MariaDB 交互?
  • MariaDB 是如何保障安全性的?
  • MariaDB 是否适合大型企业?
  • MariaDB 的性能如何?
  • MariaDB 是否支持存储过程?
  • 我可以将现有的 MySQL 数据库迁移到 MariaDB 吗?
  • 在哪里可以找到 MariaDB 的文档和社区支持?
  • MariaDB 的最新版本是什么,在哪里可以下载?

MariaDB 是一个免费、开源的数据库系统,是流行的 MySQL 数据库系统的一个分支。它由 MySQL 的原始开发者打造,是一款被广泛使用的关系型数据库管理系统。它专为可扩展性和关键业务部署而设计。默认情况下,MariaDB 软件包在所有主流 Linux 发行版的默认仓库中均可获取。在撰写本教程时,MariaDB 的最新版本为 10.8。每个主要版本至少会被维护 5 年。因此,MariaDB 10.8.0 将一直支持到 2027 年。

在本教程中,我们将向您展示如何在 Ubuntu 22.04 服务器上安装 MariaDB 10.8。

先决条件

  • 一台运行 Ubuntu 22.04 的服务器。
  • 服务器上已配置 root 密码。

添加 MariaDB 仓库

默认情况下,最新的 MariaDB 版本在 Ubuntu 22.04 的标准仓库中不可用。因此,您需要将 MariaDB 官方仓库添加到您的系统中。

首先,使用以下命令安装所有必需的依赖项:

apt-get install software-properties-common gnupg2 -y

所有依赖项安装完成后,使用以下命令导入 MariaDB 签名密钥:

apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'

您应该会看到以下输出:

Executing: /tmp/apt-key-gpghome.kGFC5Ag1H8/gpg.1.sh --fetch-keys https://mariadb.org/mariadb_release_signing_key.asc
gpg: requesting key from 'https://mariadb.org/mariadb_release_signing_key.asc'
gpg: key F1656F24C74CD1D8: public key "MariaDB Signing Key <
[email protected]
>" imported
gpg: Total number processed: 1
gpg:               imported: 1

接下来,使用以下命令添加 MariaDB 仓库:

add-apt-repository 'deb [arch=amd64] http://mariadb.mirror.globo.tech/repo/10.8/ubuntu jammy main'

仓库添加完成后,使用以下命令更新仓库:

apt-get update -y

至此,MariaDB 仓库已添加且为最新状态。您现在可以进入下一步。

安装并配置 MariaDB

现在,使用以下命令安装最新版本的 MariaDB 服务端和客户端:

apt-get install mariadb-server mariadb-client -y

安装成功后,启动 MariaDB 服务并设置为开机自启:

systemctl start mariadb
systemctl enable mariadb

接下来,您需要加固 MariaDB 安装并设置 MariaDB 的 root 密码。您可以通过运行以下脚本来完成:

mysql_secure_installation

系统会要求您提供当前的 root 密码,如下所示:

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): 

直接按 Enter 键即可。系统会要求您切换到 unix_socket 认证方式,如下所示:

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 authorization.

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

Switch to unix_socket authentication [Y/n] Y

输入 Y 并按 Enter 键。系统会要求您更改 root 密码,如下所示:

Enabled successfully!
Reloading privilege tables..
 ... Success!


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

Change the root password? [Y/n] Y

输入 Y 并按 Enter 键以更改 root 密码。

New password: 
Re-enter new password: 

输入您的强密码并按 Enter 键。您应该会看到以下输出:

Password updated successfully!
Reloading privilege tables..
 ... Success!


Next, you will be asked to remove anonymous users as shown below:

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

输入 Y 并按 Enter 键以删除匿名用户。系统会要求您禁止远程 root 登录,如下所示:

 ... 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

输入 Y 并按 Enter 键。系统会要求您删除一个测试数据库,如下所示:

 ... 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

输入 Y 并按 Enter 键以删除测试数据库。系统会要求您重新加载权限表,如下所示:

 - 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

输入 Y 并按 Enter 键,您应该会看到以下输出:

 ... 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 版本

至此,MariaDB 已安装并完成了安全加固。接下来,您需要验证已安装的 MariaDB 版本。

首先,使用以下命令登录到 MariaDB shell:

mysql -u root -p

输入您的 MariaDB root 密码并按 Enter 键。登录后,您应该会看到以下输出:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.8.3-MariaDB-1:10.8.3+maria~jammy mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

运行以下命令来验证 MariaDB 版本:

MariaDB [(none)]> SELECT VERSION();

您应该会看到以下输出:

+-------------------------------------+
| VERSION()                           |
+-------------------------------------+
| 10.8.3-MariaDB-1:10.8.3+maria~jammy |
+-------------------------------------+
1 row in set (0.000 sec)

关于 MariaDB 的常见问题

什么是 MariaDB?

MariaDB 是一个开源的关系型数据库管理系统(RDBMS),是 MySQL 的一个分支。它是在 MySQL 被 Oracle 收购引发担忧后,由 MySQL 的原始开发者创建的。MariaDB 旨在与 MySQL 高度兼容,这意味着它支持与 MySQL 相同的模式(schema)、API 和命令。

MariaDB 与 MySQL 有何不同?

虽然 MariaDB 最初是作为 MySQL 的直接替代品创建的,但此后它已经分化,增加了额外功能、新的存储引擎以及改进的性能。与 MySQL 相比,MariaDB 通常更新更频繁,并且采用社区驱动的开发模式。

MariaDB 可以免费使用吗?

是的,MariaDB 是免费且开源的,依据 GNU 通用公共许可证第 2 版(GPLv2)发布。

MariaDB 的主要特性有哪些?

主要特性包括其开源属性、与 MySQL 的兼容性、多种存储引擎、高级集群能力、复制功能,以及对性能和稳定性的高度关注。

MariaDB 能否作为 MySQL 的替代品直接替换?

是的,在大多数情况下,MariaDB 可以作为 MySQL 的替代品直接替换。它被设计为与 MySQL 数据库二进制兼容。

MariaDB 是如何存储数据的?

MariaDB 使用多种存储引擎来存储数据,包括 InnoDB、MyISAM 和 Aria 等。每种引擎都有其自身的优势和使用场景。

MariaDB 是否支持复制?

是的,MariaDB 支持主从(master-slave)和主主(master-master)复制,与 MySQL 类似。它还提供 Galera Cluster 以实现同步多主复制。

哪些编程语言可以与 MariaDB 交互?

可以使用大多数流行的编程语言访问 MariaDB,包括 PHP、Python、Java、C#、Ruby 等,通常通过 MySQL 连接器或原生驱动实现。

MariaDB 是如何保障安全性的?

MariaDB 提供强大的安全特性,包括密码哈希、基于角色的访问控制、SSL/TLS 加密以及审计功能,以确保数据安全和合规性。

MariaDB 是否适合大型企业?

MariaDB 具有足够的可扩展性和健壮性,能够处理大规模企业级应用。它被许多大型组织使用,能够处理复杂数据库和高并发事务。

MariaDB 的性能如何?

MariaDB 以其高性能而著称。它拥有多种存储引擎和优化选项,可针对不同类型的工作负载进行配置,从而提升性能。

MariaDB 是否支持存储过程?

是的,MariaDB 支持存储过程和函数,允许用户将复杂的业务逻辑封装在数据库内部。

我可以将现有的 MySQL 数据库迁移到 MariaDB 吗?

是的,从 MySQL 迁移到 MariaDB 通常比较直接,因为 MariaDB 被设计为与 MySQL 数据库兼容。

在哪里可以找到 MariaDB 的文档和社区支持?

MariaDB 官方网站提供了全面的文档。此外,还有活跃的社区论坛、Stack Overflow 以及其他在线资源,您可以在这些地方找到与 MariaDB 相关的支持和讨论。

MariaDB 的最新版本是什么,在哪里可以下载?

MariaDB 的最新版本可以在其官方网站上找到。请务必访问该网站以获取最新版本和下载详情。

结论

在本文中,我们介绍了如何在 Ubuntu 22.04 服务器上安装最新的 MariaDB 数据库。我们还介绍了如何加固 MariaDB 以及查看 MariaDB 版本。现在您可以连接到 MariaDB,开始创建数据库、用户,并将其集成到您的应用程序中。