如何在 Ubuntu 22.04 上安装和使用 MariaDB如何在 Ubuntu 22.04 上安装和使用 MariaDB如何在 Ubuntu 22.04 上安装和使用 MariaDB如何在 Ubuntu 22.04 上安装和使用 MariaDB
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何在 Ubuntu 22.04 上安装和使用 MariaDB

在此页

  1. 先决条件
  2. 安装和配置 MariaDB
  3. 验证 MariaDB 版本
  4. 结论

MariaDB 是一个免费、开源且功能强大的数据库管理系统,用于存储应用程序数据。它是一个稳定的关系数据库管理系统,是流行的 MySQL 数据库系统的分支。它专为可扩展性和关键任务部署而设计。在撰写本教程时,MariaDB 的最新版本是 10.6.7。每个主要版本将至少维护 5 年。所以 MariaDB 10.6.7 将被支持到 2026 年。

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

先决条件

  • 一台运行 Ubuntu 22.04 的服务器。
  • 在服务器上配置了根密码。

安装和配置 MariaDB

首先,使用以下命令更新所有系统包:

apt update -y

更新所有包后,使用以下命令安装最新版本的 MariaDB 服务器和客户端:

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

安装成功后,启动MariaDB服务并使其在系统重启时启动:

systemctl start mariadb
systemctl enable mariadb
systemctl status mariadb

您应该看到以下输出:

? mariadb.service - MariaDB 10.6.7 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-06-25 14:34:47 UTC; 39s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 3174 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
    Process: 3175 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 3177 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $?>
    Process: 3218 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 3220 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
   Main PID: 3206 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 13 (limit: 4579)
     Memory: 57.2M
        CPU: 582ms
     CGroup: /system.slice/mariadb.service
             ??3206 /usr/sbin/mariadbd

Jun 25 14:34:47 ubuntu2204 mariadbd[3206]: 2022-06-25 14:34:47 0 [Note] /usr/sbin/mariadbd: ready for connections.
Jun 25 14:34:47 ubuntu2204 mariadbd[3206]: Version: '10.6.7-MariaDB-2ubuntu1'  socket: '/run/mysqld/mysqld.sock'  port: 3306  Ubuntu 22.04
Jun 25 14:34:47 ubuntu2204 systemd[1]: Started MariaDB 10.6.7 database server.
Jun 25 14:34:47 ubuntu2204 /etc/mysql/debian-start[3222]: Upgrading MySQL tables if necessary.

接下来,您需要保护 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。系统将要求您更改根密码,如下所示:

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。登录后,您应该会看到以下输出:

Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 41
Server version: 10.6.7-MariaDB-2ubuntu1 Ubuntu 22.04

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.6.7-MariaDB-2ubuntu1 |
+-------------------------+
1 row in set (0.000 sec)

结论

恭喜!您已经在 Ubuntu 22.04 服务器上成功安装并保护了 MariaDB 10.6。您现在可以创建一个新的数据库,并使用它并将其连接到您的应用程序。

©2015-2025 艾丽卡 support@alaica.com