在Linux(AlmaLinux 9)上安装MariaDB

当 MariaDB Server 的前身 MySQL 于 2009 年被 Oracle 收购时,MySQL 创始人 Michael “Monty” Widenius 出于对 Oracle 管理权的担忧而分叉了该项目,并将新项目命名为 MariaDB。 MySQL 以他的第一个女儿 My 命名,而 MariaDB 则以他的第二个女儿 Maria 命名。

在 MariaDB 5.5 之前,MariaDB Server 遵循 MySQL 版本编号模式,旨在与 MySQL 的同一主要版本兼容。

2012 年,为了反映 MySQL 中不可用的功能越来越多,MariaDB Server 的版本编号出现了分歧,MariaDB 发布了 10.0,而 MySQL 发布了 5.6。 当前的长期支持版本是 MariaDB 10.6,而最新的稳定短期支持版本是 MariaDB 10.9。

MariaDB Server 仍然保持与 MySQL 的高度兼容性,并且大多数使用 MySQL 的流行应用程序将与 MariaDB 无缝协作。

MariaDB Server 非常强调不破坏其用户的向后兼容性。 就地升级支持从旧的 MySQL 版本升级到最新的 MariaDB 版本。

MariaDB Server 提供了一种 Oracle 语法兼容模式,无需更改即可运行 Oracle 数据库应用程序。

1. 安装

1.1. 检查是否已安装 mariadb

1
dnf list installed | grep -i mariadb

更新 DNF 存储库缓存

1
sudo dnf update

1.2. 安装 MariaDB

如果未安装,执行以下命令安装命令

1
sudo dnf -y install mariadb mariadb-server

说明:

  1. 这里的 -y 表示 yes, 它会自动接受所有默认选项,减少与用户交互,这在自动化脚本中非常有用。
  2. Maria 是 mysql 的一个分支,它在 mysql 的基础上进行了改进和增强,以提供更好的性能、安全性和可靠性。
    mariadb-server 是 MariaDB 的服务器软件包,它包含了 MariaDB 数据库服务器及其相关的工具、库和配置文件。
    在安装 MariaDB 时,通常需要安装 mariadb-server 软件包,这样才能启动 MariaDB 服务器,并且使用 MariaDB 数据库。
    因此,mariadb-server 是 MariaDB 的一个重要组成部分。
  3. 安装前可以使用 dnf search mariadbdnf info mariabd 查看 rpm repo list 是否包含了 maridb 以及确认版本是否是合适

1.4. 启动 MariaDB 并设置为开机启动

1
sudo systemctl enable --now mariadb

2. 配置

2.1. 修改配置授权远程访问

1
2
3
4
vi /etc/my.cnf.d/mariadb-server.cnf

bind-address = <some ip-address>

2.2. 创建远程访问用户,并授予访问权限

1
2
3

sudo mysql

1
2
3
4
5
6
7
8

CREATE USER 'user1'@'internet_ip' IDENTIFIED BY 'the_password';

GRANT ALL ON *.* TO 'user1'@'internet_ip';
-- e.g. GRANT ALL ON *.* TO 'user1'@'localhost';

--or reset password if user exists
SET PASSWORD FOR 'user1'@'localhost' = PASSWORD('the_password');

2.3. 设置中文字符集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
vi /etc/my.cnf.d/mariadb-server.cnf
在[mysqld]标签下添加
#character
init_connect='SET collation_connection = utf8mb4_unicode_ci'
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
skip-character-set-client-handshake

vi /etc/my.cnf.d/client.cnf
在[client]下添加
#character
default-character-set=utf8mb4

vi /etc/my.cnf.d/mysql-clients.cnf

在[mysql]中添加

#character

default-character-set=utf8mb4

2.4. 重启 mariadb 服务,使配置生效

1
sudo systemctl restart mariadb

2.5. 查看版本

1
sudo mysql 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
MariaDB [(none)]> \s
--------------
mysql Ver 15.1 Distrib 10.5.22-MariaDB, for Linux (x86_64) using EditLine wrapper

Connection id: 8
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 10.5.22-MariaDB MariaDB Server
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 7 min 27 sec

Threads: 1 Questions: 13 Slow queries: 0 Opens: 17 Open tables: 10 Queries per second avg: 0.029
--------------

2.6. 查看字符集是否生效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mysql> show variables like 'character%';

MariaDB [(none)]> show variables like 'character%';
+--------------------------+------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mariadb/charsets/ |
+--------------------------+------------------------------+
8 rows in set (0.001 sec)

2.7. 设置数据库对大小写不敏感

进入 /etc/my.cnf.d ,编辑 mariadb-server.cnf

1
2
3
vi /etc/my.cnf.d/mariadb-server.cnf
在[mysqld]下添加
lower_case_table_names=1

重启让配置生效

1
sudo systemctl restart mariadb

增强 MariaDB的安全性

MariaDB 安装默认不安全。安装数据库软件后,运行以下命令来保护您的 MariaDB 服务器。此命令将启动一系列安全配置,以增强对 MariaDB 安装的保护:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
$ 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!

3. 参考文档

Centos7上安装MariaDB

在Linux(AlmaLinux 9)上安装MariaDB

https://pengtech.net/database/install_mariadb_on_linux.html

作者

鹏叔

发布于

2024-08-26

更新于

2024-08-29

许可协议

评论