1

I have a redhat server with docker installed I want to create a docker image in which I want to run django with MySQL but the problem is django is unable to connect to MySQL server(remote server). I'm getting following error:

 Plugin caching_sha2_password could not be loaded: /usr/lib/x86_64-linux-gnu/mariadb19/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory

I googled it and found that libraries does not support 'caching_sha2_password'. Can anyone suggest me which distro have libraries that support 'caching_sha2_password'? Thanks in advance.

P.S. I don't have access to MySQL server so any change in server side is not in my hand. UPDATED:

Dockerfile:

FROM python:3.7.4-stretch

COPY code/ /code/

WORKDIR /code

RUN apt-get update
RUN apt-get -y upgrade

RUN pip install -r requirements.txt

EXPOSE 8000

RUN python manage.py migrate

CMD python manage.py runserver

Error:

Step 8/9 : RUN python manage.py migrate
 ---> Running in a907f2d6dce6

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 233, in get_new_connection
    return Database.connect(**conn_params)
  File "/usr/local/lib/python3.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/MySQLdb/connections.py", line 179, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib/x86_64-linux-gnu/mariadb18/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory")

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

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 366, in execute
    self.check()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 395, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 63, in _run_checks
    issues = run_checks(tags=[Tags.database])
  File "/usr/local/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python3.7/site-packages/django/core/checks/database.py", line 10, in check_database_backends
    issues.extend(conn.validation.check(**kwargs))
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/validation.py", line 9, in check
    issues.extend(self._check_sql_mode(**kwargs))
  File "/usr/local/lib/python3.7/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.7/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 260, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 236, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 233, in get_new_connection
    return Database.connect(**conn_params)
  File "/usr/local/lib/python3.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/MySQLdb/connections.py", line 179, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib/x86_64-linux-gnu/mariadb18/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory")
The command '/bin/sh -c python manage.py migrate' returned a non-zero code: 1

Requirement.txt:

django==3.0.4
django-environ==0.4.5
bcrypt==3.1.7
mysqlclient==1.4.6
psycopg2==2.8.4
PyMySQL==0.9.3
Vaibhav Gupta
  • 43
  • 1
  • 6
  • 1
    Can we see your Dockerfile used to build your django image? – Dean Christian Armada Mar 27 '20 at 08:16
  • get the mysql-8.0 client side libraries relevant to your container distro into your container and that should provide the shared file that coudn't be opened. You appear to be running a mariadb client side libraries but I can't tell what version. Its [meant to have](https://jira.mariadb.org/browse/CONC-312) support for caching_sha2_password however it depends what version. – danblack Mar 27 '20 at 08:22
  • @DeanChristianArmada I have updated my question with Dockerfile. – Vaibhav Gupta Mar 27 '20 at 08:49
  • @danblack mariadb client libraries were preinstalled in python:3.7.4-stretch docker image. I have done apt-get upgrade. Can you suggest how I can upgrade the libraries? – Vaibhav Gupta Mar 27 '20 at 08:57
  • after checking the mariadb packages I discovered that none of them package the caching_sha2_password ([gave them a quick note](https://github.com/MariaDB/server/pull/1478)). While you might be able to extract it from the 10.4+ tarballs, changing the user back to use [mysql_native_password](https://stackoverflow.com/questions/52864853/how-to-get-mysql-8-to-run-with-laravel/52865317#52865317) plugin is probably the easiest way. – danblack Mar 27 '20 at 10:30
  • 1
    @danblack thanks for the information I will contact the DB team to change the auth plugin. – Vaibhav Gupta Mar 30 '20 at 08:41
  • @bjornsing Sure. – Vaibhav Gupta Mar 30 '20 at 08:42
  • changing to the native auth plugin is definitely the way I'd go after discovering these issues. Will hopefully be fixed before the next python:3.7.X-stretch docker image. – danblack Mar 30 '20 at 09:26
  • [debian bug report #962597](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=962597). Should reported there earlier. – danblack Jul 29 '20 at 04:41

2 Answers2

0

Oh do not include your python manage.py migrate in your build. First of all you do not have a database setup there yet.. Migrate should be done upon container start.. You can use ENTRYPOINT.. I will give you an example of a good django Dockerfile

Dockerfile

FROM python:3.8.0
WORKDIR /usr/src/app
EXPOSE 8000


# Install vim, enable compilemessages and m2crypto
RUN apt-get update &&  \
    apt-get install vim -y

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

ENTRYPOINT ["/usr/src/app/entrypoint.sh"]

entrypoint.sh

#!/bin/bash
# run makemigrate until database is already available, useful on first time setup
# as makemigrate comes first before creating the default 'db, password and user'
until python manage.py makemigrations && python manage.py migrate
do
  echo "Try again"
done &
python manage.py runserver

For the Dockerfile above

  • The reason why I have a separate COPY for requirements.txt and all my files (.)
  • Notice that I use COPY . . this is because my Dockerfile is inside my django where my manage.py also resides
Dean Christian Armada
  • 6,724
  • 9
  • 67
  • 116
  • My solution first solves the build error and a technique for continuous containeriazation for django. You might want to include how you authenticate.. We need more information on the authentication part – Dean Christian Armada Mar 29 '20 at 01:58
0

I got it working!

As @danblack suggested in the comments, I asked DB team to change the auth plugin from caching_sha2_password to mysql_native_password. As of March 2020, we do not have libraries that support caching_sha2_password auth plugin.

Also thanks to Dean Christian Armada for correcting my Dockerfile.

Vaibhav Gupta
  • 43
  • 1
  • 6