简体   繁体   English

通过python连接mysql数据库时出现连接错误

[英]Connection Error while connecting the mysql database through python

I am trying to connect with Mysql server using mentioned below python code我正在尝试使用下面提到的 python 代码连接 Mysql 服务器

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.但是在运行此代码以测试我是否已连接到 MySQL 时,我遇到了我无法理解的错误。

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注意:我已将默认值初始化为 5000

for the testing purpose, I have initialized the port 5000, user - root and password - root出于测试目的,我已经初始化了端口 5000,用户 - root 和密码 - 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?有什么方法可以检查用户 ID 和密码以在 python 代码中进行交叉验证和更新吗?

suggestions will be helpful建议会有所帮助

Follow these steps:按着这些次序:

  1. You must install MySQL Server ( https://dev.mysql.com/downloads/installer/ )您必须安装 MySQL 服务器 ( https://dev.mysql.com/downloads/installer/ )

from this install MySQL Server.从此安装 MySQL 服务器。

  1. Install mysql-connector-python (in your python environment)安装mysql-connector-python (在你的 python 环境中)
  2. Use this code :使用此代码:

    Open database connection打开数据库连接

    import mysql.connector导入 mysql.connector

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

    print(mydb)打印(mydb)

As per SQL documentation for python library.you need to specify the auth plugin as follows:-根据 python 库的 SQL 文档。你需要指定 auth 插件如下:-

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

I come across a similar problem on my centOS VPS using webmin + python3.我在使用 webmin + python3 的 centOS VPS 上遇到了类似的问题。

At first I think it might due to connection package , so I tried:起初我认为可能是由于 connection package ,所以我尝试了:

  1. 'python3 -m pip install mysql-connector' , in python it will use 'import mysql.connector' 'python3 -m pip install mysql-connector' ,在 python 中它将使用 'import mysql.connector'

  2. 'pip3 install PyMySQL' , in python it use 'import PyMySQL' 'pip3 install PyMySQL' ,在python中使用'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.然后我从一个网站上找到了其他几种连接方法(现在我不记得它的地址了),详细解释了每种方法的优点和缺点,根据它,我最终选择使用mysqlclient。

  1. 'sudo pip3 install mysqlclient' , I use this because it is more versatile and have support for python3. 'sudo pip3 install mysqlclient' ,我使用它是因为它更通用并且支持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, ....我尝试了几次随机测试,不小心使用了 root 和配置文件中的 skip-grant-tables 选项,我能够使用 SSH python 代码登录 mysql,但这不是原因,因为普通的新创建的用户仍然不能无论我在代码中访问什么主机,localhost,127.0.0.1,域,...。

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.最近,默认情况下 MySQL 服务器中已经存在用户“root”,我发现它有 4 个条目,所以我尝试在 webmin -> 服务器 -> MySQL 服务器 -> 用户权限中创建 4 个单独的类似条目记录。

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'它们是 '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.而且,Bingo,在添加了以上 4 个条目后,新用户可以使用通过 python3 代码提供的密码 authenthicate 登录。

(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) (记住第一次,你必须在MySQL Server页面中设置MySQL Admin 'root'密码,这样密码登录认证功能才会起作用,并且该密码不需要与系统的root密码相同)

Thats all, this take me 6 hours.就是这样,这需要我 6 个小时。 hope it can help someone using webmin as well.希望它也可以帮助使用 webmin 的人。 Please give positive vote if it helps you in anyway.如果它对您有帮助,请投赞成票。 thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM