varul jain
varul jain

Reputation: 334

Connection Error while connecting the mysql database through python

I am trying to connect with Mysql server using mentioned below python code

import mysql.connector

mydb = mysql.connector.connect(
    host = "127.0.0.1",
    port = 5000,
    user = "user id",
    password = "password"
    )

print(mydb)

But while running this code to test whether I have been connected with MySQL or not, I am facing the error which I am not able to understand.

Traceback (most recent call last):
  File "C:\Users\varul.jain\Desktop\Test Phase\Mysql\mydb_test.py", line 7, in <module>
    password = "root"
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
    return MySQLConnection(*args, **kwargs)
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\connection.py", line 95, in __init__
    self.connect(**kwargs)
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\abstracts.py", line 716, in connect
    self._open_connection()
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\connection.py", line 210, in _open_connection
    self._ssl)
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\connection.py", line 142, in _do_auth
    auth_plugin=self._auth_plugin)
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\protocol.py", line 102, in make_auth
    auth_data, ssl_enabled)
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\protocol.py", line 58, in _auth_response
    auth = get_auth_plugin(auth_plugin)(
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\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 supported

Note: I have initialized the default as 5000

for the testing purpose, I have initialized the port 5000, user - root and password - root

but the authorization the default password is not available as per mentioned above error

Is there any way to check the user id and password to cross verify and update in python code accordingly?

suggestions will be helpful

Upvotes: 0

Views: 16532

Answers (4)

Roberto Reis
Roberto Reis

Reputation: 1

I come across a similar problem just use one of this:

  • from mysql.connector.plugins import caching_sha2_password

    from mysql.connector.plugins import mysql_native_password*

this will solve the problem

Upvotes: 0

billynhk
billynhk

Reputation: 11

I come across a similar problem on my centOS VPS using webmin + python3.

At first I think it might due to connection package , so I tried:

  1. 'python3 -m pip install mysql-connector' , in python it will use 'import mysql.connector'

  2. 'pip3 install PyMySQL' , in python it use 'import PyMySQL'

both dont solve the problem. Then I found there are a couples of other connection method from a website (which I dont remember its address now), which detaily explain pro and cos of each, according to it, I finally choose to use mysqlclient.

  1. 'sudo pip3 install mysqlclient' , I use this because it is more versatile and have support for python3.

All above does not solve my problem, I still face connection error.

I tried a couple of random test and accidentially with root and the skip-grant-tables option in config file, I was able to log in mysql with SSH python code, but that is not the reason , cos a normal newly created user still cannot login no matter what hosts I am accessing in the code, localhost, 127.0.0.1, domains, ....

So I revoke the skip-grant-tables option cos thats risky without protection (just for test).

Lately, from default already exist user 'root' in MySQL Server, I found it has 4 entries, so I have try to create 4 individual similar entries record in webmin -> server -> MySQL server -> User Permission.

these records have same user name and less privilege right but similar host content assigned. they are 'local' , '127.0.0.1' , 'myservername.vps.provider.ca' , '::1'

(I do not know what does the last one means)

And, Bingo, after above 4 entries added, the new user is able to log in with password authenthicate provided via python3 code.

(remember the first time, you have to set the MySQL Admin 'root' with password in MySQL Server page, so that password Login authenthication feature will work, and that password need not same as root password of system)

Thats all, this take me 6 hours. hope it can help someone using webmin as well. Please give positive vote if it helps you in anyway. thanks

Upvotes: 0

Gokul KS
Gokul KS

Reputation: 21

Follow these steps:

  1. You must install MySQL Server (https://dev.mysql.com/downloads/installer/)

from this install MySQL Server.

  1. Install mysql-connector-python (in your python environment)
  2. Use this code :

    Open database connection

    import mysql.connector

    mydb = mysql.connector.connect(host="127.0.0.1", port="3306", user="root", password="root", auth_plugin="mysql_native_password")

    print(mydb)

Upvotes: 2

As per SQL documentation for python library.you need to specify the auth plugin as follows:-

conn = mysql.connector.connect(user='root', password='password', host='127.0.0.1',port=5000, database='test', auth_plugin='mysql_native_password')
print(conn)

Upvotes: 1

Related Questions