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

关于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 一下权限啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • yun6853992 2022-11-06 17:12
    关注

    这类问题你直接百度一下就是啊,我简单百度貌似是数据库里要给相关用户设置权限,
    https://blog.csdn.net/m0_46108436/article/details/104721631

    评论
查看更多回答(1条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥30 android百度地图SDK海量点显示标题
  • ¥15 windows导入environment.yml运行conda env create -f environment_win.yml命令报错
  • ¥15 这段代码可以正常运行,打包后无法执行,在执行for内容之前一直不断弹窗,请修改调整
  • ¥15 C语言判断有向图是否存在环路
  • ¥15 请问4.11到4.18以及4.27和4.29公式的具体推导过程是怎样的呢
  • ¥20 将resnet50中的卷积替换微ODConv动态卷积
  • ¥15 通过文本框输入商品信息点击按钮将商品信息列举出来点击加入购物车商品信息添加到表单中
  • ¥100 这是什么压缩算法?如何解压?
  • ¥20 upload上传实验报错500,如何解决?(操作系统-windows)
  • ¥15 谁知道 ShaderGraph 那个节点可以接入 Particle System -> Custom Data