关于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> 

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

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