Using Django and MySQL on Windows

I'm just starting to get into Python and decided to download the Django Framework, but it works fine for me, but then I tried to create my first "Django app" (tutorial on the Django website) and when I ran into the "database setup". I am having problems using Windows 7 and I have always played with MySQL in WAMP before uploading to the server, so I figured I would try it here as well, but I just couldn't connect it even after setting up my settings.py, like:

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.

      

Then I downloaded and was able to install MySQLdb correctly so that I can now import python MySQLdb, but 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)")

      

Do I still have to configure something or have I misconfigured at some point? Also I'm not really sure if WAMP should work at this = S point, but couldn't find anything on Google regarding the problem I'm having.

+2


source to share


2 answers


Look in the directory where you installed MySQL, maybe something like C:\Program Files\MySQL\MySQL Server 5.0

. Take a look at 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 port number are you using? Ok, go to command line and start 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)

      



Ok then you can use mysql. Go to your Django local_settings.py and make sure MySQL is configured 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've done all of this from the command line, then this Django setup will work.

+2


source


One thing I notice is that your settings.py doesn't set the port # which says it should be the default, but the tb doesn't mention the default mysql port which is 3306. Also on Windows. If you haven't checked your connection parameters yet and that the server is running and on what port. Tb says it can't even find the server to connect to. If you have a mysql client installed, try connecting to it with the correct port to make sure the server exists. Or telnet 3306 to see if you are getting a response.



0


source







All Articles