MySQL mysql_exceptions.OperationalError: (1045, “访问被拒绝用户 'root'@'localhost'(使用密码:YES)”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10181344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
提问by Massimo Variolo
I've started working with django, and here is the error on 'runserver' after setting up 'DATABASES' in settings.py
我已经开始使用 django,这是在 settings.py 中设置“DATABASES”后“runserver”上的错误
mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
My settings.py portion of code:
我的 settings.py 代码部分:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'my_site', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'root', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
I've created 'my_site' database and privileges are set in the right way (root has all privileges). I've done this operations with 'phpmyadmin'.
我已经创建了 'my_site' 数据库并以正确的方式设置了权限(root 拥有所有权限)。我已经用“phpmyadmin”完成了这个操作。
What goes wrong?
出了什么问题?
采纳答案by Akhilraj N S
Run in console
在控制台中运行
mysql> grant all privileges on *.* to root@localhost identified by 'password' with grant option;
回答by Rahul Satal
After struggling for two days I finally found whats wrong. Entire steps to set up a mysql database for Django site are-
折腾了两天,终于发现哪里不对了。 为 Django 站点设置 mysql 数据库的整个步骤是-
Your settings.py file must look like-
您的 settings.py 文件必须如下所示 -
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db', #Name of database you created
'USER': 'root', #Username of database you created
'PASSWORD':'', #Password of database you created
'HOST': '',
'PORT': '',
} }
Note:-The password and username given above is of my localhost you have change it to yours.
注意:-上面给出的密码和用户名是我的本地主机,您已将其更改为您的。
create a database in localhost let name it django_db(for this you can use lamp/wamp/Xampp or mysql command prompt)
Edit the file settings.pyand save the password you require to open your database in DATABASES field as given below in the code.
open terminal.
If you are using virtual environment then switch into it by using command -> workon env_name
Now cd app_nameeg. cd mysite
在 localhost 中创建一个数据库,将其命名为django_db(为此,您可以使用 lamp/wamp/Xampp 或 mysql 命令提示符)
编辑文件settings.py并保存在 DATABASES 字段中打开数据库所需的密码,如下面的代码所示。
打开终端。
如果您使用的是虚拟环境,请使用 command -> workon env_name切换到它
现在cd app_name例如。cd mysite
step 3, 4 and 5 just tells how to reach the folder where manage.py file found in your Application.
第 3、4 和 5 步只是告诉如何到达在您的应用程序中找到 manage.py 文件的文件夹。
6.To check you are in right directory run command ls -l. If you find manage.py file then everything is right go ahead
7.python manage.py migrate
6.要检查您是否在正确的目录中,请运行命令ls -l。如果您找到 manage.py 文件,则一切正常,继续
7.python manage.py 迁移
If you are having any error like- "No module named MySQLdb" in running above command you can visit the link
如果您在运行上述命令时遇到诸如“No module named MySQLdb”之类的错误,您可以访问该链接
8.python manage.py syncdb
9.python manage.py runserver
10.You will be asked for username and password for your Django admin, give whatever you want to open Django admin site.
11.check you are having some tables in your database in localhost at `http://localhost/phpmyadmin. You are good to go now.
8.python manage.py 同步数据库
9.python manage.py 运行服务器
10.您将被要求输入您的 Django 管理员的用户名和密码,提供您想要打开 Django 管理站点的任何内容。
11.检查你的数据库中有一些表在 localhost 的http://localhost/phpmyadmin。你现在可以走了。
回答by hjpotter92
The instruction said to add the
username
with the SQL statement as:
该指令说添加
username
与 SQL 语句为:
GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES ON winestores.* TO [email protected] IDENTIFIED by 'password';
After doing this as root, I quit and tried to log back in with the new user. I got the ERROR 1045. I fixed it by logging back in as root and reissuing the SQL statement again except with
username@localhost
. It worked! I don't know why? Maybe it is the difference between IPs of '127.0.0.1' and 'localhost'???
以 root 身份执行此操作后,我退出并尝试使用新用户重新登录。我收到了错误 1045。我通过以 root 用户身份重新登录并再次重新发出 SQL 语句来修复它,除了
username@localhost
. 有效!我不知道为什么?也许这是'127.0.0.1'和'localhost'的IP之间的区别???
as read on dev.mysql.com.
正如在dev.mysql.com 上阅读的那样。
回答by Dennis Kioko
For anyone encountering this issue - check to make sure that the database user hostis localhost
. If set to %
, it won't work, you will need to add localhost
or change your overall security settings.
对于遇到此问题的任何人 - 检查以确保数据库用户主机是localhost
. 如果设置为%
,它将不起作用,您将需要添加localhost
或更改您的整体安全设置。
回答by jcho360
your DB doesn't recognize your root user and password. If you can go to the phpmyAdmin and try to create a different user and password and assign to the DB, if that is not working try to create but console with create user, and grant all.
您的数据库无法识别您的 root 用户和密码。如果您可以转到 phpmyAdmin 并尝试创建不同的用户和密码并分配给数据库,如果这不起作用,请尝试创建但使用创建用户进行控制台,并授予所有权限。
回答by Sparsh Bansal
This occurs when your Username or password is wrong ....so go through the username and password once again.
当您的用户名或密码错误时会发生这种情况......所以再次输入用户名和密码。
回答by vinnie
FYI if your using XAMPP, go directly into localhost/phpmyad>Privileges to modify this
仅供参考,如果您使用 XAMPP,请直接进入 localhost/phpmyad>Privileges 进行修改