django连接mysql指定用户,使用MySQL与Django - 访问被拒绝用户'@'本地主机

So I'm learning Django (1, 3, 1, 'final', 0) through this resource: 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.

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 "", line 1, in

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?

解决方案

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,

$mysql -u root -p < script.sql

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': '',

}

}

and

python manage.py syncdb

and you're done.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值