我正在使用 ubuntu 18.04。我的 pycharm 版本是:
PyCharm 2022.1(社区版)构建号 #PC-221.5080.212,构建于 2022 年 4 月 12 日运行时版本:11.0.14.1+1-b2043.25 amd64 VM:JetBrains sro 的 OpenJDK 64 位服务器 VM Linux 4.15.0-175-generic GC:G1 年轻代,G1 老一代内存:1972M 核心:12
当前桌面:ubuntu:GNOME
当我尝试使用以下程序连接 mysql 时:
import pymysql
conn = pymysql.connect(host='localhost',
user='root',
password='',
database='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
cursor = db.cursor()
我收到以下错误:
> File "/home/user/PycharmProjects/pythonProjectDB/main2.py", line 9,
> in <module>
> cursorclass=pymysql.cursors.DictCursor) File "/home/user/PycharmProjects/pythonProjectRev/venv/lib/python3.6/site-packages/pymysql/connections.py",
> line 353, in __init__
> self.connect() File "/home/user/PycharmProjects/pythonProjectRev/venv/lib/python3.6/site-packages/pymysql/connections.py",
> line 633, in connect
> self._request_authentication() File "/home/user/PycharmProjects/pythonProjectRev/venv/lib/python3.6/site-packages/pymysql/connections.py",
> line 907, in _request_authentication
> auth_packet = self._read_packet() File "/home/user/PycharmProjects/pythonProjectRev/venv/lib/python3.6/site-packages/pymysql/connections.py",
> line 725, in _read_packet
> packet.raise_for_error() File "/home/user/PycharmProjects/pythonProjectRev/venv/lib/python3.6/site-packages/pymysql/protocol.py",
> line 221, in raise_for_error
> err.raise_mysql_exception(self._data) File "/home/user/PycharmProjects/pythonProjectRev/venv/lib/python3.6/site-packages/pymysql/err.py",
> line 143, in raise_mysql_exception
> raise errorclass(errno, errval) pymysql.err.OperationalError: (1698, "Access denied for user 'root'@'localhost'")
我的mysql版本是:
mysql> 选择版本();
+-------------------------+
| version() |
+-------------------------+
| 5.7.37-0ubuntu0.18.04.1 |
+-------------------------+
1 row in set (0.00 sec)
创建的数据库包括:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db |
| menagerie |
| mysql |
| performance_schema |
| sys |
+--------------------+
请有人指导我。
祖尔菲。
答案1
回溯(自下而上读取)错误“ raise errorclass(errno, errval) pymysql.err.OperationalError: (1698, "Access denied for user 'root'@'localhost'")
”表示本地用户root
被拒绝访问数据库服务器。
root
您是以还是用运行查询的sudo
?为什么?不要混淆系统管理员帐户root
和 MySql“root”用户。
看https://serverpilot.io/docs/how-to-access-mysql-with-the-mysql-root-user/
并读man mysql mysqld
。