如何在 Raspberry Pi 上打开 MySQL 以进行外部/远程连接?
我有一个 Raspberry Pi,用于存储家庭酿造活动的温度数据.我正在我的电脑上制作一个 Spring MVC 应用程序,我想点击数据.我的 Pi 和我的电脑都在本地网络上.我可以完美地通过 SSH 和 FTP 连接到我的 RPi.
I have a Raspberry Pi that stores temperature data for homebrewing activity. I am making a Spring MVC application on my computer and I want to tap the data. Both my Pi and my computer are on the local network. I can SSH and FTP into my RPi perfectly.
mysql --192.168.1.102 --u root -p db
导致无法连接到 '192.168.1.102' 上的 MySQL 服务器".
Causes a "Can't connect to MySQL server on '192.168.1.102'".
显然,我的 Java 应用程序也无法连接.
My Java application isn't connecting either, obviously.
SHOW VARIABLES WHERE VARIABLE_NAME = 'port' ;
返回默认端口 3306.
returns the default port, 3306.
是否必须启用允许远程连接到 MySQL 的设置?
Is there a setting that must be enabled to allow remote connections into MySQL?
推荐答案
我最近也遇到了同样的问题.我通过执行以下操作使其正常工作:
I have recently had the same problem myself. I got it working by doing the following:
编辑 MySQL 配置
默认情况下,MySQL 未配置为接受远程连接.可以通过修改配置文件启用远程连接:
By default, MySQL is not configured to accept remote connections. You can enable remote connections by modifying the configuration file:
sudo nano /etc/mysql/my.cnf
找到 [mysqld]
部分.您需要更改的行是bind-address
,它应该设置为默认值127.0.0.1
.您想编辑此行以在网络上显示您的 RPi 的 IP(从您的示例来看,它似乎是 192.168.1.102).编写更改.
Find the [mysqld]
section. The line you need to alter is bind-address
, which should be set to the default value of 127.0.0.1
. You want to edit this line to instead show the IP of your RPi on the network (which would seem to be 192.168.1.102 from your example). Write the changes.
重启 MySQL 服务
sudo service mysql restart
设置 MySQL 权限
以 root 身份连接到您的 MySQL 实例:
Connect to your MySQL instance as root:
mysql -p -u root
创建用户:
CREATE USER '<username>'@'<ip_address>' IDENTIFIED BY '<password>';
- 语法中的撇号 ( ' ) 是必需的
- IP 地址是您尝试连接的网络上设备的 IP 地址
授予相关数据库和表的权限:
Grant permissions to the relevant databases and tables:
GRANT ALL PRIVILEGES ON <database>.* TO '<username>'@'<ip_address>' IDENTIFIED BY '<password>';
- 参数是您在上一步中用于创建用户的参数
- * 将授予对指定数据库中所有表的访问权限.或者,您可以指定一个特定的表
- 您可能只想通过授予相关权限来加强安全性,但这应该足以测试它是否有效
希望如此!
相关文章