Python MySQL connecting to database error

mysql.connector.connect python
python mysql error handling
python database exception handling
python mysql module
mysql.connector python documentation
how to connect sql with python
python mysql client
mysql workbench python script example

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

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.

Python MySQL Database Connectivity [Complete Guide], mysql connector Error object is used to show us an error when we failed to connect Databases or if any other database error occurred while  Understand the Python MySQL Database connection program import mysql.connector. This line imports the MySQL Connector Python module in your program so you can use this module’s from mysql.connector import Error. Example ACCESS DENIED ERROR when username or password is wrong.

try this :

mysql.connector.connect(
        host="localhost",
        user="user_name",
        passwd="db_password",
        database="db_name")

Python MySQL connecting to database error, How do I connect to a MySQL database in python? I'm writing a Python Script to store JSON data into MySQL Database. I used pandas to store into MySQL Database. I used two different modules (MySQLdb and sqlalchemy) to connect to MySQL dtaabase.

Ok its fixed, as users above mentioned, correct hostname and going back to legacy security along with creating new database fixed the issue

Connection Error while connecting the mysql database through python, I guess, your computer (where the database runs) is not named "hostname": host​="hostname",. If it is the local machine, try localhost . It seems that your MySQL Server version is 8.x and in that case the default MySQL connector is caching_sha2_password. In the other hand your error is maybe because your python client connector does not support this Authentication Plugin and you should explicitly change the Authentication Plugin to the old one (mysql_native_password).

5.1 Connecting to MySQL Using Connector/Python, As per SQL documentation for python library.you need to specify the auth plugin as follows:- conn = mysql.connector.connect(user='root',  However, MySQL default setting doesn't allow remote connections. In this tutorial, you will learn how to connect to a remote MySQL server in Python. We'll be using Python MySQL connector library, let's install it: pip3 install mysql-connector-python. The below code is responsible for connecting to a MySQL server:

Python - Connecting to MySQL Databases, The connect() constructor creates a connection to the MySQL server and returns a MySQLConnection object. The following password=' password ', host='127.0.​0.1', database='employees') cnx.close() Error as err: if err.errno == errorcode. Let’s examine this module in detail: First, import the mysql.connector and Error objects from the MySQL Connector/Python package. Second, use the connect () function to connect to the MySQL Server. The connect () function accepts four parameters: Third, check if the connection to the MySQL

Getting Started with MySQL and Python, Third, check if the connection to the MySQL database has been established successfully by using is_connected() method. In case an exception occurs such as MySQL server is not available, the database does not exist or invalid user name or password, Python will raise an exception. The example which uses error number 1146 is used when Connector/Python receives an error packet from the MySQL Server. The information is parsed and passed to the Error exception as shown. Each exception subclassing from Error can be initialized using the previously mentioned arguments.

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
  • No luck with this