树莓派4b安装mariadb数据库

文章详细介绍了如何在树莓派4B上安装MariaDB数据库,包括使用`sudoapt-getinstallmariadb-server`命令安装,通过`mysql_secure_installation`进行安全配置,设置root用户的密码,以及删除匿名用户和test数据库。此外,还讲解了如何授权root用户远程访问,并解决远程连接时可能出现的问题,涉及编辑`/etc/mysql/mariadb.conf.d/50-server.cnf`文件,注释掉`bind-address=127.0.0.1`行以允许远程连接。

一、安装mariabd数据库

使用以下命令安装mariadb数据库:
sudo apt-get install mariadb-server
之后会提示安装,如下图所示,键入Y即可安装成功
在这里插入图片描述

查看数据库状态
service mariadb status
出现如下图所示即代表数据库安装成功
在这里插入图片描述

二、配置数据库

安装完数据库需要对数据进行安全设置

// 配置数据库安全设置
sudo mysql_secure_installation
pi@raspberrypi:~ $ 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		#root帐户已受保护,回答“n”
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

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

Disallow root login remotely? [Y/n] n		#是否禁止root用户远程登录
 ... skipping.

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!

在这里插入图片描述

添加账号/给账号授权

grant all privileges on *.* to root@'%' identified by 'admin' with grant option;

刷新权限,使刚才的命令生效

flush privileges;

解释一下,上面这段sql中 grant all privileges on *.*,其中*.*表示所有库的所有表,如需指定库表可对应修改,如访问test库的user表,则可修改成grant all privileges on test.user;这段sql中grant all privileges on *.* to root@'%',其中root@'%'表示root账户可以通过所有的IP地址来对数据库进行访问,如需指定root账户的IP访问,如指定10.18.18.12这个IP对数据库进行访问,则可修改成root@'10.18.18.12'

三、 关于MariaDB 数据库没有办法远程访问的问题

在 RaspberryPi(树莓派) 上安装 MariaDB 或者 MySQL 数据库的时候,设置的用户没有办法通过网络远程登录。首先确定的是,用户已经设置了访问权限。
提示的错误信息是:
ERROR 2002 (HY000): Can’t connect to MySQL server on ‘xx.xx.xx.xx’ (115)
其中地址 xx.xx.xx.xx 是 RaspberryPi(树莓派)的地址
这是一个有关 RaspberryPi(树莓派)的配置问题。
使用命令sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf编辑。
找到 bind-address = 127.0.0.1 这一行,注释掉,如下图所示
在这里插入图片描述
注:如果不注释掉这一行,MariaDB 数据库只会绑定 127.0.0.1 的地址,这样只能在 localhost上登录

### 解决方案 树莓派4B 的 IP 地址始终显示为 `127.0.0.1` 可能是因为网络配置错误或者 DHCP 配置未生效。以下是可能的原因分析以及对应的解决方案: #### 原因 1: 网络接口配置不正确 如果树莓派的网络接口未被正确定义,可能会导致其默认绑定到回环地址 (`127.0.0.1`)。 **解决方法**: 检查并更新 `/etc/netplan/*.yaml` 文件中的网络配置,确保它类似于以下内容[^4]: ```yaml network: version: 2 ethernets: eth0: optional: true dhcp4: true ``` 对于静态 IP 地址分配,可以修改为: ```yaml network: version: 2 ethernets: eth0: addresses: - 192.168.50.100/24 gateway4: 192.168.50.1 nameservers: addresses: - 8.8.8.8 - 8.8.4.4 ``` 应用更改后运行命令以重新加载网络设置: ```bash sudo netplan apply ``` --- #### 原因 2: DNS 或主机名解析问题 DNS 配置不当可能导致树莓派无法正常解析外部资源,从而影响 IP 获取过程。 **解决方法**: 编辑 `/etc/hosts` 文件,确认其中的内容与实际网络环境匹配[^2]。例如: ``` 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters 192.168.50.20 rsp01 192.168.50.22 rsp02 192.168.50.25 rsp03 ``` 同时,可以通过手动指定 DNS 来解决问题,在 `/etc/resolv.conf` 中添加以下内容: ``` nameserver 8.8.8.8 nameserver 8.8.4.4 ``` --- #### 原因 3: 路由器或交换机端口限制 某些路由器会对设备 MAC 地址进行过滤,阻止新设备自动获取 IP 地址。 **解决方法**: 尝试通过路由器管理界面查看是否有针对该设备的 MAC 地址过滤规则,并将其移除。另外,也可以在路由器中预留特定的静态 IP 给树莓派使用的 MAC 地址。 --- #### 原因 4: MySQL 数据库权限问题干扰 虽然此问题看似无关,但如果存在数据库连接失败的情况(如引用提到的 `ERROR 1698 (28000)`),也可能间接引发其他服务异常行为[^3]。 **解决方法**: 重置 MySQL 用户密码并赋予适当权限: ```sql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword'; FLUSH PRIVILEGES; ``` 重启相关服务以验证修复效果: ```bash sudo systemctl restart mariadb.service ``` --- #### 原因 5: LlamaFile 打包工具的影响 如果是基于预打包镜像启动,则可能存在固化的网络参数设定,难以动态调整[^5]。 **解决方法**: 建议从官方 Ubuntu Server ISO 制作全新系统盘,避免依赖第三方封装版本带来的潜在隐患。 --- ### 总结 综合以上可能性,推荐优先排查 NetPlan 配置文件是否存在问题,并结合实际情况逐步测试各环节直至恢复正常工作状态。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值