开发者

Use Django and MySQL on Windows

开发者 https://www.devze.com 2022-12-08 13:51 出处:网络
I am just starting to get into Python and decided to download the Django Framework as well, I have that working just fine but then I tried to create my first \"Django App\" (the tutorial at the Django

I am just starting to get into Python and decided to download the Django Framework as well, I have that working just fine but then I tried to create my first "Django App" (the tutorial at the Django website) and when I ran into the "Database setup" I start having issues I am using Windows 7 and I've always played around with MySQL in WAMP before uploading to the server so I figured I'd try this here as well, but I just couldn't connect it even after configuring the settings.py like this:

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.

I then downloaded and managed to correctly install MySQLdb so I can now do python import MySQLdb however I get this message and I really don't know how to get past this:

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)")

Should I still configure something or did I misconfigured at some po开发者_运维百科int? Also I'm not entirely sure if WAMP should be running at this point =S but haven't been able to find anything on Google as to what's the problem I'm facing.


Look in the directory where you installed MySQL, probably something like C:\Program Files\MySQL\MySQL Server 5.0. Look in the my.ini file. Check the server section. It will look something like this:

# 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

What's the port number you are using? Okay, go to the command line and run 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)

Okay, then you can use mysql. Go into your Django local_settings.py and check that MySQL is set up correctly:

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'

If you did all these things from the command line, then this Django setup will work.


One thing I notice is that your settings.py doesn't set the port #, which says it should be default but the tb doesn't mention the default mysql port which is 3306. Also on Windows. If you haven't already you should verify your connection parameters and that the server is running and on which port. The tb is saying it can't even find a server to try to connect to. If you have the mysql client installed, try to connect to it with the right port to verify that the server is there. Or telnet 3306 to see if you get a response.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号