系统环境

标题 版本 备注
系统版本 ubuntu 22.04.3 LTS (Jammy Jellyfish) -
内核版本 5.15.0-91-generic #98-Ubuntu SMP Mon Oct 2 15:18:56 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux -
MariaDB v0.10.7 -

MariaDB 简介

MariaDB是一个开源的关系型数据库管理系统(RDBMS),是MySQL的一个分支,由MySQL的创始人之一Michael “Monty” Widenius创建和维护。它保留了与MySQL兼容的API和命令,但也增加了许多新的功能和性能优化。

以下是MariaDB的一些关键特性和优势:

  1. 开源性:MariaDB是开源的,可以免费获取和使用,用户可以根据需要自由地查看、修改和分发源代码。

  2. 兼容性:MariaDB与MySQL兼容,支持MySQL的API和命令,因此用户可以轻松迁移现有的MySQL应用程序到MariaDB。

  3. 性能优化:MariaDB对MySQL进行了优化,提高了查询性能和吞吐量,并引入了一些新的查询优化器和执行计划。

  4. 高可用性:MariaDB支持主从复制、集群和分片等高可用性和扩展性解决方案,可以实现数据的备份和故障恢复。

  5. 安全性:MariaDB实现了诸如访问控制、加密传输、安全存储等安全功能,保障了数据库的安全性和可靠性。

  6. 新功能:MariaDB引入了许多新的功能和扩展,如动态列、全文搜索、JSON支持等,增强了数据库的功能和灵活性。

  7. 活跃的社区:MariaDB拥有一个活跃的开发和用户社区,提供了丰富的文档、教程和支持资源,为用户提供了便利。

  8. 跨平台支持:MariaDB可以在多种操作系统上运行,包括Linux、Windows、macOS等,适用于各种不同的部署环境。

总的来说,MariaDB是一种功能强大、性能优越、安全可靠的关系型数据库管理系统,适用于各种规模和类型的应用程序开发。

MariaDB 安装

注意:以下操作都是在 root 用户下执行

安装准备

1.安装依赖

1
apt-get install libaio1 libaio-dev libncurses5 libncurses5-dev libssl1.1 libssl-dev perl libtinfo-dev

2.创建用户组

1
groupadd -g 1010 mariadb

3.创建用户

1
useradd -g mariadb -u 1010 -M -s /sbin/nologin mariadb

4.创建数据目录

1
mkdir -p /data/mariadb

5.为了后面启动数据库不报找不到 libncurses.so.5libtinfo.so.5 文件,所以我们还需要使用 find 命令查找这 2 个库文件所在路径,然后做个相应的软链接:

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
# libncurses.so.5
root@vm181-ubuntu22:~# find /* -type f -iname 'libncurses*'
/snap/core20/1974/usr/lib/x86_64-linux-gnu/libncurses.so.6.2
/snap/core20/1974/usr/lib/x86_64-linux-gnu/libncursesw.so.6.2
/usr/lib/x86_64-linux-gnu/libncurses++w.a
/usr/lib/x86_64-linux-gnu/libncurses.so.6.3
/usr/lib/x86_64-linux-gnu/libncursesw.so
/usr/lib/x86_64-linux-gnu/libncurses.a
/usr/lib/x86_64-linux-gnu/libncurses.so
/usr/lib/x86_64-linux-gnu/libncurses++.a
/usr/lib/x86_64-linux-gnu/libncursesw.a
/usr/lib/x86_64-linux-gnu/libncursesw.so.6.3
/var/lib/dpkg/info/libncurses5-dev:amd64.md5sums
/var/lib/dpkg/info/libncurses6:amd64.shlibs
/var/lib/dpkg/info/libncurses5-dev:amd64.list
/var/lib/dpkg/info/libncursesw6:amd64.md5sums
/var/lib/dpkg/info/libncursesw6:amd64.triggers
/var/lib/dpkg/info/libncurses6:amd64.md5sums
/var/lib/dpkg/info/libncursesw6:amd64.symbols
/var/lib/dpkg/info/libncursesw6:amd64.list
/var/lib/dpkg/info/libncurses6:amd64.triggers
/var/lib/dpkg/info/libncursesw6:amd64.shlibs
/var/lib/dpkg/info/libncurses6:amd64.list
/var/lib/dpkg/info/libncurses-dev:amd64.md5sums
/var/lib/dpkg/info/libncurses-dev:amd64.list
/var/lib/dpkg/info/libncurses6:amd64.symbols

root@vm181-ubuntu22:~# ln -s /usr/lib/x86_64-linux-gnu/libncurses.so.6.3 /usr/lib/x86_64-linux-gnu/libncurses.so.5


