简体   繁体   中英

Can you connect to a database on another pc?

I'm using MySQLdb for python, and I would like to connect to a database hosted on an other PC on the same network/LAN.
I tried the followings for host:

192.168.5.37  
192.168.5.37:3306  
http://192.168.5.37
http://192.168.5.37:3306

None of the above work, I always get the

2005, Unknown MySQL server host ... (0)

What might be the problem?

Code:

db = MySQLdb.connect(host="192.168.5.37", user = "root" passwd = "password", db = "test1")

You can use MySQL Connector/Python , a standardized database driver for Python.
You must provide username, password, host, database name.

import mysql.connector

conn = mysql.connector.connect(user=username, password=password,
                          host="192.168.5.37",
                          database=databaseName)
conn.close()

You can download it from: https://dev.mysql.com/downloads/connector/python/

The IP you posted are local IPs Give it a try with your external IP (for example on this website) https://www.whatismyip.com/

If it works with the external IP, then it's maybe a misconfiguration of your firewall.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM