使用带有Django的MySQL—用户“@”localhost拒绝访问

时间:2022-06-01 12:43:25

So I'm learning Django (1, 3, 1, 'final', 0) through this resource: http://www.djangobook.com/en/2.0/chapter05/

所以我正在通过这个资源学习Django (1, 3, 1, 'final', 0): http://www.djangobook.com/en/2.0/chapter05/。

I installed 'mysql-server' and 'python-mysqldb' via Synaptic. I changed the relevant setting in settings.py.

我通过Synaptic安装了“mysql-server”和“python-mysqldb”。我改变了设置的相关设置。

The book mentioned above tells us to run from the manage.py shell:

上面提到的那本书告诉我们要逃避管理。py壳:

>>> from django.db import connection
>>> cursor = connection.cursor()

I get this error after running these commands:

我在运行这些命令后会得到这个错误:

OperationalError: (1044, "Access denied for user ''@'localhost' to database 'mydb'")

  Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 250, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 322, in _cursor
    self.connection = Database.connect(**kwargs)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (1044, "Access denied for user ''@'localhost' to database 'mydb'")

MySQL did ask me to set a root password when I installed it the first time, would that be utilized here? or is it something else?

MySQL在我第一次安装时要求我设置根密码,这里可以使用吗?还是别的什么?

6 个解决方案

#1


28  

Your user does not have an access to database. Use the commands below to set up your database.

您的用户没有访问数据库的权限。使用下面的命令设置数据库。

DROP DATABASE IF EXISTS `mydb`;
CREATE DATABASE `mydb`
    DEFAULT CHARACTER SET utf8
    DEFAULT COLLATE utf8_general_ci;

USE 'mysql';
GRANT ALL PRIVILEGES ON mydb.* TO 'mydb_user'@'localhost' IDENTIFIED BY 'your_password'

WITH GRANT OPTION;
FLUSH PRIVILEGES;

Also, you need to have enough privileges to run it. Save it as script.sql then,

此外,您需要有足够的权限来运行它。将其保存为脚本。sql之后,

$mysql -u root -p < script.sql

Than on to settings.py where you need to make sure your db settings are set up properly

比设置。py,你需要确保你的db设置设置设置正确

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',                  
        'USER': 'mydb_user',             
        'PASSWORD': 'your_password',                  
        'HOST': '',                     
        'PORT': '',                      
    }
}

and

python manage.py syncdb

and you're done.

你就完成了。

#2


4  

I also ran into this problem while following the Django tutorial on testing (https://docs.djangoproject.com/en/dev/intro/tutorial05/). When trying to test the polls app, I received the following error:

在测试Django教程时,我也遇到了这个问题(https://docs.djangoproject.com/en/dev/intro/tutorial05/)。在尝试测试民意调查应用程序时,我收到了以下错误:

user@host:~/django/mysite$ python manage.py test polls

Creating test database for alias 'default'...
Got an error creating the test database: (1044, "Access denied for user 'admin'@'localhost' to database 'test_django_mysite'")
Type 'yes' if you would like to try deleting the test database 'test_django_mysite', or 'no' to cancel: 

The problem is that django is creating a temporary database called test_django_mysite.
I was able to solve this problem by granting access to 'admin'@'localhost' on the test_django_mysite database that it was trying to create.

问题是django正在创建一个名为test_django_mysite的临时数据库。我可以通过授予对test_django_mysite数据库上的“admin”@“localhost”的访问来解决这个问题。

mysql> GRANT ALL PRIVILEGES ON test_django_mysite.* TO admin@localhost IDENTIFIED BY 'mypassword';
Query OK, 0 rows affected (0.04 sec)

It's worth noting that I only granted the privileges to test_django_mysite. I didn't need to actually create it.

值得注意的是,我只授予test_django_mysite特权。我不需要创建它。

#3


1  

My problem was that I was editing settings.py whereas configuration should have been done in local_settings.py

我的问题是我在编辑设置。而配置应该在local_settings.py中完成

Now everything seems to work.

现在看来一切都很顺利。

#4


0  

The django book is fairly old by now. in particular, database settings are now a dictionary, rather then the bunch of variables described in the django book.

《被解放的姜戈》这本书现在已经相当古老了。特别是,数据库设置现在是一个字典,而不是django书中描述的一堆变量。

looking at the last line of your traceback, it looks like it's trying connect without a username.

查看回溯的最后一行,它看起来像是在尝试没有用户名的连接。

try changing

试着改变

DATABASE_ENGINE = ''
DATABASE_NAME = ''
DATABASE_USER = ''
DATABASE_PASSWORD = ''

to

DATABASES = {
    'default': {
        'ENGINE': '',
        'NAME': ''
        'USER': ''
        # etc
    }
}

#5


0  

If you changed the database password and updated settings.py, you will also have to restart the wsgi.py process, for example by restarting apache.

如果您更改了数据库密码并更新了设置。py,还需要重新启动wsgi。py进程,例如通过重新启动apache。

#6


0  

Just gonna leave my answer here because I lost few good minutes (almost hour) on this matter.

我把答案留在这里,因为我在这件事上浪费了几分钟(几乎是一小时)。

Watch Your copy-paste :) Unfortunately, mysql return success while granting priviliges for not existing user to not existing database...

查看您的复制粘贴:)不幸的是,mysql返回成功,同时为不存在的用户授予特权以不存在的数据库……