# libtinfo.so.5
root@vm181-ubuntu22:~# find /* -type f -iname libtinfo*
/snap/core20/1974/usr/lib/x86_64-linux-gnu/libtinfo.so.6.2
/usr/lib/x86_64-linux-gnu/libtinfo.a
/usr/lib/x86_64-linux-gnu/libtinfo.so.6.3
/var/lib/dpkg/info/libtinfo6:amd64.symbols
/var/lib/dpkg/info/libtinfo-dev:amd64.list
/var/lib/dpkg/info/libtinfo6:amd64.triggers
/var/lib/dpkg/info/libtinfo6:amd64.shlibs
/var/lib/dpkg/info/libtinfo6:amd64.list
/var/lib/dpkg/info/libtinfo-dev:amd64.md5sums
/var/lib/dpkg/info/libtinfo6:amd64.md5sums

root@vm181-ubuntu22:~# ln -s /usr/lib/x86_64-linux-gnu/libtinfo.so.6.3 /usr/lib/x86_64-linux-gnu/libtinfo.so.5

安装 mariadb

1.由于从官方网站下载 mariadb 安装包速度较慢,所以我这里使用国内的清华源镜像下载。

1
wget -O /usr/local/src/mariadb-10.10.7-linux-x86_64.tar.gz https://mirror.tuna.tsinghua.edu.cn/mariadb/mariadb-10.10.7/bintar-linux-systemd-x86_64/mariadb-10.10.7-linux-systemd-x86_64.tar.gz

2.将下载的安装包解压到 /usr/local/src 目录下

1
tar -zxf /usr/local/src/mariadb-10.10.7-linux-x86_64.tar.gz -C /usr/local/src/

3.将解压后的文件夹移动到 /usr/local/mariadb 目录下

1
mv /usr/local/src/mariadb-10.10.7-linux-x86_64 /usr/local/mysql

4.创建配置文件 vim /etc/my.cnf ,配置内容为:

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
[mysqld]
# 监听地址
bind-address = 0.0.0.0
# 监听端口
port = 3306
# pid 文件
pid-file = /data/mariadb/mysql.pid
# 程序安装目录
basedir = /usr/local/mysql/
# 数据存放目录
datadir = /data/mariadb
# 该条配置需在[client]段同时配置
socket=/tmp/mysql.sock
# 设定默认字符为utf8mb4
character-set-server=utf8mb4


[mysql]
# 允许通过 TAB 键提示
auto-rehash
# 数据库字符集
default-character-set = utf8mb4
connect-timeout = 3


[client]
default-character-set = utf8mb4
port = 3306
socket = /tmp/mysql.sock

5.进入 mysql 目录并初始化数据库:

1
cd /usr/local/mysql && ./scripts/mysql_install_db --user=mariadb --basedir=/usr/local/mysql --datadir=/data/mariadb --group=mariadb --skip-name-resolve

6.将数据存放目录 /data/mariadb 和 mariadb 程序目录 /usr/local/mysql 的所属主和组都修改为 mariadb:

1
chown -R mariadb:mariadb /data/mariadb /usr/local/mysql

7.配置 mariadb 系统环境变量:在 /etc/profile.d 目录下新建 mariadb.sh 文件,内容为:

1
PATH=$PATH:/usr/local/mysql/bin

然后执行命令下面的命令使其立即生效:

1
source /etc/profile.d/mariadb.sh

8.测试执行 mysql --version 看能否正常show 版本信息,如果正常,则说明系统环境配置成功。

1
2
root@vm181-ubuntu22:/usr/local/mysql# mysql --version
mysql Ver 15.1 Distrib 10.10.7-MariaDB, for linux-systemd (x86_64) using readline 5.1

启动 mariadb

1.复制 mariadb 启动脚本文件到 /lib/systemd/system 目录下:

1
cp /usr/local/mysql/support-files/systemd/mariadb.service /lib/systemd/system/

