简体   繁体   English

使用 mysql.connector for python 连接数据库时出错

[英]error when using mysql.connector for python to connect database

I am newbie to python ,started with implementing small programs ,now trying with connecting database .I installed python 3.4 and mysql.connector 2.0.4 .我是 python 新手,从实现小程序开始,现在尝试连接数据库。我安装了 python 3.4 和 mysql.connector 2.0.4。 Below given code is what i used to connnect the database下面给出的代码是我用来连接数据库的代码

#!"C:\python34\python.exe"
import sys
import mysql.connector
print("Content-Type: text/html;charset=utf-8")
print()
conn = mysql.connector.connect(host='localhost:8051',
                                       database='test',
                                       user='root',
                                       password='tiger')

if conn.is_connected():
    print('Connected to MySQL database')

But i am getting error as given below .但我收到如下错误。 not getting why this error are occurring ,because of setup environment is wrong or of some other reason Please suggest不明白为什么会发生这个错误,因为设置环境错误或其他原因请提出建议

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 448, in open_connection
    socket.SOL_TCP)
  File "C:\Python34\lib\socket.py", line 533, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#8>", line 2, in <module>
    password='tiger',database='test')
  File "C:\Python34\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
    return MySQLConnection(*args, **kwargs)
  File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 95, in __init__
    self.connect(**kwargs)
  File "C:\Python34\lib\site-packages\mysql\connector\abstracts.py", line 719, in connect
    self._open_connection()
  File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 209, in _open_connection
    self._socket.open_connection()
  File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 464, in open_connection
    errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:8051:3306' (11004 getaddrinfo failed)

The error message is quite clear:错误信息很清楚:

 Can't connect to MySQL server on 'localhost:8051:3306'

See the double port definition?看到双端口定义了吗?

host='localhost:8051'

should most likey be最应该是

host='localhost', port='8051'

You are using the connection paramater wrong.您使用的连接参数错误。 I think you need to do this:我认为你需要这样做:

conn = mysql.connector.connect(host='localhost',
                               port=8051,
                               database='test',
                               user='root',
                               password='tiger')

Check out this doc for more detail.查看此文档以获取更多详细信息。

The default Port for MySQL is 3306 , when you don't pass "port" argument to mysql.connector.connect it assumes you are using default port which is 3306 , if you use another port you should pass : MySQL 的默认端口是 3306 ,当您不将 "port" 参数传递给 mysql.connector.connect 时,它假定您使用的是默认端口 3306 ,如果您使用另一个端口,则应该传递:

port=xxxx

to the constructor.到构造函数。

For me, I added all connector on my IDE.对我来说,我在 IDE 上添加了所有连接器。

mysql-connector mysql-connector-async-dd mysql-connector mysql-connector-async-dd
mysql-connector-python mysql-connector-python
mysql-connector-python-dd mysql-connector-python-dd
mysql-connector-repackaged mysql-connector-repackaged

then you can see your all database names here:然后你可以在这里看到你所有的数据库名称:

import mysql.connector
mydb = mysql.connector.connect(host='localhost',user='root', passwd = 'Root@1234')
mycursor = mydb.cursor()
mycursor.execute("show databases")
for i in mycursor:
    print(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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