在Windows上使用Django和MySQL

在Windows上使用Django和MySQL,mysql,django,windows-7,Mysql,Django,Windows 7,我刚开始接触Python,并决定下载Django框架,我的工作还不错,但后来我尝试创建了我的第一个“Django应用程序”(Django网站上的教程),当我遇到“数据库设置”时我在使用Windows 7时开始出现问题,在上传到服务器之前,我一直在WAMP中使用MySQL,所以我想我也应该在这里尝试一下,但即使在配置settings.py之后,我还是无法连接它,如下所示: DATABASE_ENGINE = 'mysql' # 'postgresql_psycopg2', 'p

我刚开始接触Python,并决定下载Django框架,我的工作还不错,但后来我尝试创建了我的第一个“Django应用程序”(Django网站上的教程),当我遇到“数据库设置”时我在使用Windows 7时开始出现问题,在上传到服务器之前,我一直在WAMP中使用MySQL,所以我想我也应该在这里尝试一下,但即使在配置settings.py之后,我还是无法连接它,如下所示:

DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'Python'             # Or path to database file if using sqlite3.
DATABASE_USER = 'root'             # Not used with sqlite3.
DATABASE_PASSWORD = ''         # Not used with sqlite3.
DATABASE_HOST = 'localhost'             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this 
# file.
#
[mysqld]

# The TCP/IP Port the MySQL Server will listen on
port=3306
然后我下载并正确安装了MySQLdb,这样我现在就可以进行python导入MySQLdb了,但是我收到了这条消息,我真的不知道如何克服这一点:

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line
362, in execute_manager
    utility.execute()
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line
303, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 195,
 in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 221,
 in execute
    self.validate()
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 249,
 in validate
    num_errors = get_validation_errors(s, app)
  File "C:\Python26\lib\site-packages\django\core\management\validation.py", lin
e 67, in get_validation_errors
    connection.validation.validate_field(e, opts, f)
  File "C:\Python26\lib\site-packages\django\db\backends\mysql\validation.py", l
ine 15, in validate_field
    db_version = connection.get_server_version()
  File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 29
7, in get_server_version
    self.cursor()
  File "C:\Python26\lib\site-packages\django\db\backends\__init__.py", line 81,
in cursor
    cursor = self._cursor()
  File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 28
1, in _cursor
    self.connection = Database.connect(**kwargs)
  File "C:\Python26\lib\site-packages\MySQLdb\__init__.py", line 75, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 170, in __in
it__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'lo
calhost' (10061)")
回溯(最近一次呼叫最后一次):
文件“manage.py”,第11行,在
执行\u管理器(设置)
文件“C:\Python26\lib\site packages\django\core\management\\uuuu init\uuuu.py”,第行
362,在执行管理器中
utility.execute()
文件“C:\Python26\lib\site packages\django\core\management\\uuuu init\uuuu.py”,第行
303,执行中
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“C:\Python26\lib\site packages\django\core\management\base.py”,第195行,
从_argv运行_
self.execute(*args,**选项._dict__;
文件“C:\Python26\lib\site packages\django\core\management\base.py”,第221行,
执行中
self.validate()
文件“C:\Python26\lib\site packages\django\core\management\base.py”,第249行,
有效
num\u errors=get\u validation\u errors(s,app)
文件“C:\Python26\lib\site packages\django\core\management\validation.py”,lin
e 67,在get_验证_错误中
connection.validation.validate_字段(e、opts、f)
文件“C:\Python26\lib\site packages\django\db\backends\mysql\validation.py”,l
第15行,在验证_字段中
db\u version=连接。获取服务器\u版本()
文件“C:\Python26\lib\site packages\django\db\backends\mysql\base.py”,第29行
7,在get_服务器版本中
self.cursor()
文件“C:\Python26\lib\site packages\django\db\backends\\uuuu init\uuuu.py”,第81行,
光标中
cursor=self.\u cursor()
文件“C:\Python26\lib\site packages\django\db\backends\mysql\base.py”,第28行
1,in_光标
self.connection=Database.connect(**kwargs)
文件“C:\Python26\lib\site packages\MySQLdb\\uuuu init\uuuuu.py”,第75行,在Connect中
返回连接(*args,**kwargs)
文件“C:\Python26\lib\site packages\MySQLdb\connections.py”,第170行,在
它__
超级(连接,自).\uuuu初始化(*args,**kwargs2)
_mysql_exceptions.OperationalError:(2003,“无法连接到'lo'上的mysql服务器。”
calhost'(10061)”

我还是应该配置一些东西,还是在某个时候配置错误?另外,我不完全确定WAMP是否应该在这一点上运行,但在Google上找不到任何关于我所面临问题的信息。

我注意到的一件事是,your settings.py没有设置端口#,它说它应该是默认的,但tb没有提到默认的mysql端口3306。也在Windows上。如果尚未验证连接参数,则应验证服务器是否正在运行以及在哪个端口上运行。tb说它甚至找不到一台服务器来尝试连接。如果安装了mysql客户端,请尝试使用正确的端口连接到它,以验证服务器是否在那里。或者telnet 3306查看是否有响应。

查看安装MySQL的目录,可能类似于
C:\Program Files\MySQL\MySQL Server 5.0
。查看my.ini文件。检查服务器部分。它看起来像这样:

DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'Python'             # Or path to database file if using sqlite3.
DATABASE_USER = 'root'             # Not used with sqlite3.
DATABASE_PASSWORD = ''         # Not used with sqlite3.
DATABASE_HOST = 'localhost'             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this 
# file.
#
[mysqld]

# The TCP/IP Port the MySQL Server will listen on
port=3306
您使用的端口号是多少?好的,转到命令行并运行mysql:

C:\Users\foobar>mysql --port=3306 --user=root --password
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.51b-community-nt MySQL Community Edition (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| func                      |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| proc                      |
| procs_priv                |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
| user_info                 |
+---------------------------+
18 rows in set (0.00 sec)
好的,那么你可以使用mysql。进入Django local_settings.py并检查MySQL设置是否正确:

DATABASE_ENGINE = 'mysql'
DATABASE_NAME = 'whatever'
DATABASE_USER = '<special app user>'
DATABASE_PASSWORD = '<password for user>'
DATABASE_HOST = '<ip address or host name of server>'
DATABASE_PORT = '3306'
DATABASE_引擎='mysql'
数据库名称='whatever'
数据库用户=“”
数据库密码=“”
数据库_主机=“”
数据库_端口='3306'

如果您从命令行执行了所有这些操作,那么Django安装程序将正常工作。

您确定MySQL正在运行吗?如果您使用的是WAMP,您可能需要运行它才能使用MySQL。除非你知道你已经单独运行了MySQL。我确定当我点击WAMP图标,然后点击MySQL文件夹,然后点击服务文件夹“开始/恢复服务”,它说“正在运行”,我也尝试过停止它并重新启动它,但仍然没有什么我有同样的问题,只需将:DATABASE_HOST='localhost'替换为DATABASE_HOST='127.0.0.1'修复了以下问题:
Django
local_settings.py
文件在哪里可以找到?@xan:这可能会有帮助