2.将启动脚本文件修改为(主要修改运行 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades. If you want to customize, the
# best way is to create a file "/etc/systemd/system/mariadb.service",
# containing
# .include /usr/lib/systemd/system/mariadb.service
# ...make your changes here...
# or create a file "/etc/systemd/system/mariadb.service.d/foo.conf",
# which doesn't need to include ".include" call and which will be parsed
# after the file mariadb.service itself is parsed.
#
# For more info about custom unit files, see systemd.unit(5) or
# https://mariadb.com/kb/en/mariadb/systemd/
#
# Copyright notice:
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.

[Unit]
Description=MariaDB 10.10.7 database server
Documentation=man:mariadbd(8)
Documentation=https://mariadb.com/kb/en/library/systemd/
After=network.target

[Install]
WantedBy=multi-user.target


[Service]

##############################################################################
## Core requirements
##

Type=notify

# Setting this to true can break replication and the Type=notify settings
# See also bind-address mariadbd option.
PrivateNetwork=false

##############################################################################
## Package maintainers
##

User=mariadb
Group=mariadb

# CAP_IPC_LOCK To allow memlock to be used as non-root user
# CAP_DAC_OVERRIDE To allow auth_pam_tool (which is SUID root) to read /etc/shadow when it's chmod 0
# does nothing for non-root, not needed if /etc/shadow is u+r
# CAP_AUDIT_WRITE auth_pam_tool needs it on Debian for whatever reason
CapabilityBoundingSet=CAP_IPC_LOCK CAP_DAC_OVERRIDE CAP_AUDIT_WRITE

# PrivateDevices=true implies NoNewPrivileges=true and
# SUID auth_pam_tool suddenly doesn't do setuid anymore
PrivateDevices=false

# Prevent writes to /usr, /boot, and /etc
ProtectSystem=full

# Database dir: '/usr/local/mariadb/data' should be writable even
# ProtectSystem=full prevents it
ReadWritePaths=-/data/mariadb
#ReadWritePaths=-/usr/local/mariadb/data


# Doesn't yet work properly with SELinux enabled
# NoNewPrivileges=true

# Prevent accessing /home, /root and /run/user
ProtectHome=true

# Execute pre and post scripts as root, otherwise it does it as User=
PermissionsStartOnly=true



# Perform automatic wsrep recovery. When server is started without wsrep,
# galera_recovery simply returns an empty string. In any case, however,
# the script is not expected to return with a non-zero status.
# It is always safe to unset _WSREP_START_POSITION environment variable.
# Do not panic if galera_recovery script is not available. (MDEV-10538)
ExecStartPre=/bin/sh -c "systemctl unset-environment _WSREP_START_POSITION"
ExecStartPre=/bin/sh -c "[ ! -e /usr/local/mariadb/bin/galera_recovery ] && VAR= || \
VAR=`cd /usr/local/mariadb/bin/..; /usr/local/mariadb/bin/galera_recovery`; [ $? -eq 0 ] \
&& systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1"

# Needed to create system tables etc.
# ExecStartPre=/usr/local/mysql/scripts/mysql_install_db -u mysql

# Start main service
# MYSQLD_OPTS here is for users to set in /etc/systemd/system/mariadb.service.d/MY_SPECIAL.conf
# Use the [Service] section and Environment="MYSQLD_OPTS=...".
# This isn't a replacement for my.cnf.
# _WSREP_NEW_CLUSTER is for the exclusive use of the script galera_new_cluster
ExecStart=/usr/local/mysql/bin/mariadbd $MYSQLD_OPTS $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION

# Unset _WSREP_START_POSITION environment variable.
ExecStartPost=/bin/sh -c "systemctl unset-environment _WSREP_START_POSITION"



KillSignal=SIGTERM

# Don't want to see an automated SIGKILL ever
SendSIGKILL=no

# Restart crashed server only, on-failure would also restart, for example, when
# my.cnf contains unknown option
Restart=on-abort
RestartSec=5s

UMask=007

##############################################################################
## USERs can override
##
##
## by creating a file in /etc/systemd/system/mariadb.service.d/MY_SPECIAL.conf
## and adding/setting the following under [Service] will override this file's
## settings.

# Useful options not previously available in [mysqld_safe]

# Kernels like killing mariadbd when out of memory because its big.
# Lets temper that preference a little.
# OOMScoreAdjust=-600

# Explicitly start with high IO priority
# BlockIOWeight=1000

# If you don't use the /tmp directory for SELECT ... OUTFILE and
# LOAD DATA INFILE you can enable PrivateTmp=true for a little more security.
PrivateTmp=false

# Set an explicit Start and Stop timeout of 900 seconds (15 minutes!)
# this is the same value as used in SysV init scripts in the past
# Galera might need a longer timeout, check the KB if you want to change this:
# https://mariadb.com/kb/en/library/systemd/#configuring-the-systemd-service-timeout
TimeoutStartSec=900
TimeoutStopSec=900

##
## Options previously available to be set via [mysqld_safe]
## that now needs to be set by systemd config files as mysqld_safe
## isn't executed.
##

# Number of files limit. previously [mysqld_safe] open-files-limit
LimitNOFILE=32768

# Maximium core size. previously [mysqld_safe] core-file-size
# LimitCore=

# Nice priority. previously [mysqld_safe] nice
# Nice=-5

# Timezone. previously [mysqld_safe] timezone
# Environment="TZ=UTC"

# Library substitutions. previously [mysqld_safe] malloc-lib with explicit paths
# (in LD_LIBRARY_PATH) and library name (in LD_PRELOAD).
# Environment="LD_LIBRARY_PATH=/path1 /path2" "LD_PRELOAD=

# Flush caches. previously [mysqld_safe] flush-caches=1
# ExecStartPre=sync
# ExecStartPre=sysctl -q -w vm.drop_caches=3

# numa-interleave=1 equalivant
# Change ExecStart=numactl --interleave=all /usr/local/mariadb/bin/mariadbd......

# crash-script equalivent
# FailureAction=

3.执行命令 systemctl daemon-reload 加载 mariadb 启动脚本文件:

1
systemctl daemon-reload

4.执行命令 systemctl enable --now mariadb.service 启动并设置 mariadb 开机自启动:

1
systemctl enable --now mariadb.service

5.启动后,如果终端没有报错输出,则可以使用命令 ss -lnpt |egrep 3306 验证 mariadb 是否正常启动:

1
2
root@vm181-ubuntu22:~# ss -lnpt |egrep 3306
LISTEN 0 80 0.0.0.0:3306 0.0.0.0:* users:(("mariadbd",pid=4833,fd=20))