Both queries:

这两个查询:

GRANT ALL PRIVILEGES ON python_user.* TO 'python_db'@'%' WITH GRANT OPTION;

and:

和:

GRANT ALL PRIVILEGES ON python_db.* TO 'python_user'@'%' WITH GRANT OPTION

will return success. Aldo I don't have such a user called python_db and db named python_user. :) That's sad. My mistake but also MySql was not helpful enough with output. :)

将返回成功。Aldo,我没有一个名为python_db和db的用户叫做python_user。:)这是悲伤。我的错误和MySql在输出方面也不够有用。:)

#1


28  

Your user does not have an access to database. Use the commands below to set up your database.

您的用户没有访问数据库的权限。使用下面的命令设置数据库。

DROP DATABASE IF EXISTS `mydb`;
CREATE DATABASE `mydb`
    DEFAULT CHARACTER SET utf8
    DEFAULT COLLATE utf8_general_ci;

USE 'mysql';
GRANT ALL PRIVILEGES ON mydb.* TO 'mydb_user'@'localhost' IDENTIFIED BY 'your_password'

WITH GRANT OPTION;
FLUSH PRIVILEGES;

Also, you need to have enough privileges to run it. Save it as script.sql then,

此外,您需要有足够的权限来运行它。将其保存为脚本。sql之后,

$mysql -u root -p < script.sql

Than on to settings.py where you need to make sure your db settings are set up properly

比设置。py,你需要确保你的db设置设置设置正确

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',                  
        'USER': 'mydb_user',             
        'PASSWORD': 'your_password',                  
        'HOST': '',                     
        'PORT': '',                      
    }
}

and

python manage.py syncdb

and you're done.

你就完成了。

#2


4  

I also ran into this problem while following the Django tutorial on testing (https://docs.djangoproject.com/en/dev/intro/tutorial05/). When trying to test the polls app, I received the following error:

在测试Django教程时,我也遇到了这个问题(https://docs.djangoproject.com/en/dev/intro/tutorial05/)。在尝试测试民意调查应用程序时,我收到了以下错误:

user@host:~/django/mysite$ python manage.py test polls

Creating test database for alias 'default'...
Got an error creating the test database: (1044, "Access denied for user 'admin'@'localhost' to database 'test_django_mysite'")
Type 'yes' if you would like to try deleting the test database 'test_django_mysite', or 'no' to cancel: 

The problem is that django is creating a temporary database called test_django_mysite.
I was able to solve this problem by granting access to 'admin'@'localhost' on the test_django_mysite database that it was trying to create.

问题是django正在创建一个名为test_django_mysite的临时数据库。我可以通过授予对test_django_mysite数据库上的“admin”@“localhost”的访问来解决这个问题。

mysql> GRANT ALL PRIVILEGES ON test_django_mysite.* TO admin@localhost IDENTIFIED BY 'mypassword';
Query OK, 0 rows affected (0.04 sec)

It's worth noting that I only granted the privileges to test_django_mysite. I didn't need to actually create it.

值得注意的是,我只授予test_django_mysite特权。我不需要创建它。

#3


1  

My problem was that I was editing settings.py whereas configuration should have been done in local_settings.py

我的问题是我在编辑设置。而配置应该在local_settings.py中完成

Now everything seems to work.

现在看来一切都很顺利。

#4


0  

The django book is fairly old by now. in particular, database settings are now a dictionary, rather then the bunch of variables described in the django book.

《被解放的姜戈》这本书现在已经相当古老了。特别是,数据库设置现在是一个字典,而不是django书中描述的一堆变量。

looking at the last line of your traceback, it looks like it's trying connect without a username.

查看回溯的最后一行,它看起来像是在尝试没有用户名的连接。

try changing

试着改变

DATABASE_ENGINE = ''
DATABASE_NAME = ''
DATABASE_USER = ''
DATABASE_PASSWORD = ''

to

DATABASES = {
    'default': {
        'ENGINE': '',
        'NAME': ''
        'USER': ''
        # etc
    }
}

#5


0  

If you changed the database password and updated settings.py, you will also have to restart the wsgi.py process, for example by restarting apache.

如果您更改了数据库密码并更新了设置。py,还需要重新启动wsgi。py进程,例如通过重新启动apache。

#6


0  

Just gonna leave my answer here because I lost few good minutes (almost hour) on this matter.

我把答案留在这里,因为我在这件事上浪费了几分钟(几乎是一小时)。

Watch Your copy-paste :) Unfortunately, mysql return success while granting priviliges for not existing user to not existing database...

查看您的复制粘贴:)不幸的是,mysql返回成功,同时为不存在的用户授予特权以不存在的数据库……

Both queries:

这两个查询:

GRANT ALL PRIVILEGES ON python_user.* TO 'python_db'@'%' WITH GRANT OPTION;

and:

和:

GRANT ALL PRIVILEGES ON python_db.* TO 'python_user'@'%' WITH GRANT OPTION

will return success. Aldo I don't have such a user called python_db and db named python_user. :) That's sad. My mistake but also MySql was not helpful enough with output. :)

将返回成功。Aldo,我没有一个名为python_db和db的用户叫做python_user。:)这是悲伤。我的错误和MySql在输出方面也不够有用。:)