0

I'm trying to create a small app with Python and MySQL. I managed to connect Python and MySQL using the MySQL connection. But now when I try to run this script to actually connect to the database:

import mysql.connector
mydb = mysql.connector.connect(
 passwd="12345", 
 db="DB", 
 host="hostname", 
 user="root"
 )
print(mydb)

I get a lot of errors like:

 File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\network.py", line 485, in open_connection
socket.SOL_TCP)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 748, in getaddrinfo
 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
File "c:/Users/Krystian/Desktop/DB/db.py", line 7, in <module>
 user="root"
 File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
 return MySQLConnection(*args, **kwargs)
 File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 94, in __init__
 self.connect(**kwargs)
 File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 722, in connect
 self._open_connection()
 File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 207, in _open_connection
 self._socket.open_connection()
 File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\network.py", line 501, in open_connection
 errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'hostname:3306' (11001 getaddrinfo failed)

I tried other ways of conecting to Database but they didn't work and I'm not really sure what to do now.

Thanks in advance for any help!

EDIT1

As Juergen mentioned, after fixing the hostname most of the errors above are gone, but new ones showed up:

File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 94, in __init__
 self.connect(**kwargs)
 File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 722, in connect
 self._open_connection()
 File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 211, in _open_connection
 self._ssl)
 File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 141, in _do_auth
 auth_plugin=self._auth_plugin)
 File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\protocol.py", line 102, in make_auth
 auth_data, ssl_enabled)
 File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\protocol.py", line 58, in _auth_response
 auth = get_auth_plugin(auth_plugin)(
 File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\authentication.py", line 191, in get_auth_plugin
 "Authentication plugin '{0}' is not supported".format(plugin_name))
mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supporte
Juergen
12.8k7 gold badges43 silver badges56 bronze badges
asked Nov 17, 2018 at 20:31

2 Answers 2

2

I guess, your computer (where the database runs) is not named "hostname":

 host="hostname", 

If it is the local machine, try localhost. "hostname" is just a placeholder inside example coding. You always have to replace such.

About your edit:

Looks as your db server is misconfigured. Maybe you need that plugin that is named in the error message.

answered Nov 17, 2018 at 20:39
Sign up to request clarification or add additional context in comments.

2 Comments

Just fixed that, old errors disappeared but new errors appeared
Ok, changing the security back to legacy removed the error about the plugin. Rest of the errors still there
0

try this :

mysql.connector.connect(
 host="localhost",
 user="user_name",
 passwd="db_password",
 database="db_name")
answered Nov 17, 2018 at 20:43

1 Comment

No luck with this

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.