Forums

Django 1.9, Python 3.4, and MySQL

Hi, I'm currently trying to configure my web app to run a mysql backend with python 3.4. I have installed mysqlclient, but I continue to get the following error:

> File "/home/rchurch4/.virtualenvs/django19/lib/python3.4/site-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
> File "/home/rchurch4/.virtualenvs/django19/lib/python3.4/site-packages/MySQLdb/connections.py", line 204, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
> django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

Basically, it is still trying to load MySQLdb, even though I've installed mysqlclient. Am I supposed to be editing anything else that every thread on this has forgotten to mention?

Here's my settings file (the part that matters):

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'rchurch4$xxx',
        'USER': 'rchurch4',
        'PASSWORD': 'xxx',
        'HOST': 'rchurch4.mysql.pythonanywhere-services.com',
    } }

I've looked through all the pythonanywhere tutorials as well as a few stackoverflow questions that were similar to this question, and not a single one has remedied this problem. Thanks in advance for the help.

Your error sounds like you are trying to connect to a local mysql server instead of the rchurch4.mysql.pythonanywhere-services.com one.

What code are you running that generates that error? Is it the your django webapp? Or are you trying to connect using a script you wrote? Note that that wouldn't read your django settings/use the django db.

Conrad,

My webapp runs as configured, but I get an http 500 when I try to connect to a page that retrieved information from the database. I get the posted error when I try to run makemigrations or any other similar manage.py command.

Be sure to take a look at your error log to debug why you are getting 500's. I took a look at the log that just got rotated and it seems to say that you are missing a database table.

This makes sense because you have not run makemigrations + migrate yet (which creates those database tables on your mysql database).

Here are some potential steps to debug: 1. check if you can open a console to the database (from the databases tab)- connect to the database and check to see if the tables exist/don't exist 2. try connecting in a bash terminal to the database by doing mysql -u USERNAME -h HOSTNAME -p 3. start a manage.py shell, and double check that you do have the correct database settings (by importing settings and looking at DATABASES)

Conrad,

I had a feeling this was going to be the problem last night as I went to bed, but I did not know how to address it: The settings file that was being used by manage.py was the development settings. Thanks to your help with the pointers to where to debug, I was able to figure out how to temporarily change which settings are used. So problem solved. Thank you very much.

Coming off of that, not a huge deal, more of a convenience thing: Do you know how to permanently set the settings to my production settings file?

Thanks very much for your help,

Rob

I don't understand the distinction you're making between "temporarily" and "permanently" changing the settings your app is using. Could you be more specific about what you mean?

Glenn,

By temporary I mean that when I open a new bash console the django settings module resets to the default settings file, which is not what I want for production

  • Rob

Nope. Still confused. How are you specifying your settings file? Surely you can just change the settings that you're pointing to or edit the file that contains the settings?