question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

django.db.utils.OperationalError: (2002, 'Can\'t connect to local MySQL server through socket \'/run/mysqld/mysqld.sock\' (2 "No such file or directory")')

See original GitHub issue

not really understand why its happened my DATABASE_URL mysql://promgen:xxxxxxxxxxxxxxx@localhost/promgen

ubuntu@prometheus:~/.config/promgen$ docker run --rm -v ~/.config/promgen:/etc/promgen/ line/promgen migrate
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 195, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
    return Database.connect(**conn_params)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/__init__.py", line 84, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 164, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2002, 'Can\'t connect to local MySQL server through socket \'/run/mysqld/mysqld.sock\' (2 "No such file or directory")')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/bin/promgen", line 11, in <module>
    load_entry_point('promgen', 'console_scripts', 'promgen')()
  File "/usr/src/app/promgen/manage.py", line 20, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 361, in execute
    self.check()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 64, in _run_checks
    issues = run_checks(tags=[Tags.database])
  File "/usr/local/lib/python3.6/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python3.6/site-packages/django/core/checks/database.py", line 10, in check_database_backends
    issues.extend(conn.validation.check(**kwargs))
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 9, in check
    issues.extend(self._check_sql_mode(**kwargs))
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
    with self.connection.cursor() as cursor:
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 256, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 233, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 195, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
    return Database.connect(**conn_params)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/__init__.py", line 84, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 164, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2002, 'Can\'t connect to local MySQL server through socket \'/run/mysqld/mysqld.sock\' (2 "No such file or directory")')

(Edited to format as code block)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
kfdmcommented, Aug 4, 2019

Can you try using an IP address (I think even 127.0.0.1 should work) instead of localhost? I believe mysql has localhost hardcoded to use a unixsocket instead of tcp connection

0reactions
saqibarfeencommented, Dec 31, 2021

Unfortunately, I’m not super familiar with all the details of docker, so I may not be able to help too much, I can give you some of the settings I use as a reference.

In my case, I develop Promgen locally on my machine, and use docker for my database

For my connection string, I use something similar to what you’re using mysql://promgen:xxx@127.0.0.1/promgen

though because the database is running in a container, the grant line from the database’s perspective is a bit different

GRANT ALL PRIVILEGES ON `promgen`.* TO 'promgen'@'172.17.0.1' WITH GRANT OPTION
GRANT ALL PRIVILEGES ON `test\_promgen`.* TO 'promgen'@'172.17.0.1' WITH GRANT OPTION

Though in your case, since it’s saying connection refused I wonder if it’s failing before it can even check the password. Can you check the port and ip that your database is running on ? Also see if you need to use --link to make sure Promgen is linked to the database container it needs to run

Thanks, very helpful, worked for me. It was that promgen runs in a container itself, so 127.0.0.1 means promgen not the mysql container. So, either use docker network and set hostname e.g mysql I copied the mysql container’s IP from $docker inspect mysql , and pasted that IP in the ~/.config/promgen/DATABASE_URL file, and ran the migrate command again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

(2002, "Can't connect to local MySQL server through socket ...
I faced this problem when connecting MySQL with Django when using Docker. Try 'PORT':'0.0.0.0' . Do not use 'PORT': 'db' . This will...
Read more >
Can't connect to local MySQL server through socket [SOLVED ...
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 “No such file or directory”). In this short article, ...
Read more >
SOLVED! “Can't connect to local MySQL server through socket ...
I got this error yesterday when trying to connect to my mysql database after getting it running with NodeJS. There is a lot...
Read more >
How to solve "Can't connect to local MySQL server through ...
It was working fine before I changed the default data directory. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib...
Read more >
Can't connect to local MySQL server through socket '/var/run ...
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2). Hello all,. I am trying to access ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found