Python MySQL连接数据库错误

时间:2018-11-17 20:31:33

标签: python mysql database

我正在尝试使用Python和MySQL创建一个小型应用程序。 我设法使用MySQL连接来连接Python和MySQL。但是现在,当我尝试运行此脚本以实际连接到数据库时:

import mysql.connector

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

我遇到很多错误,例如:

 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)

我尝试了其他与数据库连接的方法,但是它们没有用,我不确定现在该怎么办。

在此先感谢您的帮助!

EDIT1

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

3 个答案:

答案 0 :(得分:2)

我想,您的计算机(运行数据库的计算机)未命名为“主机名”:

    host="hostname", 

如果它是本地计算机,请尝试localhost。 “主机名”只是示例代码中的占位符。您总是必须替换掉。

关于您的修改:

好像您的数据库服务器配置错误。也许您需要在错误消息中命名的插件。

答案 1 :(得分:0)

尝试:

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

答案 2 :(得分:0)

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