blog / gadflysu

希望與熱烈的風
Talk is Cheap

  1. 1. 安装
  2. 2. 安全配置
  3. 3. 创建用户

MariaDB 是流行的关系型数据库管理系统 (RDBMS),其著名用户有 Wikipedia、WordPress.com 和 Google。在 2009 年 Oracle 收购 Sun Microsystems 之后,后者旗下的开源数据库系统 MySQL 自然归属于(臭名昭著的)Oracle。为规避潜在的闭源风险,MySQL 的创始人 Michael Widenius 及原开发团队创建了一个保证开源的新分支——MariaDB

安装

MariaDB 可以从 Ubuntu 的官方软件源安装:

1
sudo apt install mariadb-server

从前文的背景简述可知,MariaDB 兼容 MySQL,毕竟……/usr/bin/mariadb 就是 /usr/bin/mysql 的软链接。

1
lrwxrwxrwx 1 root root 5 Feb  6 13:53 /usr/bin/mariadb -> mysql*

要检查软件版本,执行:

1
2
➜  ~ mysql --version
mysql Ver 15.1 Distrib 10.1.38-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

本文将在上示环境和软件版本下进行。

启动 MariaDB 服务:

1
systemctl start mysql

安全配置

安装完成后建议首先用软件自带的一个脚本执行初始的安全配置:

1
sudo mysql_secure_installation

注意使用 sudo 以获取 root 权限来执行,这是因为 MariaDB 的默认用户 root 采用 unix_socket 插件进行鉴权(这一点会在后面提到),而执行安全配置需要登录。

1
2
3
4
5
6
7
8
9
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
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):

root 默认密码为空,按下「Enter」键即可。接下来设置你的 root 用户密码(注意没有回显):

1
2
3
4
5
6
7
8
9
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

MariaDB 默认有一个允许任何人登录的匿名用户,方便测试或者初次安装配置(例如后文会讲的用户配置),在生产环境下建议删除之:

1
2
3
4
5
6
7
8
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!

root 用户一般应该仅允许从本地登录,禁止远程连接:

1
2
3
4
5
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!

MariaDB 默认有一个允许任何人访问的数据库 (test),在生产环境下建议删除之:

1
2
3
4
5
6
7
8
9
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!

最后重载权限表以应用上面所做的变更:

1
2
3
4
5
6
7
8
9
10
11
12
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 server 看看,直接执行:

1
mysql

此时会遇到错误:

1
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

看起来是访问权限的问题:以 root 用户从本地尝试访问被拒绝。

我们在前面的配置中已经删除了匿名用户,现在 MariaDB 只有一个 root 用户;而我当前系统登录用户并非 root——其实数据库系统的用户和操作系统的用户显然是两回事,两者有什么关系呢?

参考 Stack Overflow 的一个问答,MySQL 在一些系统(比如 Ubuntu)默认使用 unix_socket 插件,依赖系统用户凭证进行鉴权。官方文档讲,这个插件发布于 MariaDB 5.2.0,从 MariaDB 10.4.3 开始默认启用。unix_socket 允许用户通过本地的 Unix socket 文件使用系统凭证登录到 MariaDB Server。此插件通过系统调用可以获取连接到 socket 的进程 uid,进而得到与其关联的用户名,然后会将连接用户作为具有相同用户名的 MariaDB 用户进行身份验证。

简单的理解大概是,MariaDB 现在是通过系统用户来登录的,那么就要求 MariaDB 有一个同名用户。我们来看看 MariaDB 现在的用户配置:

1
2
3
4
5
6
7
8
9
10
➜  ~ sudo mysql  # connect as "root"

MariaDB [(none)]> USE mysql;
MariaDB [mysql]> SELECT User, Host, plugin FROM mysql.user;
+----------+-----------+-------------+
| User | Host | plugin |
+----------+-----------+-------------+
| root | localhost | unix_socket |
+----------+-----------+-------------+
2 rows in set (0.12 sec)

虽然也可以修改插件配置,但是这里我仍然建议创建一个同名的数据库用户:

1
2
3
4
5
6
7
8
9
10
➜  ~ sudo mysql  # connect as "root"

MariaDB [(none)]> USE mysql;
MariaDB [mysql]> CREATE USER 'YOUR_SYSTEM_USER'@'localhost' IDENTIFIED BY '';
MariaDB [mysql]> GRANT ALL PRIVILEGES ON *.* TO 'YOUR_SYSTEM_USER'@'localhost';
MariaDB [mysql]> UPDATE user SET plugin='unix_socket' WHERE User='YOUR_SYSTEM_USER';
MariaDB [mysql]> FLUSH PRIVILEGES;
MariaDB [mysql]> exit;

➜ ~ systemctl restart mysql

其中 YOUR_SYSTEM_USER 是你的用户名。至此就可以开始顺利地玩耍了。

Author : gadflysu
本文采用「知识共享署名 - 非商业性使用 - 相同方式共享 4.0 国际许可协议 (CC BY-NC-SA 4.0)」进行许可。你可自由分享演绎,惟须遵照:署名非商业性使用相同方式共享不得增加额外限制
Link to this article : https://blog.gadflysu.com/database/mariadb-on-ubuntu/

This article was last updated on days ago, and the information described in the article may have changed.