星火流明 2022-11-06 16:45 采纳率: 100%
浏览 42
已结题

关于ubuntu中mysql连接出错的问题

请问怎么配置才可以正常连接
ubuntu20.04环境下安装mysql

sudo apt-get update
sudo apt-get install -y mysql-server mysql-client

后用pymysql连接时报错,已排除密码错误

```python
import pymysql

db = pymysql.connect(host='localhost', user='root', password='123456', port=3306)
cursor = db.cursor()
cursor.execute('SELECT VERSION()')
data = cursor.fetchone()
print('Database version:', data)
cursor.execute("CREATE DATABASE spiders DEFAULT CHARACTER SET utf8mb4")
db.close()

报错信息如下

Traceback (most recent call last):
  File "/home/skf/dev/codedb0/web_spider/t.py", line 3, in <module>
    db = pymysql.connect(host='localhost', user='root', password='123456')
......
pymysql.err.OperationalError: (1698, "Access denied for user 'root'@'localhost'")

若使用一般用户,报错如下

Database version: ('8.0.31-0ubuntu0.22.04.1',)
Traceback (most recent call last):
  File "/home/skf/dev/codedb0/web_spider/t.py", line 8, in <module>
    cursor.execute("CREATE DATABASE spiders DEFAULT CHARACTER SET utf8mb4")
......
pymysql.err.OperationalError: (1044, "Access denied for user 'skf'@'localhost' to database 'spiders'")

另外尝试在终端连接

skf@skf-Lenovo:~$ mysql -u root -p
Enter password: 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
skf@skf-Lenovo:~$ mysql -u skf -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 8.0.31-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

使用root

root@skf-Lenovo:~# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 8.0.31-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
  • 写回答

2条回答 默认 最新

  • IT民工金鱼哥 运维领域新星创作者 2022-11-07 15:11
    关注

    pymysql.err.OperationalError: (1698, "Access denied for user 'root'@'localhost'")
    就是数据库连接的权限问题。
    你确定的创建用户各种,授权各种没问题吗?还有机器防火墙相关是否关闭或者放通?
    你在PY脚本是本机运行吗?
    授权之后,记得flush 一下权限啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月15日
  • 已采纳回答 11月7日
  • 修改了问题 11月6日
  • 修改了问题 11月6日
  • 展开全部

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?