0
我試圖用django連接mysql。下面的命令可以正常工作。[django + mysql] MySQLdb.connect工作正常,但無法通過settings.py連接mysql
import MySQLdb
>>> con = MySQLdb.Connect(host = "127.0.0.1", port= 3306, user = "root", passwd = "mycode", db = "test")
>>> con.cursor()
<MySQLdb.cursors.Cursor object at 0x020F2230>
>>> cursor = con.cursor()
>>> sql = "SELECT * FROM test.testing"
>>> cursor.execute(sql)
0L
當我嘗試通過settings.py連接數據庫,它打破了,我可以找到有關it.Here的我的配置settings.py中
DATABASES = {
'default':{
'ENGINE' : 'django.db.backends.mysql',
'NAME' : 'tasty',
'USER' : 'root',
'PASSWORD' : 'mycode',
'DATABASE_HOST' : '127.0.0.1',
'DATABASE_PORT' : '3306',
},
}
該錯誤消息我不知道從命令行,如下所示。我懷疑我得到錯誤的原因,是Django無法獲取適當的參數爲數據庫連接。我的環境是win7 32bit,python 2.7.5,django 1.6.7,mysql 5.1,MySQL-python- 1.2.4b4.win32-py2.7.Please有人幫助我,任何建議將不勝感激。
>>> from django.db import connection
>>> cursor = connection.cursor()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 160,
in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 132,
in _cursor
self.ensure_connection()
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 127,
in ensure_connection
self.connect()
File "C:\Python27\lib\site-packages\django\db\utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 127,
in ensure_connection
self.connect()
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 115,
in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 43
5, in get_new_connection
conn = Database.connect(**conn_params)
File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __in
it__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
謝謝,這works.After重新啓動cmd窗口,一切正常fine.Can」我相信我堅持了這樣一個明顯的錯誤。再次感謝。 – Simson 2014-11-23 13:44:15
@Simson,大部分時間我們都錯過了明顯的:)順便說一句,請接受我的答案,如果這能解決您的問題,歡呼 – Anzel 2014-11-23 22:16:09