使用帶有Django的MySQL—用戶“@”localhost拒絕訪問

[英]Using MySQL with Django - Access denied for user '@'localhost


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在輸出方面也不夠有用。:)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/06/23/7128088a9b85970aa7cdd7aef87c0501.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com