Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django connects to mysql even when port set to incorrect one

Tags:

mysql

django

I am trying to make Django connect to my localhost MySQL on port 3307. I've noticed that when I change the port number in settings.py, it still connects on 3306. So I tried to change it to some nonsensical value and it still keeps connecting on 3306. I am using WSGI on Apache2 and I tried to restart the server, too. Is there anything else I have to do to make Django connect on the desired port? Here is my settings.py:

'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'mydb',                # Or path to database file if using sqlite3.
        'TEST_NAME': 'test_mydb',
        # The following settings are not used with sqlite3:
        'USER': 'mydb',
        'PASSWORD': 'mydb',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '33074',                      # Set to empty string for default.
    }
like image 728
pkout Avatar asked Dec 21 '22 07:12

pkout


1 Answers

You are not connecting through port 3306; you are connecting through a local socket.

MySQL default behavior is to connect through a local socket (a UNIX system for processes to communicate with each other very quickly that does not involve the networking stack at all) if you leave "host" blank. If you want to explicitly use the networking stack to connect to localhost, then you have to specify "127.0.0.1" as the host. Once you do that, the port selected in configuration will become relevant.

like image 174
Andrew Gorcester Avatar answered Feb 22 '23 23:02

Andrew Gorcester