星火流明 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日
  • 展开全部

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装