与localhost具有不同端口的MySQLdb连接错误(例如8080)

时间:2012-04-27 10:47:46

标签: python mysql localhost mysql-python

我正在尝试将MySQLdb连接到MySQL。当我的服务器是localhost和端口80时它工作正常。但由于某种原因我不得不将端口更改为8080.现在如何在connect命令中指定端口?
这就是我之前使用的:

db = MySQLdb.connect(host = "localhost", user = "root", passwd = "", db = "pymysql")

现在我的服务器是“localhost:8080”。那么我应该在哪里提供有关此端口的信息? 我试过这个:

db = MySQLdb.connect(host = "127.0.0.1:8080", user = "root", passwd = "", db = "pymysql")

这就是我得到的:

Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
db = MySQLdb.connect(host = "127.0.0.1:8080", user = "root", passwd = "", db = "pymysql")
File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2005, "Unknown MySQL server host '127.0.0.1:8080' (11004)")

请帮助!!!

2 个答案:

答案 0 :(得分:4)

您应该阅读documentation

connect(parameters...)


    host
        name of host to connect to. Default: use the local host via a UNIX socket (where applicable)
    user
        user to authenticate as. Default: current effective user.
    passwd
        password to authenticate with. Default: no password.
    db
        database to use. Default: no default database.
    port
        TCP port of MySQL server. Default: standard port (3306).

答案 1 :(得分:1)

端口8080或80通常用于Web服务器而不是MySQL

MySQL的默认端口是3306