在Ubuntu上安装MariaDB

本文详细介绍在Ubuntu20.04上安装MariaDB的方法,并提供了安全配置的步骤,包括设置root密码、删除匿名用户、禁用远程root登录等,确保数据库服务器的安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导读MariaDB是一个开源的关系型数据库管理系统,它最初被设计为一个向后兼容的、二进制的即插即用的替代MySQL的系统。

在Ubuntu上安装MariaDB在Ubuntu上安装MariaDB

MariaDB由MySQL的原始开发者和开源社区开发和维护,这里我们来介绍如何在Ubuntu 20.04上安装和保护MariaDB安全配置。

安装MariaDB

wuxiangping@linuxcool:~$ sudo apt update
wuxiangping@linuxcool:~$ sudo apt install mariadb-server

安装完成后,MariaDB服务将自动启动。

验证数据库服务器运行状态

wuxiangping@linuxcool:~$ sudo systemctl status mariadb.service

在Ubuntu上安装MariaDB在Ubuntu上安装MariaDB

查看数据库版本号

wuxiangping@linuxcool:~$ mysql --version
mysql  Ver 15.1 Distrib 10.1.44-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

在Ubuntu上安装MariaDB在Ubuntu上安装MariaDB

MariaDB安全配置,初始化数据

MariaDB服务器软件包自带一个名为mysql_secure_installation的脚本,可以轻松提高数据库服务器的安全性。

wuxiangping@linuxcool:~$ 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
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.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] y    设置root用户密码
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 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y  禁止root原创连接
 ... 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   删除默认的test数据库名
 - 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!

在Ubuntu上安装MariaDB在Ubuntu上安装MariaDB

本地root用户登录

shell与MariaDB交互操作,运行时会提示输入系统用户名的密码

wuxiangping@linuxcool:~$ sudo mysql
[sudo] wuxiangping 的密码: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 49
Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.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指定主机远程访问

默认监听端口是127.0.0.1,需要修改为主机IP

wuxiangping@linuxcool:~$ sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf

bind-address=127.0.0.1
修改为
bind-address=192.168.3.22

在Ubuntu上安装MariaDB在Ubuntu上安装MariaDB

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.3.8' IDENTIFIED BY 'password' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> select host, user from user;   #查看MySQL数据库user表

在Ubuntu上安装MariaDB在Ubuntu上安装MariaDB

重启mariadb服务

wuxiangping@linuxcool:~$ sudo systemctl restart mariadb.service

远程主机连接测试

在Ubuntu上安装MariaDB在Ubuntu上安装MariaDB

在Ubuntu上安装MariaDB在Ubuntu上安装MariaDB

连接成功!Linux就该这么学

### 在 Ubuntu 24 上安装 MariaDB 的指南 尽管目前官方尚未正式发布 Ubuntu 24,但可以假设其软件包管理方式与之前的版本(如 Ubuntu 22.04 和 Ubuntu 18.04)类似。以下是在未来版本的 Ubuntu 24 上安装 MariaDB 的通用步骤[^1]。 #### 更新系统软件包 在安装任何新软件之前,确保系统的软件包列表是最新的。这可以通过运行以下命令来实现: ```bash sudo apt update sudo apt upgrade -y ``` #### 安装 MariaDB MariaDB 可以通过 APT 包管理器直接安装。使用以下命令来安装 MariaDB 服务器和客户端: ```bash sudo apt install mariadb-server mariadb-client -y ``` #### 验证 MariaDB 是否成功安装 安装完成后,可以通过检查 MariaDB 服务的状态来验证安装是否成功: ```bash sudo systemctl status mariadb ``` 如果服务正在运行,输出中将显示 `active (running)`[^1]。 #### 提高 MariaDB 的安全性 为了确保 MariaDB 的安全配置,运行以下脚本以完成一些基本的安全设置: ```bash sudo mysql_secure_installation ``` 此脚本会引导用户完成一系列安全配置,包括设置 root 密码、禁用远程 root 登录、删除匿名用户等。 #### 测试 MariaDB 是否正常工作 可以通过登录到 MariaDB 控制台来测试其功能: ```bash sudo mysql -u root -p ``` 输入之前设置的密码后,如果成功进入 MariaDB 控制台,则说明安装和配置均正确。 #### 自定义配置(可选) MariaDB 的配置文件通常位于 `/etc/mysql/mariadb.conf.d/` 目录下。可以根据需求编辑这些文件以调整性能或功能。例如,修改最大连接数或启用慢查询日志: ```bash sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf ``` --- ### 注意事项 如果 Ubuntu 24 发布时 MariaDB 尚未包含在默认存储库中,可能需要手动添加 MariaDB 官方存储库。具体操作如下: 1. 下载并安装 MariaDB 存储库密钥: ```bash sudo apt install software-properties-common -y sudo curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash ``` 2. 更新软件包列表并按照前述步骤安装 MariaDB。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值