Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL for Python on Windows via Xampp

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?

like image 713
xan Avatar asked Dec 20 '22 12:12

xan


2 Answers

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.

like image 113
xan Avatar answered Dec 28 '22 07:12

xan


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

like image 21
Varun Bajaj Avatar answered Dec 28 '22 06:12

Varun Bajaj