简体   繁体   English

Python MySQL连接数据库错误

[英]Python MySQL connecting to database error

I'm trying to create a small app with Python and MySQL. 我正在尝试使用Python和MySQL创建一个小型应用程序。 I managed to connect Python and MySQL using the MySQL connection. 我设法使用MySQL连接来连接Python和MySQL。 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 编辑1

As Juergen mentioned, after fixing the hostname most of the errors above are gone, but new ones showed up: 正如Juergen所述,在修复了主机名之后,上面的大多数错误都消失了,但是出现了新的错误:

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 . 如果是本地计算机,请尝试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. 也许您需要在错误消息中命名的插件。

try this : 尝试这个 :

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

好的,如上面的用户所述,已经更正了主机名,并返回到旧版安全性以及创建新数据库的问题得以解决。

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

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