使用MySQL与Django - 访问被拒绝用户'@'本地主机 [英] Using MySQL with Django - Access denied for user '@'localhost

查看:512
本文介绍了使用MySQL与Django - 访问被拒绝用户'@'本地主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我安装了mysql-server和python- mysqldb'via Synaptic。我更改了settings.py中的相关设置。

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

上面提到的书告诉我们从manage.py shell运行:

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

>>> 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在我第一次安装时要求我设置root密码,这可以在这里使用吗?或者是别的什么?

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?

推荐答案

您的用户无权访问数据库。使用下面的命令设置你的数据库。

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;

此外,您需要有足够的权限来运行它。将其另存为script.sql,然后

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

$mysql -u root -p < script.sql

比在settings.py上你需要确保你的数据库设置被设置正确

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

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

python manage.py syncdb

,你完成了。

这篇关于使用MySQL与Django - 访问被拒绝用户'@'本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