Django:mysql:1045,“用户拒绝访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2443419/
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
Django : mysql : 1045, "Access denied for user
提问by PlanetUnknown
I have the whole setup working for months on my local computer.
I'm installing on a remote site now.
Created a fresh mysql DB, and created a new user ("someuser") and gave it complete grants, like so -
我的整个设置在我的本地计算机上工作了几个月。
我现在正在远程站点上安装。
创建了一个新的 mysql 数据库,并创建了一个新用户(“someuser”)并给了它完整的授权,就像这样 -
GRANT ALL PRIVILEGES ON .TO 'someuser'@'localhost' IDENTIFIED BY 'somepassword' WITH GRANT OPTION;
授予所有特权。TO 'someuser'@'localhost' IDENTIFIED BY 'somepassword' WITH GRANT OPTION;
I have sync'd the db, using "python manage.py syncdb" and the correct tables were created. My settings.py has this same user.
我已经同步了数据库,使用“python manage.py syncdb”并创建了正确的表。我的 settings.py 有这个相同的用户。
But when I try to login a user through the application, and it hits the DB, I see the following in the logs -
但是,当我尝试通过应用程序登录用户并访问数据库时,我在日志中看到以下内容 -
(1045, "Access denied for user 'someuser'@'localhost' (using password: YES)")
(1045,“用户 'someuser'@'localhost' 的访问被拒绝(使用密码:YES)”)
I logged in through mysql (installed on the same box as django) and checked the grants and it correctly shows -
我通过 mysql(与 django 安装在同一个盒子上)登录并检查了授权,它正确显示 -
Grants for someuser@localhost
GRANT ALL PRIVILEGES ON * . * TO 'someuser'@'localhost' IDENTIFIED BY PASSWORD '*thesaltedpasswordOverHere' WITH GRANT OPTION
授予 someuser@localhost
GRANT ALL PRIVILEGES ON * 。* TO 'someuser'@'localhost' IDENTIFIED BY PASSWORD '*thesaltedpasswordOverHere' WITH GRANT OPTION
I don't want to use the root user/password for django, since it doesn't seem the correct way.
我不想为 django 使用 root 用户/密码,因为它似乎不是正确的方法。
Any pointers as to what might be wrong ?
关于可能出什么问题的任何指示?
回答by duffymo
I do it like this for a database named foo_db:
对于名为 foo_db 的数据库,我是这样做的:
create database foo_db;
create user foo_user identified by 'foo_password';
grant all on foo_db.* to 'foo_user'@'%';
flush privileges;
回答by Maritza Esparza
In my case the settings.py has the following:
在我的情况下,settings.py 具有以下内容:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'TRYDJANGO',
'USERNAME':'user_trydjango',
'PASSWORD':'passtry',
'PORT':'3306',
'HOST': 'localhost',
}
}
And it works if I change the 'USERNAME' to 'USER':
如果我将“用户名”更改为“用户”,它会起作用:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'TRYDJANGO',
'USER':'user_trydjango',
'PASSWORD':'passtry',
'PORT':'3306',
'HOST': 'localhost',
}
}
回答by ReturnHttp402
Refering to the answer of @duffymo and changed the last statement could work for me.
参考@duffymo 的答案并更改最后一条语句可能对我有用。
create database foo_db;
create user foo_user identified by 'foo_password';
grant all on foo_db.* to foo_user@localhost identified by 'foo_password' with grant option;
回答by Ivan Gruer
as far as i understood someuser is a DB guest user and not the admin one, right?
据我所知,someuser 是 DB 来宾用户而不是管理员,对吗?
If so, from your MySQL DB admin user, grant someuser to access MySQL environment table 'mysql.user' as follow:
如果是这样,请从您的 MySQL 数据库管理员用户中授予 someuser 访问 MySQL 环境表 'mysql.user' 的权限,如下所示:
GRANT SELECT ON mysql
.user
TO 'someuser'@'%';
授予选择mysql
。user
TO 'someuser'@'%';
To me it worked, Ivan
对我来说它奏效了,伊万
回答by Csaba Toth
My error message indicated that there wasn't password supplied to the database:
我的错误消息表明没有向数据库提供密码:
django.db.utils.OperationalError: (1045, "Access denied for user 'mylocalusername'@'localhost' (using password: NO)")
django.db.utils.OperationalError: (1045, “访问被拒绝用户 'mylocalusername'@'localhost'(使用密码:NO)”)
The manage.py
should pick up the database access credentials from the settings.py
, that's a specific database user (and of course that user's password as well). It's a security mistake to react to my error message by granting database login privileges to the local user.
本manage.py
应该拿起从该数据库访问权限settings.py
,这是一个特定的数据库用户(当然该用户的密码以及)。通过向本地用户授予数据库登录权限来响应我的错误消息是一个安全错误。
In my case there were problems with the settings.py
(we were restructuring our build pipeline from django pipeline to webpack) and I needed to remove pipeline related parts from my settings.py
for the fix. It's a question why I didn't get any python error message about the settings.py
problems, but instead I got this error which is not local to the real problem. That error message is just a transitive result of the source problem. Once I fixed the manage.py
, the credentials were picked up from the DATABASE settings as usual and everything went smooth.
就我而言,settings.py
(我们正在将构建管道从 django 管道重构为 webpack)存在问题,我需要从我settings.py
的修复中删除管道相关部分。这是一个问题,为什么我没有收到关于这些settings.py
问题的任何 python 错误消息,而是我收到了这个不是真正问题本地的错误。该错误消息只是源问题的传递结果。一旦我修复了manage.py
,像往常一样从 DATABASE 设置中获取凭据,一切都很顺利。
In our case we were using django-pipeline
before webpack (more specifically pip
packages django-pipeline-browserify==0.4.1
and
django-pipeline==1.6.8
), so once we transitioned I had to just remove these lines from settings:
在我们的例子中,我们django-pipeline
在 webpack 之前使用(更具体地说是pip
包django-pipeline-browserify==0.4.1
和
django-pipeline==1.6.8
),所以一旦我们转换,我只需要从设置中删除这些行:
NODE_MODULES_BIN = '/home/myuser/sourcerepositorydir/node_modules/.bin/'
PIPELINE['SASS_BINARY'] = '/var/lib/gems/2.3.0/gems/sass-3.5.3/bin/sass'
PIPELINE['BABEL_BINARY'] = '{}{}'.format(NODE_MODULES_BIN, 'babel')
PIPELINE['BROWSERIFY_BINARY'] = '{}{}'.format(NODE_MODULES_BIN, 'browserify')
PIPELINE_BROWSERIFY_BINARY = PIPELINE['BROWSERIFY_BINARY']
PIPELINE['BROWSERIFY_ENV'] = {'NODE_ENV': 'development'}
PIPELINE['BROWSERIFY_ARGUMENTS'] = PIPELINE['BROWSERIFY_ARGUMENTS'] + ' --debug'
Until that I was just getting nonsensical error messages.
在那之前,我只是收到了无意义的错误消息。
回答by JoJo_wen
I use this config and it works.
我使用此配置并且它有效。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/etc/my.cnf',
},
#'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
回答by Eric
It seems that all I have done is:
似乎我所做的只是:
- drop my database
- drop the user
- re-create the user
- re-create the database
- grant to this user, flush privileges
- 删除我的数据库
- 删除用户
- 重新创建用户
- 重新创建数据库
- 授予此用户,刷新权限
and then it begin to work.
然后它开始工作。
回答by Bilal Ahmed Yaseen
I got the following error:
我收到以下错误:
ModuleNotFoundError: No module named 'MySQLdb' django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?
ModuleNotFoundError:没有名为“MySQLdb”的模块 django.core.exceptions.ImproperlyConfigured:加载 MySQLdb 模块时出错。你安装了mysqlclient吗?
Solution # 01:Verify that If you user has permission to access the database and perform the DDL & DML operations on it.
解决方案#01:验证您的用户是否有权访问数据库并对其执行 DDL 和 DML 操作。
Solution # 02:Changed the database configuration in settings.pyfile
解决方案#02:更改settings.py文件中的数据库配置
From:
从:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'root'
}
}
To:
到:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
Andit started working.
而且它开始工作。
回答by Vijay
Also change the port number to '3307'if MySQl-python 64-bit in settings.py, Then only the connection happening in Windows and MySql 5.7 django.
如果在 settings.py 中的 MySQl-python 64 位,也将端口号更改为“3307”,然后仅在 Windows 和 MySql 5.7 django 中发生连接。
'3306' for MySQl-python 32-bit
'3306' 用于 MySQl-python 32 位
DATABASES = {
'default': {
'NAME': 'graphite',
'ENGINE': 'django.db.backends.mysql',
'USER': 'graphite',
'PASSWORD': 'thepasswordyouchoose',
'HOST': 'localhost',
'PORT': '3307'
}
}
回答by GeWi
@ mattblang:
@马特布朗:
In this case it helps if the host is set in the actually used settings.py
file too.
在这种情况下,如果主机也设置在实际使用的settings.py
文件中会有所帮助。
For me it is /etc/graphite/local_settings.py
where the DATABASES Stanzas
had a wrong value, for HOST there was:
'HOST': '127.0.0.1',
对我来说,/etc/graphite/local_settings.py
当DATABASES Stanzas
有一个错误值,HOST有:
'HOST': '127.0.0.1',
Because during the runtime of syncdb command it searched for localhost, I changed it to
'HOST': 'localhost',
因为在syncdb命令运行期间它搜索本地主机,我将其更改为
'HOST': 'localhost',
Now it looks something like this:
现在它看起来像这样:
DATABASES = {
'default': {
'NAME': 'graphite',
'ENGINE': 'django.db.backends.mysql',
'USER': 'graphite',
'PASSWORD': 'thepasswordyouchoose',
'HOST': 'localhost',
'PORT': '3306'
}
}
... and now the syncdb
command python manage.py syncdb
runs successfully.
...现在syncdb
命令python manage.py syncdb
成功运行。