Fernando Freitas Alves
Fernando Freitas Alves

Reputation: 3777

Can't connect to MySQL with Django using portable Xampp

I need to test a Django application that uses mysql, so I installed portable-xampp and loaded the database, but I can't access it with django. It works when I'm using the remote database, but not locally. The user, password and database are OK and I also tried using root, just for test purposes Any thoughts?

In settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'awe_db',            # Or path to database file if using sqlite3.
        'USER': 'awe_db',                      # Not used with sqlite3.
        'PASSWORD': 'mypwd',                  # 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.
    }
}

Error:

Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 2.6.1\helpers\pycharm\django_manage.py", line 21, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Python27\lib\runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Users\Fernando Alves\Dropbox\Projetos\nova_b2blue\manage.py", line 14, in <module>
                                                                                    execute_manager(settings)
File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line 459, in execute_manager
utility.execute()
File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 231, in execute
self.validate()
File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "C:\Python27\Lib\site-packages\django\core\management\validation.py", line 103, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "C:\Python27\Lib\site-packages\django\db\backends\mysql\validation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "C:\Python27\Lib\site-packages\django\db\backends\mysql\base.py", line 415, in get_server_version
self.cursor().close()
File "C:\Python27\Lib\site-packages\django\db\backends\__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Python27\Lib\site-packages\django\db\backends\mysql\base.py", line 387, in _cursor
self.connection = Database.connect(**kwargs)
File "C:\Python27\Lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\Lib\site-packages\MySQLdb\connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

Upvotes: 2

Views: 1426

Answers (1)

Samudra
Samudra

Reputation: 1213

For the benefit of someone who finds this from Google, try this:

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'dbname', 
    'USER': 'user',
    'PASSWORD': 'password',
    'HOST': '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock', 
    'PORT': '7777', 
  }
}

The mysql.sock file is the key here.

Upvotes: 5

Related Questions