简体   繁体   中英

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 . 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 :

port=xxxx

to the constructor.

For me, I added all connector on my IDE.

mysql-connector mysql-connector-async-dd
mysql-connector-python
mysql-connector-python-dd
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)

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