MySql Python 连接

发布于 2024-11-02 04:15:02 字数 189 浏览 5 评论 0原文

我正在尝试将 python 与 MySQL 连接,但出现此错误:

OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)")

我尝试解决它 6 小时但失败了。有人可以帮我吗?

I'm trying to connect python with MySQL, but I get this error:

OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)")

I'm trying to solve it for 6 hours and I fail. Can any one please help me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凡间太子 2024-11-09 04:15:02

这意味着你忘记了密码参数,在mysql中密码参数不是必需的。

从unix命令行:

$ mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
$ mysql -u root -p
Enter password: 
<typing password>
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 47
Server version: 5.1.37-1ubuntu5.5 (Ubuntu)

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

mysql> 

与从python登录不完全相同,

但它是相同的错误,并且很可能意味着在您的情况下密码参数根本没有进入mysql。

当我输入错误的密码时,我在命令行上收到一个非常不同的错误:

$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

it means that you forgot the password parameter, which in mysql is not required.

from a unix command line:

$ mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
$ mysql -u root -p
Enter password: 
<typing password>
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 47
Server version: 5.1.37-1ubuntu5.5 (Ubuntu)

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

mysql> 

not exactly the same as logging from python,

but it's the same error, and most likely means that in your case the password argument did not make it to mysql at all.

when I type the wrong password I get a very different error on the command line:

$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
云雾 2024-11-09 04:15:02

您没有指定 root 密码。
如果您从远程计算机访问 mysql...默认情况下 root 用户仅限于本地主机连接。尝试运行此命令以启用从任何​​地方的访问:

GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY "YOUR_ROOT_PASSWORD";
FLUSH PRIVILEGES;

另请检查您的防火墙设置。

You didn't specified your root password.
If you are accessing mysql from remote computer... root user is by default limited to localhost connections only. Try running this to enable access from anywhere:

GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY "YOUR_ROOT_PASSWORD";
FLUSH PRIVILEGES;

Also check your firewall settings.

三月梨花 2024-11-09 04:15:02
import MySQLdb

cxn = MySQLdb.connect(host = 'localhost',user = 'root',passwd = 'password', db = 'yourdatabase')

因此,您需要知道您的密码是什么,并且需要使用以下命令创建数据库:

CREATE DATABASE yourdatabase;

如上所述,从 mysql 命令行。

import MySQLdb

cxn = MySQLdb.connect(host = 'localhost',user = 'root',passwd = 'password', db = 'yourdatabase')

So you need to know what your password is and you need to have created a database with:

CREATE DATABASE yourdatabase;

from the mysql command line as described above.

旧人 2024-11-09 04:15:02

我收到此错误是因为我的配置文件中有引号。

[mysql]
username: 'uuuu'
password: 'pppp'
host: 'localhost'
database: 'dddd'

应该是

[mysql]
username: uuuu
password: pppp
host: localhost
database: dddd

I was getting this error because I had quotes in my config file.

[mysql]
username: 'uuuu'
password: 'pppp'
host: 'localhost'
database: 'dddd'

should have been

[mysql]
username: uuuu
password: pppp
host: localhost
database: dddd
枉心 2024-11-09 04:15:02

执行操作时在密码列前面没有看到任何值,

SELECT user, host, password FROM mysql.user;

如果您在 mysql 中

在 python 脚本中不要指定密码

例如

import MySQLdb

db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                     user="root",         # your username
                     passwd="",           # your password
                     db="dynamo",
                     port = 3306)         # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

# Use all the SQL you like
cur.execute("select * from SomeTable limit 10")

# print all the first cell of all the rows
for row in cur.fetchall():
    print(row[0])

db.close()

If you don't see any value in front of the password column when you do

SELECT user, host, password FROM mysql.user;

within your mysql

In your python script don't specify the password

for example

import MySQLdb

db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                     user="root",         # your username
                     passwd="",           # your password
                     db="dynamo",
                     port = 3306)         # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

# Use all the SQL you like
cur.execute("select * from SomeTable limit 10")

# print all the first cell of all the rows
for row in cur.fetchall():
    print(row[0])

db.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文