[英]“Unknown MySQL server host” when using Flask in Python
I am trying to run a web app with Flask. 我正在尝试使用Flask运行Web应用程序。 I have MySQL server on my device and have changed its bind address
to 192.168.0.102
. 我的设备上装有MySQL服务器,并将其bind address
更改为192.168.0.102
。
Now, in Python I am attempting to connect with MySQLdb: 现在,在Python中,我尝试与MySQLdb连接:
conn = MySQLdb.connect("user='myuser', password='mypassword', host='192.168.0.102', database='usersdb'")
...with Flask app running on 192.168.0.102 ...使用在192.168.0.102上运行的Flask应用
app.run(debug=True, host='192.168.0.102')
Now, I get this error : 现在,我得到这个错误:
OperationalError: (2005, "Unknown MySQL server host 'user='myuser', password='mypassword', host='192.168.0.102', database='usersdb'' (0)")
I don't know if its because MySQL is not running on 192.168.0.102 or if it is a problem with Flask. 我不知道是否是因为MySQL未在192.168.0.102上运行,还是Flask出现了问题。
What could be the problem? 可能是什么问题呢?
Try this: 尝试这个:
Enter the IP address where the MySQL server is at in MySQLdb.connect: 在MySQLdb.connect中输入MySQL服务器所在的IP地址:
conn = MySQLdb.connect(host="localhost", # MySQL host, usually localhost
user="myuser",
passwd="mypassword",
db="usersdb")
And use the address 192.168.0.102
for: 并将地址192.168.0.102
用于:
app.run(debug=True, host='192.168.0.102')
Sadly, you try to put a string
which is invalid host
to MySQLdb.connection
, it's no the right params which function expect! 可悲的是,您尝试将无效host
的string
放入MySQLdb.connection
,这不是函数期望的正确参数! you should read more documentations or try to use help function to deep understand this function. 您应该阅读更多文档或尝试使用帮助功能来深入了解此功能。 and What params its require and which type of param its expected. 以及它需要什么参数以及期望的参数类型。
import MySQLdb
help(MySQLdb.connection)
you will see some docs like the below: 您将看到类似以下内容的一些文档:
class connection(__builtin__.object)
| Returns a MYSQL connection object. Exclusive use of
| keyword parameters strongly recommended. Consult the
| MySQL C API documentation for more details.
|
| host
| string, host to connect
|
| user
| string, user to connect as
|
| passwd
| string, password to use
|
| db
| string, database to use
|
| port
| integer, TCP/IP port to connect to
check your mysql server running on the machine which ip is 192.168.0.102
and then try to connect your mysql server by python command line interaction : 检查您的IP地址为192.168.0.102
的机器上运行的mysql服务器,然后尝试通过python命令行交互连接mysql服务器:
import MySQLdb
conn = MySQLdb.connect(user='myuser', password='mypassword', host='192.168.0.102', database='usersdb')
it's should be working! 它应该正在工作!
BTW, your should run your flask on all address which your machine have! 顺便说一句,您应该在机器具有的所有地址上运行烧瓶! like : 喜欢 :
app.run(host='0.0.0.0', debug=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.