1
resposta

[Bug] (2002, "Can't connect to MySQL server on '127.0.0.1' (115)")

Quando tento executar o job seguindo até esta aula, me deparo com esse erro.

 ["gunicorn", "to_do.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3"]


 ---> Running in 3841437ec97c

Removing intermediate container 3841437ec97c

 ---> cb356df20194

Successfully built cb356df20194

Tagging built image with django_todolist_image_build:latest
Docker Build Response : cb356df20194
Docker Build Done
provisioning config files...
copy managed file [.env-dev] to file:/var/lib/jenkins/workspace/jenkins-todo-list-principal/to_do/.env
[jenkins-todo-list-principal] $ /bin/sh /tmp/jenkins9493855020280798265.sh
f5cae9d3a45ce4e4c82f9349e4154305550a57feb1b4faaae1f6e13501d17701
Using existing test database for alias 'default'...
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, 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 MySQL server on '127.0.0.1' (115)")

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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    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/commands/test.py", line 26, in run_from_argv
    super().run_from_argv(argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/test.py", line 56, in handle
    failures = test_runner.run_tests(test_labels)
  File "/usr/local/lib/python3.6/site-packages/django/test/runner.py", line 604, in run_tests
    old_config = self.setup_databases()
  File "/usr/local/lib/python3.6/site-packages/django/test/runner.py", line 551, in setup_databases
    self.parallel, **kwargs
  File "/usr/local/lib/python3.6/site-packages/django/test/utils.py", line 174, in setup_databases
    serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/creation.py", line 54, in create_test_db
    self._create_test_db(verbosity, autoclobber, keepdb)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/creation.py", line 164, in _create_test_db
    with self._nodb_connection.cursor() as cursor:
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, 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 216, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, 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 MySQL server on '127.0.0.1' (115)")
todo-list-teste
Build step 'Execute shell' marked build as failure
Finished: FAILURE
1 resposta

Oii, Ivan! Tudo bem contigo? :)

O erro 2002 geralmente ocorre quando o MySQL não consegue se conectar ao servidor local. No seu caso, o erro parece estar ocorrendo porque o container Docker onde você está tentando rodar o MySQL não consegue acessar o serviço MySQL no seu host local.

Uma possível solução para este problema é garantir que o serviço MySQL esteja rodando no seu host local e que o Docker tenha acesso a ele.

No script que você está seguindo no curso, há uma linha que mapeia o socket MySQL do host local para o container Docker:

docker run -d -p 82:8000 -v /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock -v /var/lib/jenkins/workspace/jenkins-todo-list-principal/to_do/.env:/usr/src/app/to_do/.env --name=todo-list-teste django_todolist_image_build

Se a dúvida persistir, estamos aqui no fórum.

Abraços!

Caso este post tenha lhe ajudado, por favor, marcar como solucionado ✓.