【问题标题】:raspberry pi sending data to a XAMPP database树莓派将数据发送到 XAMPP 数据库
【发布时间】:2017-09-01 12:06:20
【问题描述】:

我在 python 中有以下代码用于将数据发送到 mysql 数据库

import time
import datetime
import MySQLdb
from time import strftime
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
PIR_PIN = 21
GPIO.setup(PIR_PIN, GPIO.IN)

# Variables for MySQL
db = MySQLdb.connect(host="*******",    user="root",passwd="*****", db="sensor1")
cur = db.cursor()

while True:

i = GPIO.input(PIR_PIN)
print i

datetimeWrite = (time.strftime("%Y-%m-%d ") + time.strftime("%H:%M:%S"))
print datetimeWrite

sql = ("""INSERT INTO templog (datetime,temperature) VALUES (%s,%s)""",(datetimeWrite,i))
try:
    print "Writing to database..."
    # Execute the SQL command
    cur.execute(*sql)
    # Commit your changes in the database
    db.commit()
    print "Write Complete"

except:
    # Rollback in case there is any error
    db.rollback()
    print "Failed writing to database"

cur.close()
db.close()
break

我的问题是我的 XAMPP 服务器安装在 pc 中,我想在 mysql 数据库中查看来自树莓派的数据。 那么为了建立连接,我应该在 host="?" 中写些什么?

【问题讨论】:

    标签: python mysql xampp


    【解决方案1】:

    主机将是安装 XAMPP 的系统的 IP 地址,例如192.168.x.x

    【讨论】:

    • 我按照你说的做了,但仍然没有连接。无法连接到 192.168.x.x 上的 mysql 服务器
    【解决方案2】:

    连接字符串应该是这样的:

    db = MySQLdb.connect(host="192.168.0.xxx",    user="root",passwd="*****", db="sensor1")
    

    this Question

    【讨论】:

    • 我已经做到了。但是有错误:can't connect to MySQL server on 192.168.0.xxx
    • 你检查你的防火墙设置了吗?
    • 在您的 Pi 上尝试以下命令:telnet 192.168.0.xxx 3306 3306 根据文档是 mysql python 连接器的标准端口。如果它以connection refused 或类似名称退出,则您的防火墙(可能是您的路由器设置或数据库服务器上的 Windows 防火墙)阻止了连接。
    • 显示无法连接到远程主机:连接超时。
    • 嗯,这个没有标准答案,这取决于很多事情: - 网络配置:确保两台机器在同一个子网 - MySQL 配置:确保用户(root)允许从网络位置访问。据我所知,标准配置只能访问 127.0.0.1 - Windows 设置:确保 Windows 允许来自本地网络的连接
    猜你喜欢
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 2021-05-01
    相关资源
    最近更新 更多