Forums

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock

Hello Everyone, I read all files about import database and using MySQL. I did the following thing: 1. create database under Database dashboard 2. start the console on the database I created and then source my .sql files 3. set my setting files according to https://help.pythonanywhere.com/pages/UsingMySQL/

However I get the error message: "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock"

my setting file looks like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',  # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'myusername$mydatabase',  # Or path to database file if using sqlite3.
        'USER': 'myusername',
        'PASSWORD': 'mydatabasepassword',
        'HOST': 'mysql.sever',  # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',  # Set to empty string for default.
    }
}

I searched all similar questions and tried all solutions provided under those questions, but it doesn't work. Could you please give me any suggestions?

THANK YOU!

You need to set HOST to the hostname from your "Databases" tab, then reload your web app.

By the way, where did you get mysql.sever? We deprecated that hostname over a year ago, and it won't work now.

Hi giles, In my real setting files, I set the host name as Database host address showed in database tab, not mysql.sever. And I reload the web app every time I make changes. But it just not work.

Thank you!

Could I take a look at your code? We can see it from our side but we always ask for permission first.

Yes, sure. Go ahead. Thank you!

Yes, sure. Go ahead. Thank you!

It looks like you have two settings files, settings.py and local_settings.py, and the settings in the latter overriding the ones in the former -- so far, a pretty normal configuration. But the DATABASES setting you have in local_settings.py doesn't have a HOST in it -- that's causing the problem.

Thank you! Problem solved!

Thanks for confirming!

Hi, I am having same problem. Database settings are correct and I don't have extra settings file. Is there anything else that can cause this problem. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

If you're seeing that error, it's because your database settings are not correct. Have you checked that the settings that you think you're applying are actually the ones that Django is using? Have you reloaded your web app so that you're sure that the most recent settings have been loaded?

i have the same error the host name in setting.py same as host name in databse tab

@AWN where do you see this error?

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) i am getting this error in bash console when i use mysql -u username -p entre password: mypassword the n this error came can you please help me out

The command should be mysql -u USERNAME -h HOSTNAME -p 'USERNAME$DATABASENAME', where:

  • the USERNAME is the username you use to log in to PythonAnywhere
  • the HOSTNAME is on your Databases tab
  • the 'USERNAME$DATABASENAME' is the full name of your database, which comprises your username, then a dollar sign, then the name you gave it.

See: https://help.pythonanywhere.com/pages/UsingMySQL/.