开发者

MySql Python connect

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

OperationalError: (1045, "Access deni开发者_JS百科ed 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 ?


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)


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.


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.


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


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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