本文档风哥主要介绍MariaDB的官方标准安装方法及生产环境初始化配置,包括安装前准备、安装步骤、初始化配置和安全加固等内容。风哥教程参考MariaDB官方文档Installation Guides内容,适合数据库管理员学习和实施。
Part01-基础概念与理论知识
1.1 MariaDB安装方法
MariaDB支持多种安装方法:
- 二进制包安装(推荐)
- 源码编译安装
- 容器化安装(Docker)
- 包管理器安装(yum/apt)
1.2 MariaDB安装要求
风哥提示:安装MariaDB前需确保系统满足最低硬件和软件要求。
1.3 MariaDB初始化流程
MariaDB初始化流程包括:
- 安装软件包
- 启动服务
- 设置root密码
- 配置基本参数
- 验证安装
更多视频教程www.fgedu.net.cn
Part02-生产环境规划与建议
2.1 安装前检查
更多学习教程公众号风哥教程itpux_com
# 检查系统版本
[root@fgedu.net.cn ~]# cat /etc/redhat-release
Oracle Linux Server release 9.3
# 检查系统架构
[root@fgedu.net.cn ~]# uname -m
x86_64
# 检查可用磁盘空间
[root@fgedu.net.cn ~]# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 100G 12G 83G 13% /
# 检查内存
[root@fgedu.net.cn ~]# free -h
total used free shared buff/cache available
Mem: 31G 2.1G 26G 128M 2.8G 28G
[root@fgedu.net.cn ~]# cat /etc/redhat-release
Oracle Linux Server release 9.3
# 检查系统架构
[root@fgedu.net.cn ~]# uname -m
x86_64
# 检查可用磁盘空间
[root@fgedu.net.cn ~]# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 100G 12G 83G 13% /
# 检查内存
[root@fgedu.net.cn ~]# free -h
total used free shared buff/cache available
Mem: 31G 2.1G 26G 128M 2.8G 28G
2.2 磁盘配置建议
生产环境建议:
- 使用SSD存储提高性能
- 数据目录和日志目录分离
- 配置适当的文件系统(如XFS)
- 启用磁盘配额和监控
2.3 网络配置建议
网络配置建议:
- 配置静态IP地址
- 关闭不必要的网络服务
- 配置防火墙规则
- 启用网络监控
学习交流加群风哥微信: itpux-com
Part03-生产环境项目实施方案
3.1 官方标准安装步骤
# 添加MariaDB官方仓库
[root@fgedu.net.cn ~]# cat > /etc/yum.repos.d/MariaDB.repo << 'EOF'
[mariadb]
name = MariaDB
baseurl = https://yum.mariadb.org/10.11/rhel9-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF
# 安装MariaDB
[root@fgedu.net.cn ~]# yum install -y MariaDB-server MariaDB-client
# 启动MariaDB服务
[root@fgedu.net.cn ~]# systemctl start mariadb
[root@fgedu.net.cn ~]# systemctl enable mariadb
[root@fgedu.net.cn ~]# cat > /etc/yum.repos.d/MariaDB.repo << 'EOF'
[mariadb]
name = MariaDB
baseurl = https://yum.mariadb.org/10.11/rhel9-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF
# 安装MariaDB
[root@fgedu.net.cn ~]# yum install -y MariaDB-server MariaDB-client
# 启动MariaDB服务
[root@fgedu.net.cn ~]# systemctl start mariadb
[root@fgedu.net.cn ~]# systemctl enable mariadb
3.2 初始化配置
# 运行安全初始化脚本
[root@fgedu.net.cn ~]# 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
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):
OK, successfully used password, moving on…
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!
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 ‘fgedu.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!
[root@fgedu.net.cn ~]# 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
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):
OK, successfully used password, moving on…
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!
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 ‘fgedu.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.3 安全加固
# 创建专用数据库用户
[root@fgedu.net.cn ~]# mysql -u root -p
Enter password:
MariaDB [(none)]> CREATE USER ‘fgedu’@’fgedu.localhost’ IDENTIFIED BY ‘YourStrongPassword!123’;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON fgedudb.* TO ‘fgedu’@’fgedu.localhost’;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> EXIT;
Bye
[root@fgedu.net.cn ~]# mysql -u root -p
Enter password:
MariaDB [(none)]> CREATE USER ‘fgedu’@’fgedu.localhost’ IDENTIFIED BY ‘YourStrongPassword!123’;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON fgedudb.* TO ‘fgedu’@’fgedu.localhost’;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> EXIT;
Bye
学习交流加群风哥QQ113257174
Part04-生产案例与实战讲解
4.1 MariaDB安装实战演示
# 检查MariaDB安装状态
[root@fgedu.net.cn ~]# systemctl status mariadb
● mariadb.service – MariaDB 10.11.8 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service;
enabled;
vendor preset: disabled)
Active: active (running) since Mon 2026-04-07 10:30:00 CST;
10min ago
Docs: man:mariadbd(8)
https://mariadb.com/kb/en/library/systemd/
Process: 1234 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 1200 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`cd /usr/bin/..;
/usr/bin/galera_recovery`;
[ $? -eq 0 ] && systemctl set-environment _WSREP_START_POSITION=$VAR || : (code=exited, status=0/SUCCESS)
Process: 1198 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Main PID: 1233 (mariadbd)
Status: “Taking your SQL requests now…”
Tasks: 31 (limit: 32768)
Memory: 150.0M
CPU: 1.2s
CGroup: /system.slice/mariadb.service
└─1233 /usr/sbin/mariadbd
Apr 07 10:30:00 fgedu.net.cn systemd[1]: Starting MariaDB 10.11.8 database server…
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] /usr/sbin/mariadbd (server 10.11.8-MariaDB) starting as process 1233 …
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Warning] Could not increase number of max_open_files to more than 1024 (request: 32186)
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Number of pools: 1
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Using crc32 + pclmulqdq instructions
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Initializing buffer pool, total size = 128.0M, instances = 1, chunk size = 128.0M
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Completed initialization of buffer pool
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=16384
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: 128 rollback segments are active.
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Creating shared tablespace for temporary tables
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Setting file ‘./ibtmp1’ size to 12.0 MB. Physically writing the file full;
Please wait …
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: File ‘./ibtmp1’ size is now 12.0 MB.
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: 10.11.8 started;
log sequence number 16384;
transaction id 2
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] Plugin ‘FEEDBACK’ is disabled.
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] Server socket created on IP: ‘127.0.0.1’.
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] /usr/sbin/mariadbd: ready for connections.
Version: ‘10.11.8-MariaDB’ socket: ‘/var/lib/mysql/mysql.sock’ port: 3306 MariaDB Server
Apr 07 10:30:00 fgedu.net.cn systemd[1]: Started MariaDB 10.11.8 database server.
[root@fgedu.net.cn ~]# systemctl status mariadb
● mariadb.service – MariaDB 10.11.8 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service;
enabled;
vendor preset: disabled)
Active: active (running) since Mon 2026-04-07 10:30:00 CST;
10min ago
Docs: man:mariadbd(8)
https://mariadb.com/kb/en/library/systemd/
Process: 1234 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 1200 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`cd /usr/bin/..;
/usr/bin/galera_recovery`;
[ $? -eq 0 ] && systemctl set-environment _WSREP_START_POSITION=$VAR || : (code=exited, status=0/SUCCESS)
Process: 1198 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Main PID: 1233 (mariadbd)
Status: “Taking your SQL requests now…”
Tasks: 31 (limit: 32768)
Memory: 150.0M
CPU: 1.2s
CGroup: /system.slice/mariadb.service
└─1233 /usr/sbin/mariadbd
Apr 07 10:30:00 fgedu.net.cn systemd[1]: Starting MariaDB 10.11.8 database server…
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] /usr/sbin/mariadbd (server 10.11.8-MariaDB) starting as process 1233 …
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Warning] Could not increase number of max_open_files to more than 1024 (request: 32186)
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Number of pools: 1
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Using crc32 + pclmulqdq instructions
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Initializing buffer pool, total size = 128.0M, instances = 1, chunk size = 128.0M
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Completed initialization of buffer pool
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=16384
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: 128 rollback segments are active.
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Creating shared tablespace for temporary tables
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Setting file ‘./ibtmp1’ size to 12.0 MB. Physically writing the file full;
Please wait …
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: File ‘./ibtmp1’ size is now 12.0 MB.
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: 10.11.8 started;
log sequence number 16384;
transaction id 2
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] Plugin ‘FEEDBACK’ is disabled.
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] Server socket created on IP: ‘127.0.0.1’.
Apr 07 10:30:00 fgedu.net.cn mariadbd[1233]: 2026-04-07 10:30:00 0 [Note] /usr/sbin/mariadbd: ready for connections.
Version: ‘10.11.8-MariaDB’ socket: ‘/var/lib/mysql/mysql.sock’ port: 3306 MariaDB Server
Apr 07 10:30:00 fgedu.net.cn systemd[1]: Started MariaDB 10.11.8 database server.
4.2 初始化配置实战
# 配置MariaDB参数
[root@fgedu.net.cn ~]# cat >> /etc/my.cnf.d/server.cnf << 'EOF'
[mysqld]
# 基本配置
user = mysql
datadir = /mariadb/fgdata
socket = /var/lib/mysql/mysql.sock
# 性能配置
innodb_buffer_pool_size = 4G
innodb_log_file_size = 1G
max_connections = 1000
# 安全配置
bind-address = 127.0.0.1
skip-networking = 0
EOF
# 创建数据目录
[root@fgedu.net.cn ~]# mkdir -p /mariadb/fgdata
[root@fgedu.net.cn ~]# chown -R mysql:mysql /mariadb/
# 重新初始化数据库
[root@fgedu.net.cn ~]# systemctl stop mariadb
[root@fgedu.net.cn ~]# mysqld –initialize –user=mysql –datadir=/mariadb/fgdata
[root@fgedu.net.cn ~]# systemctl start mariadb
[root@fgedu.net.cn ~]# cat >> /etc/my.cnf.d/server.cnf << 'EOF'
[mysqld]
# 基本配置
user = mysql
datadir = /mariadb/fgdata
socket = /var/lib/mysql/mysql.sock
# 性能配置
innodb_buffer_pool_size = 4G
innodb_log_file_size = 1G
max_connections = 1000
# 安全配置
bind-address = 127.0.0.1
skip-networking = 0
EOF
# 创建数据目录
[root@fgedu.net.cn ~]# mkdir -p /mariadb/fgdata
[root@fgedu.net.cn ~]# chown -R mysql:mysql /mariadb/
# 重新初始化数据库
[root@fgedu.net.cn ~]# systemctl stop mariadb
[root@fgedu.net.cn ~]# mysqld –initialize –user=mysql –datadir=/mariadb/fgdata
[root@fgedu.net.cn ~]# systemctl start mariadb
4.3 验证与测试
# 验证MariaDB连接
[root@fgedu.net.cn ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with;
or \g.
Your MariaDB connection id is 8
Server version: 10.11.8-MariaDB MariaDB Server
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)]> SELECT VERSION();
+—————–+
| VERSION() |
+—————–+
| 10.11.8-MariaDB |
+—————–+
1 row in set (0.00 sec)
MariaDB [(none)]> CREATE DATABASE fgedudb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> USE fgedudb;
Database changed
MariaDB [fgedudb]> CREATE TABLE fgedu_test (id INT PRIMARY KEY, name VARCHAR(50));
Query OK, 0 rows affected (0.01 sec)
MariaDB [fgedudb]> INSERT INTO fgedu_test VALUES (1, ‘Test’);
Query OK, 1 row affected (0.00 sec)
MariaDB [fgedudb]> SELECT * FROM fgedu_test;
+—-+——+
| id | name |
+—-+——+
| 1 | Test |
+—-+——+
1 row in set (0.00 sec)
MariaDB [fgedudb]> EXIT;
Bye
[root@fgedu.net.cn ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with;
or \g.
Your MariaDB connection id is 8
Server version: 10.11.8-MariaDB MariaDB Server
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)]> SELECT VERSION();
+—————–+
| VERSION() |
+—————–+
| 10.11.8-MariaDB |
+—————–+
1 row in set (0.00 sec)
MariaDB [(none)]> CREATE DATABASE fgedudb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> USE fgedudb;
Database changed
MariaDB [fgedudb]> CREATE TABLE fgedu_test (id INT PRIMARY KEY, name VARCHAR(50));
Query OK, 0 rows affected (0.01 sec)
MariaDB [fgedudb]> INSERT INTO fgedu_test VALUES (1, ‘Test’);
Query OK, 1 row affected (0.00 sec)
MariaDB [fgedudb]> SELECT * FROM fgedu_test;
+—-+——+
| id | name |
+—-+——+
| 1 | Test |
+—-+——+
1 row in set (0.00 sec)
MariaDB [fgedudb]> EXIT;
Bye
风哥提示:安全开发是防止SQL注入的第一道防线
Part05-风哥经验总结与分享
5.1 安装常见问题与解决
- 依赖包缺失:使用yum/dnf安装缺失的依赖
- 端口冲突:检查3306端口是否被占用
- 权限问题:确保数据目录权限正确
- 初始化失败:检查日志文件定位问题
5.2 初始化最佳实践
风哥提示:生产环境中应根据实际硬件配置和业务需求调整MariaDB参数。
5.3 安装后检查清单
- 服务状态检查
- 连接测试
- 参数配置验证
- 安全设置检查
- 备份策略制定
from MariaDB视频:www.itpux.com
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
