MySql Python连接
我正在尝试将python与MySQL连接,但出现以下错误:MySql Python连接,python,mysql,mysql-error-1045,Python,Mysql,Mysql Error 1045,我正在尝试将python与MySQL连接,但出现以下错误: OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)") 我花了6个小时试图解决它,但我失败了。谁能帮帮我吗 您没有指定您的根密码。 如果您正在从远程计算机访问mysql。。。默认情况下,root用户仅限于本地主机连接。尝试运行此命令以启用从任何位置的访问: GRANT ALL PRIVILEGES ON *.*
OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)")
我花了6个小时试图解决它,但我失败了。谁能帮帮我吗 您没有指定您的根密码。 如果您正在从远程计算机访问mysql。。。默认情况下,root用户仅限于本地主机连接。尝试运行此命令以启用从任何位置的访问:
GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY "YOUR_ROOT_PASSWORD";
FLUSH PRIVILEGES;
同时检查防火墙设置。这意味着您忘记了密码参数,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>
$ mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
因此,您需要知道您的密码是什么,并且您需要使用以下信息创建数据库:
CREATE DATABASE yourdatabase;
如上所述,从mysql命令行。我收到这个错误,因为我的配置文件中有引号
[mysql]
username: 'uuuu'
password: 'pppp'
host: 'localhost'
database: 'dddd'
应该是
[mysql]
username: uuuu
password: pppp
host: localhost
database: dddd
如果在密码列前面没有看到任何值
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()
看起来您正试图以
root
身份连接到本地主机上的mysql服务器,而无需密码。这是您通常以root用户身份登录的方式吗?没有密码?你在用什么操作系统?您是否能够以root用户身份登录MySQL命令行而无需密码?(即不是通过mysqldb?我使用的是windows 7!!在mysql命令行中,它要求一个pswrd,我将其放入!!!并且它工作!!!注意,我重新安装mysql可能是因为它工作正常!!请帮助我使用windows 7你能帮我做什么吗!!windows命令行应该是相同的,可能参数是\p而不是-p,但是如果我没记错的话,它是-p风格的。打开一个命令提示符。例如从phpMyAdmin(如果您有),或者从控制台使用:mysql-u root-pOperationalError:(1045,“拒绝用户'root'@'localhost(使用密码:YES)”访问)由于更改密码而更改错误
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()