0

I am working on a Django project without docker, and I am using Mysql database for local and production environements.

I am using windows 10 OS and I also have WSL on my windows. My Python version for windows is 3.9.10 and my python version for WSL is 3.8.10.

When I was working with 3.9.10 using Windows command line, there was no issue with mysql connection and project was running successfully, however, I lost some requirement dependencies. I was not allowed to loose those dependecies by my client.

So I asked my client about python version they were already using and it was python3.8.10 and it worked using WSL and I didn't lose any requirement dependency.

My Problem:

Now the problem is I am facing issue of django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket when I run python3 manage.py runserver after installing all the requirements successfully.

I have searched a lot and I came to know that there is something wrong in my.cnf file.

I searched for my.cnf file in MySql/etc/my.cnf folder but there is no file in the whole mysql folder. Can I create a my.cnf file manually?

NOTE: Mysql8 is already running.

How can I fix this issue so that I can resolve my mysql connection issue?

Traceback Error:

Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
    self.check_migrations()
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/core/management/base.py", line 564, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/db/migrations/loader.py", line 58, in __init__
    self.build_graph()
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/db/migrations/loader.py", line 235, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
    if self.has_table():
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 57, in has_table
    with self.connection.cursor() as cursor:
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 323, in cursor
    return self._cursor()
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 299, in _cursor
    self.ensure_connection()
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 282, in ensure_connection
    self.connect()
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 282, in ensure_connection
    self.connect()
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 263, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 247, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/MySQLdb/__init__.py", line 123, in Connect
    return Connection(*args, **kwargs)
  File "/mnt/c/Users/Dell/Documents/HRMS-Backend/venv/lib/python3.8/site-packages/MySQLdb/connections.py", line 185, in __init__
    super().__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
`
```

1 Answers1

0

First, find your /etc/my.cnf file. You can get some help by this SO.

After that, you should find your socket file. By running this command, you can get all socket files:

sudo find / -type s

When you found your MySQL socket file, add it in your my.conf. For example:

socket=/var/lib/mysql/mysql.sock

You can find additional info in SO.

Amin
  • 2,605
  • 2
  • 7
  • 15