简体   繁体   English

尝试使用连接器/Python 时出现错误“用户'root'@'localhost 的访问被拒绝”

[英]I am getting error “Access denied for user 'root'@'localhost” when trying to use Connector/Python

I am trying to connect to my local mysql system using python.我正在尝试使用 python 连接到我的本地 mysql 系统。

I note that I can only login to my mysql system from the console by sudo /usr/bin/mysql -u root -p我注意到我只能通过 sudo /usr/bin/mysql -u root -p 从控制台登录到我的 mysql 系统

I have looked around the web and am very confused by all the suggestions of which none work for me.我环顾了 web 并且对所有对我都不起作用的建议感到非常困惑。

Like I tried就像我试过的

mysql> SELECT User, Host, plugin FROM mysql.user;

Which gives me...这给了我...

+------------------+-----------+-----------------------+
| User             | Host      | plugin                |
+------------------+-----------+-----------------------+
| root             | localhost | mysql_native_password |
| mysql.session    | localhost | mysql_native_password |
| mysql.sys        | localhost | mysql_native_password |
| debian-sys-maint | localhost | mysql_native_password |
| keith            | localhost | mysql_native_password |
+------------------+-----------+-----------------------+

5 rows in set (0.12 sec)

... ...

... ...

import mysql.connector

cnx = mysql.connector.connect(user='root', password='#########',
                          host='127.0.0.1',
                          database='SurveyData')
cnx.close()

... ...

I get the following errors我收到以下错误

... ...

MySQLInterfaceError                       Traceback (most recent call last)
~/miniconda3/lib/python3.7/site-packages/mysql/connector  /connection_cext.py in _open_connection(self)
    199         try:
--> 200             self._cmysql.connect(**cnx_kwargs)
    201         except MySQLInterfaceError as exc:

MySQLInterfaceError: Access denied for user 'root'@'localhost'

During handling of the above exception, another exception occurred:

It seemes that you use root@localhost then connecting.似乎您使用 root@localhost 然后连接。 Have you tried root@127.0.0.1?你试过root@127.0.0.1吗?

Also check out: Access Denied for User 'root'@'localhost' (using password: YES) - No Privileges?另请查看: 用户'root'@'localhost'的访问被拒绝(使用密码:是) - 没有权限?

Grant Global Privileges To User Account By Command Line.通过命令行向用户帐户授予全局权限。 Open a terminal and run below command.打开终端并运行以下命令。

:~$ mysql -u root -p
Enter password: 
mysql> GRANT SELECT ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

Grant Special Schema Privileges To User Account.向用户帐户授予特殊架构特权。 Open terminal run below command.打开终端运行下面的命令。

:~$ mysql -u root -p
Enter password: 
mysql> GRANT SELECT,UPDATE,INSERT,DELETE ON SurveyData.* TO 'root'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON SurveyData.* TO 'root'@'localhost';
Query OK, 0 rows affected (0.00 sec)

I use this script for test:我使用这个脚本进行测试:

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='SurveyData',
                                         user='root',
                                         password='########')

    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL Server version ", db_Info)
        cursor = connection.cursor()
        cursor.execute("select database();")
        record = cursor.fetchone()
        print("Your connected to database: ", record)

except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    if (connection.is_connected()):
        cursor.close()
        connection.close()
        print("MySQL connection is closed")

Logging in with the root user can be considered a "petential risk" and this explains the blocking of any connection through this user.使用 root 用户登录可以被视为“潜在风险”,这解释了通过该用户进行的任何连接的阻塞。 So the solution is to create a new user and grant them all privileges.因此解决方案是创建一个新用户并授予他们所有权限。

mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'newpassword';    
mysql> GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ProgrammingError: 1698 (28000): 在 python 中使用 mysql.connector 时,用户 'root'@'localhost 的访问被拒绝 - ProgrammingError: 1698 (28000): Access denied for user 'root'@'localhost when using mysql.connector in python 返回我的错误是“用户'root'@'localhost'的访问被拒绝(使用密码:是)。 即使我使用`mike` - error that returns me is `Access denied for user 'root'@'localhost' (using password: YES)`. Even when I use `mike` 错误 1045 (28000):通过 Python 连接到 MySQL 时,用户 'root'@'localhost'(使用密码:YES)的访问被拒绝 - ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) when connecting to MySQL through Python 即使在python pyspark中授予root@localhost权限后,用户“root”@“localhost”错误的访问也被拒绝 - Access denied for user 'root'@'localhost' error even after grant the privileges to root @localhost in python pyspark Python MySQL OperationalError:1045,“访问被拒绝用户root @'localhost' - Python MySQL OperationalError: 1045, "Access denied for user root@'localhost' mysql.connector.errors.ProgrammingError: 1045 (28000): 拒绝用户 'root'@'localhost' 访问(使用密码:YES) - mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 使用密码:用户“ root” @“ localhost”的访问被拒绝 - Access denied for user 'root'@'localhost' using password: NO 为什么在 python 中尝试使用 .get_text() 和漂亮的汤时会出错? - Why am I getting an error when trying to use .get_text() in python with beautiful soup? 为什么在 Python 中尝试使用 requests.get 时出现 [403] 错误 - Why am I getting a [403] Error when trying to use requests.get in Python 尝试使用 eBay 查找 sdk 和 python 时出现连接错误 - I am getting a Connection error when trying to use the eBay finding sdk with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM