1

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)

4 Answers 4

5

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'
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

2 Comments

I think port above should be an integer (i.e. do not use quotes)
@KolaB yes you are right, I checked docs right now : mysql-python.sourceforge.net/MySQLdb.html . I guess in previous versions it was using string since it is creating a string "connection string".
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.