0

I'm working on my first Django project and I kept hitting the limits of sqlite. So I decided to switch to MySQL before adding lots of additional test data. Ever since no page in my Django app loads anymore (neither the admin nor the project pages). I get no error message until Chrome eventually displays "ERR_CONNECTION_REFUSED".

I added the following to my settings:

    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'db_name',
    'USER': 'user',
    'PASSWORD': 'password',
    'HOST': '127.0.0.1',
    'PORT': '3306',
    'OPTIONS': {
       'read_default_file': os.path.join(BASE_DIR, 'my.cnf'),
    },

The my.cnf file referenced above looks like this:

[client]
database = dbname
user = username
password = password
default-character-set = utf8

[mysqld]
max_allowed_packet = 512M

wait_timeout = 604800
interactive_timeout = 28800

It's definetely being used, as changing the database name in the file causes Django to connect to a different db.

The project uses Celery, so I also modified the Celery result backend:

CELERY_RESULT_BACKEND = 'db+mysql://user:password@localhost:3306/dbname'

Django can connect to the database (see tests like this one: how can I check database connection to mysql in django). I migrated my content according the second answer to this question: What's the best way to migrate a Django DB from SQLite to MySQL? without any errors. All data are present in phpMyAdmin. There's nothing in the MySQL error log. Neither Django nor Celery outputs any error in the debug console/command line. I installed mysqlclient and (for Celery) SQLAlchemy.

Switching back the the original sqlite configuration remedies the problem, so it's definitely linked to the MySQL db. What am I overlooking? Have I missed some vital configuration step? Is there a way to get more error messages? Especially the complete lack of the latter really has me flummoxed.

Trying to get objescts from the database bia the Python console results in ´mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away'), which once more points to a MySQL issue. Closing old connections fixed this error, which did not reappear so far, but the page still doesn't load.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
user7398455
  • 111
  • 1
  • 8
  • What's the error stack trace; please include that in your question – rtindru May 05 '18 at 09:57
  • @rtindru There is no error. That's my biggest problem, as I don't know what to test or change. – user7398455 May 05 '18 at 10:01
  • `python manage.py runserver` executes without issue? – rtindru May 05 '18 at 10:02
  • @rtindru Yes. "System check identified no issues (0 silenced).", et cetera – user7398455 May 05 '18 at 10:03
  • Can you run `python manage.py check` once? Are you sure you're hitting the right endpoint in chrome? – rtindru May 05 '18 at 10:05
  • @rtindru Check identifies no issues. And I'm sure, renaming the old sqlite configuration to "default" and reloading the same URL works. – user7398455 May 05 '18 at 10:09
  • Are you able to connect to the local mysql instance using `mysql -u root -p -h 127.0.0.1`? – rtindru May 05 '18 at 10:24
  • @rtindru Yes, I am. Testing the connection via PyCharm also works, as does connecting to the db via the python console. But trying to get objects from the database results in `mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')`, so that's progress. – user7398455 May 05 '18 at 10:39
  • Which version of MySQL are you using? There are a number of reasons for this issue: https://dev.mysql.com/doc/refman/8.0/en/gone-away.html Can you check if the user `user` in your case has permissions for the databases you need? You can do a `SHOW GRANTS FOR 'user'@'localhost'`. Also that the databases indeed exist? – rtindru May 07 '18 at 06:22
  • Also can you confirm that your MySQL server is running on the TCP port and not on a socket? Do a `ps aux | grep mysql` and share the dump here, also do a `cat my.cnf` and share the same here. – rtindru May 07 '18 at 06:24
  • @rtindru Closing old conenctions fixed this issue. It was likely conencted to Celery, as I found several reports of long Celery tasks leading to this error. So I'm back to my starting point - I can execute runserver, I can query the db via the Django console and get the expected results, but no page loads, the PyCharm Debugger won't load any variables and no error message shows anywhere. – user7398455 May 07 '18 at 09:34

0 Answers0