python - 不允许主机连接到此 MySQL 服务器(从 VM)

标签 python mysql python-2.7 mysql-python mysql-error-1064

我正在尝试从我在 VirtualBoxVM 中运行的虚拟 Raspberry PI 连接到 mysql。

使用 Python 脚本:

import mysql.connector

mydb = mysql.connector.connect(
host="MBP",
user="PI-2",
passwd="",
database="temperatureData"
)

mycursor = mydb.cursor()

sql = "INSERT INTO temperature (date, temp_celsius, temp_farenheit) 
VALUES (%s, %s, %s)"
val = ("2019-01-14", "20")

mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")

但失败并出现错误:

1130: Host 'MBP' is not allowed to connect to this MySQL server

在 Mysql(在我的 Macbook 上)中,我通过以下方式设置了用户:

CREATE USER 'PI-2'@'127.0.0.1' IDENTIFIED BY 'PI-2';
GRANT ALL PRIVILEGES ON *.* TO 'PI-2'@'127.0.0.1' WITH GRANT OPTION;

(使用 127.0.0.1,因为我在虚拟机内使用 NAT - 如此处所述:how to get the ipaddress of a virtual box running on local machine)

my.cnf 文件:

# Default Homebrew MySQL server config
[mysqld]

# Only allow connections from localhost

# bind-address = 127.0.0.1

bind-address = 0.0.0.0

更新:

MBP 上 ifconfig 的输出:

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
[ ... ]

在虚拟机上:

eth0      Link encap:Ethernet  HWaddr 08:00:27:41:cb:b4
      inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
      inet6 addr: fe80::ef2d:bd28:6834:9191/64 Scope:Link


lo        Link encap:Local Loopback
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:65536  Metric:1

最佳答案

使用此命令授予权限:将 *.* 上的所有权限授予“PI-2”@“MBP”WITH GRANT OPTION;

它允许 MySQL 接受来自名为 MBP 的主机的连接。

https://dev.mysql.com/doc/refman/5.6/en/grant.html#grant-accounts-passwords

关于python - 不允许主机连接到此 MySQL 服务器(从 VM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55165660/

相关文章:

python - 是否可以在此代码逻辑上使用应用函数或向量化?

python - 在python中获取两个一维数组的乘积

python - 使用 csv.reader 在 Python 中读取同一文件中具有两个不同分隔符的文件

mysql - 你能解决这个简单的 SQL 查询吗?

php - 使用 PDO 插入空条目,bindparam 失败

Python 程序重命名文件名,如果已经存在该文件则覆盖

python - 使用 cookiejar 删除 cookies/结束 session

php - 为什么我不应该在 PHP 中使用 mysql_* 函数?

python - 从属于帖子的网站获取第一张图片

linux - GCC在centos上安装pyICU报错