mysql_exceptions。OperationalError:(1045,“访问被拒绝的用户根”@“localhost”(使用密码:YES)”)

时间:2021-12-03 20:11:11

I've started working with django, and here is the error on 'runserver' after setting up 'DATABASES' in settings.py

我已经开始和django一起工作,在设置“数据库”之后,这里是“runserver”的错误。

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

My settings.py portion of code:

我的设置。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?

什么错了吗?

5 个解决方案

#1


13  

Run in console

运行在控制台

mysql> grant all privileges on *.* to root@localhost identified by 'password' with grant option;

#2


3  

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-

您的设置。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.

注意:上面给出的密码和用户名是我的本地主机,您可以将其更改为您的。

  1. create a database in localhost let name it django_db (for this you can use lamp/wamp/Xampp or mysql command prompt)

    在localhost中创建一个数据库,让它命名为django_db(为此您可以使用lamp/wamp/Xampp或mysql命令提示符)

  2. Edit the file settings.py and save the password you require to open your database in DATABASES field as given below in the code.

    编辑该文件设置。py并保存您需要在数据库字段中打开数据库的密码,如下面的代码所示。

  3. open terminal.

    打开终端。

  4. If you are using virtual environment then switch into it by using command -> workon env_name

    如果您正在使用虚拟环境,那么请使用命令-> workon env_name切换到它。

  5. Now cd app_name eg. cd mysite

    现在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告诉如何到达管理的文件夹。在您的应用程序中发现的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

6。要检查您是否处于正确的目录运行命令ls -l。如果你发现管理。py文件然后一切都是正确的。

7.python manage.py migrate

7所示。python管理。py迁移

If you are having any error like- "No module named MySQLdb" in running above command you can visit the link

如果您有任何错误,比如在运行上述命令时,“没有一个名为MySQLdb的模块”,您可以访问该链接。

8.python manage.py syncdb

8。python管理。py syncdb

9.python manage.py runserver

9。python管理。py runserver

10.You will be asked for username and password for your Django admin, give whatever you want to open Django admin site.

10。您将被要求为您的Django管理员提供用户名和密码,您想要打开Django管理站点。

11.check you are having some tables in your database in localhost at `http://localhost/phpmyadmin. You are good to go now.

11。检查您的数据库中有一些表在localhost的“http://localhost/phpmyadmin”中。你现在可以走了。

#3


1  

The instruction said to add the username with the SQL statement as:

该指令表示将用户名添加到SQL语句中:

GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES ON winestores.* TO username@127.0.0.1 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'???

在做了根目录之后,我退出并尝试重新登录到新用户。我得到了错误1045。我将它作为根重新登录,并再次重新发布SQL语句,不过使用username@localhost。它工作!我不知道为什么?也许是“127.0.0.1”和“localhost”之间的区别?

as read on dev.mysql.com.

在dev.mysql.com上为已读。

#4


0  

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.

您的DB不识别您的根用户和密码。如果您可以访问phpmyAdmin并尝试创建一个不同的用户和密码,并分配给DB,如果这不是工作尝试创建,而是创建用户的控制台,并授予all。

#5


0  

For anyone encountering this issue - check to make sure that the database user host is localhost . If set to %, it won't work, you will need to add localhost or change your overall security settings.

对于遇到此问题的任何人,请检查以确保数据库用户主机是本地主机。如果设置为%,它将不起作用,您将需要添加localhost或更改整个安全设置。

#1


13  

Run in console

运行在控制台

mysql> grant all privileges on *.* to root@localhost identified by 'password' with grant option;

#2


3  

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-

您的设置。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.

注意:上面给出的密码和用户名是我的本地主机,您可以将其更改为您的。

  1. create a database in localhost let name it django_db (for this you can use lamp/wamp/Xampp or mysql command prompt)

    在localhost中创建一个数据库,让它命名为django_db(为此您可以使用lamp/wamp/Xampp或mysql命令提示符)

  2. Edit the file settings.py and save the password you require to open your database in DATABASES field as given below in the code.

    编辑该文件设置。py并保存您需要在数据库字段中打开数据库的密码,如下面的代码所示。

  3. open terminal.

    打开终端。

  4. If you are using virtual environment then switch into it by using command -> workon env_name

    如果您正在使用虚拟环境,那么请使用命令-> workon env_name切换到它。

  5. Now cd app_name eg. cd mysite

    现在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告诉如何到达管理的文件夹。在您的应用程序中发现的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

6。要检查您是否处于正确的目录运行命令ls -l。如果你发现管理。py文件然后一切都是正确的。

7.python manage.py migrate

7所示。python管理。py迁移

If you are having any error like- "No module named MySQLdb" in running above command you can visit the link

如果您有任何错误,比如在运行上述命令时,“没有一个名为MySQLdb的模块”,您可以访问该链接。

8.python manage.py syncdb

8。python管理。py syncdb

9.python manage.py runserver

9。python管理。py runserver

10.You will be asked for username and password for your Django admin, give whatever you want to open Django admin site.

10。您将被要求为您的Django管理员提供用户名和密码,您想要打开Django管理站点。

11.check you are having some tables in your database in localhost at `http://localhost/phpmyadmin. You are good to go now.

11。检查您的数据库中有一些表在localhost的“http://localhost/phpmyadmin”中。你现在可以走了。

#3


1  

The instruction said to add the username with the SQL statement as:

该指令表示将用户名添加到SQL语句中:

GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES ON winestores.* TO username@127.0.0.1 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'???

在做了根目录之后,我退出并尝试重新登录到新用户。我得到了错误1045。我将它作为根重新登录,并再次重新发布SQL语句,不过使用username@localhost。它工作!我不知道为什么?也许是“127.0.0.1”和“localhost”之间的区别?

as read on dev.mysql.com.

在dev.mysql.com上为已读。

#4


0  

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.

您的DB不识别您的根用户和密码。如果您可以访问phpmyAdmin并尝试创建一个不同的用户和密码,并分配给DB,如果这不是工作尝试创建,而是创建用户的控制台,并授予all。

#5


0  

For anyone encountering this issue - check to make sure that the database user host is localhost . If set to %, it won't work, you will need to add localhost or change your overall security settings.

对于遇到此问题的任何人,请检查以确保数据库用户主机是本地主机。如果设置为%,它将不起作用,您将需要添加localhost或更改整个安全设置。