4

I've MySQL running through XAMPP, and I've also installed MySQLdb for python installed. I, however cannot figure out a way of using my XAMPP's MySQL for Python. Each time I execute python manage.py runserver it shows an error:

..2.4c1-py2.7-win32.egg.tmp\MySQLdb\connections.py", line 187, in __init__ _mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

I'm new to Python and these are the settings in settings.py file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'tester',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': 'password',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

I'm using Django. If I use SQLite3 instead of MySQL, it works fine. But I wanted to use MySQL.

EDIT#1

MySQL is using port: 3306. How do I get them working?

4
  • May seem a trivial question, but did you check that MySQL server was actually started and running on your system before trying to connect to the database? Commented Dec 20, 2012 at 9:41
  • Yes. I checked that. MySQL was running when I executed python manage.py runserver command. Commented Dec 20, 2012 at 9:44
  • 1
    You have misunderstood Django's documentation. At no point does it say that it looks for MySQL on port 3306. Port 8000 is where the web application itself is served by the development server by default. And you should notice that in the code you posted above, there is an (empty) entry for PORT. Did you try filling that in? Commented Dec 20, 2012 at 9:59
  • @DanielRoseman:Yes, I filled it in. I tried with 3306 and 8000 both. Still no use. Commented Dec 20, 2012 at 10:03

3 Answers 3

7

After 5-6 hours of trying, I finally got it working.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'tester',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': 'password',                  # Not used with sqlite3.
        'HOST': '127.0.0.1',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '3306',                      # Set to empty string for default. Not used with sqlite3.
    }
}

run command in cmd: python manage.py runserver, and open the webpage.

Sign up to request clarification or add additional context in comments.

Comments

2

Can you check the port because mysql uses default port as 3306 (if not get change manually)

1 Comment

Yes. I just found that. MySQL is using port 3306. But Django's documentation uses 8000. How do I get them working?
2

You most likely need to add a port where mysql is running. This could be that xampp set mysql to another port than standard 3306.

Can you connect to mysql outside django?

You should find some mysql.exe file inside xampp directory I guess. If you navigate to that directory through terminal and then run mysql.exe -uroot -p . Add password, which is most likely a blank password.

7 Comments

Thankyou. I found that the error may be due to ports. How do I change the ports?MySQL is using port 3306. But Django's documentation uses 8000. How do I get them working?
8000 is used for djangos wsgi. So localhost:8000 will be pointed to django. If you manage.py runserver 0.0.0.0 you get localhost without port. If you run manage.py runserver 0.0.0.0:9999 you get localhost:9999
So I now ran python manage.py runserver 0.0.0.0:3306, and navigated to localhost:3306-->Still the webpage is not getting displayed.
3306 is the port the database uses as standard. 0.0.0.0:XXXX is the port the webserver uses. In your DATABASES above you have a key called PORT. You need to add the port where mysql runs. Try my suggestion on opening the database through cms that I suggested above.
I've changed the XAMPP MySQL port to 8000. [Its working]. Now again if I try python manage.py runserver or python manage.py runserver 0.0.0.0:8000 [I've tried both], still the page doesn't open.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.