Failing to connect to mysql server when I change port number - mysql

I attempted to change MYSQL container to run on a different port number ie 3307 in my docker-compose file but I get a database connection error as shown below after launching services with docker-compose up what could be the issue?
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 194, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
web_1 | return Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.7/site-packages/MySQLdb/__init__.py", line 130, in Connect
web_1 | return Connection(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/MySQLdb/connections.py", line 185, in __init__
web_1 | super().__init__(*args, **kwargs2)
web_1 | MySQLdb._exceptions.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")
web_1 |
web_1 | The above exception was the direct cause of the following exception:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
web_1 | worker.init_process()
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
web_1 | self.load_wsgi()
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
web_1 | self.wsgi = self.app.wsgi()
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
web_1 | self.callable = self.load()
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
web_1 | return self.load_wsgiapp()
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
web_1 | return util.import_app(self.app_uri)
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
web_1 | __import__(module)
web_1 | File "/src/core/wsgi.py", line 16, in <module>
web_1 | application = get_wsgi_application()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
web_1 | django.setup(set_prefix=False)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
web_1 | apps.populate(settings.INSTALLED_APPS)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 120, in populate
web_1 | app_config.ready()
web_1 | File "/usr/local/lib/python3.7/site-packages/django_prometheus/apps.py", line 23, in ready
web_1 | ExportMigrations()
web_1 | File "/usr/local/lib/python3.7/site-packages/django_prometheus/migrations.py", line 49, in ExportMigrations
web_1 | executor = MigrationExecutor(connections[alias])
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/migrations/executor.py", line 18, in __init__
web_1 | self.loader = MigrationLoader(self.connection)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/migrations/loader.py", line 49, in __init__
web_1 | self.build_graph()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/migrations/loader.py", line 212, in build_graph
web_1 | self.applied_migrations = recorder.applied_migrations()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations
web_1 | if self.has_table():
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 44, in has_table
web_1 | return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 255, in cursor
web_1 | return self._cursor()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 232, in _cursor
web_1 | self.ensure_connection()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
web_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 194, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
web_1 | return Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.7/site-packages/MySQLdb/__init__.py", line 130, in Connect
web_1 | return Connection(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/MySQLdb/connections.py", line 185, in __init__
web_1 | super().__init__(*args, **kwargs2)
web_1 | django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")
Docker Compose File
db:
restart: always
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: default_schema
MYSQL_USER: test
MYSQL_PASSWORD: test
expose:
- "3307"
ports:
- "3307:3307"
volumes:
- ../mysql-data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: pma
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3307
PMA_ARBITRARY: 1
restart: always
ports:
- 8183:80
Application DB Settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'default_schema',
'USER': 'root',
'PASSWORD': 'test',
'HOST': 'db',
'PORT': '3307',
}
}
After running docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d60c0265ed9 nllb_frontend "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp frontend
323a7dd77121 nllb_web "gunicorn --bind 0.0…" 7 seconds ago Up 2 seconds 7000/tcp nllb_web_1
89ad90d1fb10 phpmyadmin/phpmyadmin "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 0.0.0.0:8183->80/tcp pma
e5c5e2bda22c mysql:5.7 "docker-entrypoint.s…" 8 seconds ago Up 7 seconds 3306/tcp, 33060/tcp, 0.0.0.0:3307->3307/tcp nllb_db_1
1332ad2e136b redis:latest "docker-entrypoint.s…" 9 seconds ago Up 8 seconds 6379/tcp nllb_redis_1

You need to also tell MySQL about the change of plans:
environment:
MYSQL_TCP_PORT: 3307
(fixed, thanks to #derpirscher, see below)
(I previously guessed it would be PORT, DB_PORT, MYSQL_PORT, MYSQLD_PORT, or, as a testament to my willingness to believe any absurdity I find on Github, MYSQL_HOST=db:3307)

Connections between containers always use the "normal" port number for the service; these connections ignore any remapping that ports: might do. If you are only connecting to the database from another container, in fact, ports: isn't required at all; if it is present, the second port number again needs to be the "normal" port number for the service.
It should work to change the docker-compose.yml file:
version: '3.8'
services:
db:
# do not need `expose:`
ports: # optional, if you're not going to connect from outside Docker
- '3307:3306' # second port _must_ be "normal" port 3306
phpmyadmin:
# do not need `container_name:` or `links:`
environment:
PMA_HOST: db # Compose service name
PMA_PORT: 3306 # always the "normal" port, ignores `ports:`
And similarly in your configuration:
'HOST': 'db',
'PORT': '3306', # the "normal" port, ignores `ports:`
As I noted in comments, expose: and links: are only relevant for an outdated mode of Docker networking, and can be safely removed from the docker-compose.yml file. There's also usually no harm in letting Compose pick its own container_name:.

Related

Docker compose error with hue and mysql - 1130, "Host '172.20.0.3' is not allowed to connect to this MySQL server"

I need help to connect hue with my database. When I run docker-compose up, the hue down/fail.
docker-compose.yml
hue:
image: hue
hostname: hue
container_name: hue
dns: 8.8.8.8
ports:
- "8888:8888"
volumes:
- ./data/hue/hue-overrides.ini:/usr/share/hue/desktop/conf/z-hue.ini
depends_on:
- "database"
deploy:
resources:
limits:
memory: 500m
database:
image: mysql
container_name: database
hostname: database
ports:
- "33061:3306"
deploy:
resources:
limits:
memory: 500m
command: mysqld --innodb-flush-method=O_DSYNC --innodb-use-native-aio=OFF --init-file /data/application/init.sql
volumes:
- ./data/mysql/data:/var/lib/mysql
- ./data/init.sql:/data/application/init.sql
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: hue
MYSQL_USER: root
MYSQL_PASSWORD: secret
Docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
41a3cbb80704 mysql "docker-entrypoint.s…" 19 seconds ago Up 18 seconds 33060/tcp, 0.0.0.0:33061->3306/tcp, :::33061->3306/tcp database
Log:
hue | [02/Oct/2022 13:34:51 -0700] decorators INFO Using django-axes 2.2.0
hue | Traceback (most recent call last):
hue | File "./build/env/bin/hue", line 11, in <module>
hue | load_entry_point('desktop', 'console_scripts', 'hue')()
hue | File "/usr/share/hue/desktop/core/src/desktop/manage_entry.py", line 225, in entry
hue | execute_from_command_line(sys.argv)
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/core/management/__init__.py", line 364, in execute_from_command_line
hue | utility.execute()
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/core/management/__init__.py", line 356, in execute
hue | self.fetch_command(subcommand).run_from_argv(self.argv)
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/core/management/base.py", line 283, in run_from_argv
hue | self.execute(*args, **cmd_options)
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/core/management/base.py", line 327, in execute
hue | self.check()
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/core/management/base.py", line 359, in check
hue | include_deployment_checks=include_deployment_checks,
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/core/management/commands/migrate.py", line 61, in _run_checks
hue | issues = run_checks(tags=[Tags.database])
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/core/checks/registry.py", line 81, in run_checks
hue | new_errors = check(app_configs=app_configs)
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/core/checks/database.py", line 10, in check_database_backends
hue | issues.extend(conn.validation.check(**kwargs))
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/db/backends/mysql/validation.py", line 9, in check
hue | issues.extend(self._check_sql_mode(**kwargs))
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
hue | with self.connection.cursor() as cursor:
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/db/backends/base/base.py", line 254, in cursor
hue | return self._cursor()
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/db/backends/base/base.py", line 229, in _cursor
hue | self.ensure_connection()
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/db/backends/base/base.py", line 213, in ensure_connection
hue | self.connect()
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/db/utils.py", line 94, in __exit__
hue | six.reraise(dj_exc_type, dj_exc_value, traceback)
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/db/backends/base/base.py", line 213, in ensure_connection
hue | self.connect()
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/db/backends/base/base.py", line 189, in connect
hue | self.connection = self.get_new_connection(conn_params)
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/Django-1.11.22-py2.7.egg/django/db/backends/mysql/base.py", line 274, in get_new_connection
hue | conn = Database.connect(**conn_params)
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg/MySQLdb/__init__.py", line 81, in Connect
hue | return Connection(*args, **kwargs)
hue | File "/usr/share/hue/build/env/local/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg/MySQLdb/connections.py", line 193, in __init__
hue | super(Connection, self).__init__(*args, **kwargs2)
hue | django.db.utils.OperationalError: (1130, "Host '172.21.0.3' is not allowed to connect to this MySQL server")
This is my local mysql:
mysql> SELECT host FROM mysql.user WHERE User = 'root';
+-----------+
| host |
+-----------+
| % |
| localhost |
+-----------+
2 rows in set (0,00 sec)
When I run docker exec -it database bash:
$ docker exec -it database bash
root#database:/# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Can someone please help me?
The error message is:
Host '172.21.0.3' is not allowed to connect to this MySQL server
So you have to create a new user and allow access from anywhere(%) and grant the necessary permissions to it:
CREATE USER 'hueuser'#'localhost' IDENTIFIED BY 'mysafepass';
CREATE USER 'hueuser'#'%' IDENTIFIED BY 'mysafepass';
GRANT ALL ON *.* TO 'hueuser'#'localhost';
GRANT ALL ON *.* TO 'hueuser'#'%';
FLUSH PRIVILEGES;

django.db.utils.OperationalError: (1045, 'Plugin caching_sha2_password could not be

I'm trying to containerize my Django app with mysql and I get the following error when I try to run docker-compose up --build.
Successfully tagged drosmokers_web:latest
[+] Running 3/2
⠿ Network drosmokers_default Created 0.1s
⠿ Container drosmokers-db-1 Created 0.3s
⠿ Container drosmokers-web-1 Created 0.1s
Attaching to drosmokers-db-1, drosmokers-web-1
drosmokers-db-1 | 2022-05-19 02:09:40+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
drosmokers-db-1 | 2022-05-19 02:09:40+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
drosmokers-db-1 | 2022-05-19 02:09:40+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
drosmokers-db-1 | 2022-05-19T02:09:40.450429Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.28) starting as process 1
drosmokers-db-1 | 2022-05-19T02:09:40.459416Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
drosmokers-db-1 | 2022-05-19T02:09:40.971548Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
drosmokers-db-1 | 2022-05-19T02:09:41.218218Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
drosmokers-db-1 | 2022-05-19T02:09:41.218279Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
drosmokers-db-1 | 2022-05-19T02:09:41.229850Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
drosmokers-db-1 | 2022-05-19T02:09:41.261835Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.28' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
drosmokers-db-1 | 2022-05-19T02:09:41.262088Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
drosmokers-web-1 | Watching for file changes with StatReloader
drosmokers-web-1 | Exception in thread django-main-thread:
drosmokers-web-1 | Traceback (most recent call last):
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
drosmokers-web-1 | self.connect()
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
drosmokers-web-1 | return func(*args, **kwargs)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 225, in connect
drosmokers-web-1 | self.connection = self.get_new_connection(conn_params)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
drosmokers-web-1 | return func(*args, **kwargs)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 244, in get_new_connection
drosmokers-web-1 | connection = Database.connect(**conn_params)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/MySQLdb/__init__.py", line 123, in Connect
drosmokers-web-1 | return Connection(*args, **kwargs)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/MySQLdb/connections.py", line 185, in __init__
drosmokers-web-1 | super().__init__(*args, **kwargs2)
drosmokers-web-1 | MySQLdb._exceptions.OperationalError: (1045, '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')
drosmokers-web-1 |
drosmokers-web-1 | The above exception was the direct cause of the following exception:
drosmokers-web-1 |
drosmokers-web-1 | Traceback (most recent call last):
drosmokers-web-1 | File "/usr/local/lib/python3.9/threading.py", line 954, in _bootstrap_inner
drosmokers-web-1 | self.run()
drosmokers-web-1 | File "/usr/local/lib/python3.9/threading.py", line 892, in run
drosmokers-web-1 | self._target(*self._args, **self._kwargs)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
drosmokers-web-1 | fn(*args, **kwargs)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
drosmokers-web-1 | self.check_migrations()
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 576, in check_migrations
drosmokers-web-1 | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
drosmokers-web-1 | self.loader = MigrationLoader(self.connection)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 58, in __init__
drosmokers-web-1 | self.build_graph()
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 235, in build_graph
drosmokers-web-1 | self.applied_migrations = recorder.applied_migrations()
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
drosmokers-web-1 | if self.has_table():
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 57, in has_table
drosmokers-web-1 | with self.connection.cursor() as cursor:
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
drosmokers-web-1 | return func(*args, **kwargs)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 284, in cursor
drosmokers-web-1 | return self._cursor()
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 260, in _cursor
drosmokers-web-1 | self.ensure_connection()
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
drosmokers-web-1 | return func(*args, **kwargs)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
drosmokers-web-1 | self.connect()
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
drosmokers-web-1 | raise dj_exc_value.with_traceback(traceback) from exc_value
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
drosmokers-web-1 | self.connect()
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
drosmokers-web-1 | return func(*args, **kwargs)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 225, in connect
drosmokers-web-1 | self.connection = self.get_new_connection(conn_params)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
drosmokers-web-1 | return func(*args, **kwargs)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 244, in get_new_connection
drosmokers-web-1 | connection = Database.connect(**conn_params)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/MySQLdb/__init__.py", line 123, in Connect
drosmokers-web-1 | return Connection(*args, **kwargs)
drosmokers-web-1 | File "/usr/local/lib/python3.9/site-packages/MySQLdb/connections.py", line 185, in __init__
drosmokers-web-1 | super().__init__(*args, **kwargs2)
drosmokers-web-1 | django.db.utils.OperationalError: (1045, 'Plugin caching_sha2_password could not be loaded: /usr/lib/x86_64-linux-gnu/mariadb19/pl
ugin/caching_sha2_password.so: cannot open shared object file: No such file or directory')
Here is my docker-compose.yml:
version: '3.9'
services:
db:
image: mysql
restart: always
environment:
- MYSQL_USER="dexter"
- MYSQL_PASSWORD="password"
- MYSQL_DATABASE="drosmokers_db"
- MYSQL_ROOT_PASSWORD="password"
volumes:
- dev_vol:/var/lib/mysql/
ports:
- 3305:3306
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
volumes:
dev_vol:
My Dockerifle:
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONBUFFERED=1
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY . /app/
...and finally my settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'drosmokers_db',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'drosmokers-db-1', # the container name
'PORT': 3306
}
}
I don't know why I'm getting this error and what this error means. I've been able to successfully containerize this app before, but I really was just following instructions, not really knowing what was going on with each command. I spent the last hour trying to figure out why running docker-compose up --build produces this error : django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on <database> (115), and now I get this error, and I don't even know what I did or where to start looking. Any help is greatly appreciated. Thanks!

How do I connect my docker Django instance to my docker mysql instance?

I'm running Docker on Mac High Sierra. I want to run Docker container to house both my Django instances and MySql instances. I have the following docker-compose.yml file ...
version: '3'
services:
web:
restart: always
build: ./web
ports: # to access the container from outside
- "8000:8000"
env_file: .env
environment:
DEBUG: 'true'
command: /usr/local/bin/gunicorn maps.wsgi:application -w 2 -b :8000
apache:
restart: always
build: ./apache/
ports:
- "80:80"
#volumes:
# - web-static:/www/static
links:
- web:web
mysql:
restart: always
image: mysql:5.7
environment:
MYSQL_DATABASE: 'maps_data'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'chicommons'
# You can use whatever password you like
MYSQL_PASSWORD: 'password'
# Password for root access
MYSQL_ROOT_PASSWORD: 'password'
ports:
- "3406:3406"
volumes:
- my-db:/var/lib/mysql
volumes:
my-db:
The ".env" file I set up looks like this ...
DB_NAME=maps_data
DB_USER=chicommons
DB_PASS=password
DB_SERVICE=mysql
DB_PORT=3406
In my web (Django) instance, I have set my settings.py db thusly ...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ['DB_NAME'],
'USER': os.environ['DB_USER'],
'PASSWORD': os.environ['DB_PASS'],
'HOST': os.environ['DB_SERVICE'],
'PORT': os.environ['DB_PORT']
}
}
However, when I run "docker-compose up," I get the following errors complaining about not being able to connect to the database ...
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 194, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 236, in get_new_connection
web_1 | return Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
web_1 | return Connection(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/MySQLdb/connections.py", line 179, in __init__
web_1 | super(Connection, self).__init__(*args, **kwargs2)
web_1 | MySQLdb._exceptions.OperationalError: (2002, "Can't connect to MySQL server on 'mysql' (115)")
web_1 |
web_1 | The above exception was the direct cause of the following exception:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "manage.py", line 21, in <module>
web_1 | main()
web_1 | File "manage.py", line 17, in main
web_1 | execute_from_command_line(sys.argv)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
web_1 | utility.execute()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 365, in execute
web_1 | self.fetch_command(subcommand).run_from_argv(self.argv)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
web_1 | self.execute(*args, **cmd_options)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 332, in execute
web_1 | self.check()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 364, in check
web_1 | include_deployment_checks=include_deployment_checks,
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 351, in _run_checks
web_1 | return checks.run_checks(**kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/checks/registry.py", line 73, in run_checks
web_1 | new_errors = check(app_configs=app_configs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/checks/model_checks.py", line 27, in check_all_models
web_1 | errors.extend(model.check(**kwargs))
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 1200, in check
web_1 | errors.extend(cls._check_fields(**kwargs))
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 1272, in _check_fields
web_1 | errors.extend(field.check(**kwargs))
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 894, in check
web_1 | errors = super().check(**kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 206, in check
web_1 | errors.extend(self._check_backend_specific_checks(**kwargs))
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 303, in _check_backend_specific_checks
web_1 | return connections[db].validation.check_field(self, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/validation.py", line 21, in check_field
web_1 | field_type = field.db_type(self.connection)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 648, in db_type
web_1 | return connection.data_types[self.get_internal_type()] % data
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
web_1 | res = instance.__dict__[self.name] = self.func(instance)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 133, in data_types
web_1 | if self.features.supports_microsecond_precision:
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
web_1 | res = instance.__dict__[self.name] = self.func(instance)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/features.py", line 65, in supports_microsecond_precision
web_1 | return self.connection.mysql_version >= (5, 6, 4)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
web_1 | res = instance.__dict__[self.name] = self.func(instance)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 345, in mysql_version
web_1 | with self.temporary_connection() as cursor:
web_1 | File "/usr/local/lib/python3.7/contextlib.py", line 112, in __enter__
web_1 | return next(self.gen)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 576, in temporary_connection
web_1 | cursor = self.cursor()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 255, in cursor
web_1 | return self._cursor()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 232, in _cursor
web_1 | self.ensure_connection()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
web_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 194, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 236, in get_new_connection
web_1 | return Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
web_1 | return Connection(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/MySQLdb/connections.py", line 179, in __init__
web_1 | super(Connection, self).__init__(*args, **kwargs2)
web_1 | django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'mysql' (115)")
What's going on? How do I get my Django web docker instance to connect to my MySql docker instance?
Maybe port mistake
version: '3'
services:
web:
...
links: # <-- add this
- mysql # <-- add this
apache:
...
mysql:
...
ports: # <-- add this
- "3406:3306" # <-- change myslq port inside is 3306 not 3406
volumes:
my-db:
To use db's and other services, container to container, see links.
You took a mistake: "3406:3406". The first port is the host port and the second port is the service that is running inside the container, so mysql runs on 3306, therefore you have to change to "3406:3306".
If you have different services running like backend/frontend you should create networks between containers.

django-mysql connection error inside docker

errors:
govtcareer |
govtcareer | Traceback (most recent call last):
govtcareer | File "manage.py", line 15, in <module>
govtcareer | execute_from_command_line(sys.argv)
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
govtcareer | utility.execute()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
govtcareer | self.fetch_command(subcommand).run_from_argv(self.argv)
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
govtcareer | self.execute(*args, **cmd_options)
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 350, in execute
govtcareer | self.check()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
govtcareer | include_deployment_checks=include_deployment_checks,
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 59, in _run_checks
govtcareer | issues = run_checks(tags=[Tags.database])
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
govtcareer | new_errors = check(app_configs=app_configs)
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/checks/database.py", line 10, in check_database_backends
govtcareer | issues.extend(conn.validation.check(**kwargs))
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 9, in check
govtcareer | issues.extend(self._check_sql_mode(**kwargs))
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
govtcareer | with self.connection.cursor() as cursor:
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
govtcareer | return self._cursor()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
govtcareer | self.ensure_connection()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
govtcareer | self.connect()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
govtcareer | raise dj_exc_value.with_traceback(traceback) from exc_value
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
govtcareer | self.connect()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
govtcareer | self.connection = self.get_new_connection(conn_params)
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
govtcareer | return Database.connect(**conn_params)
govtcareer | File "/usr/local/lib/python3.6/site-packages/MySQLdb/__init__.py", line 85, in Connect
govtcareer | return Connection(*args, **kwargs)
govtcareer | File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
govtcareer | super(Connection, self).__init__(*args, **kwargs2)
govtcareer | django.db.utils.OperationalError: (2002, 'Can\'t connect to local MySQL server through socket \'/var/run/mysqld/mysqld.sock\' (2 "No such file or directory")')
Dockerfile:
FROM python:3.6
ENV PYTHONUNBUFFERED 1
WORKDIR /usr/src/govtcareer_api
COPY ./ /usr/src/govtcareer_api
RUN pip install -r requirements.txt
RUN apt-get install nano
CMD ["/bin/bash"]
docker-compose:
version: "3"
services:
govtcareer_api:
container_name: govtcareer
build: .
command: "bash -c 'python manage.py migrate --no-input && python manage.py runserver 0.0.0.0:8000'"
working_dir: /usr/src/govtcareer_api
ports:
- "8000:8000"
volumes:
- ./:/usr/src/govtcareer_api
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=Thinkonce
- MYSQL_ROOT_USER=soubhagya
- MYSQL_USER=soubhagya
- MYSQL_PASSWORD=Thinkonce
- MYSQL_DATABASE=freejobalert
ports:
- "3306:3306"
Django-database connections in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'freejobalert',
'USER': 'soubhagya',
'PASSWORD': 'Thinkonce',
'HOST': 'localhost',
'PORT': '3306',
},
}
I am trying to connect django with mysql inside docker using docker-compose.
i am able to access mysql using "mysql -u soubhagya -p" inside docker container
but with django connection it is showing following errors.
Please have a look into my code.

django-mysql connection inside docker-compose giving connection refused

Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
WORKDIR /usr/src/govtcareer_api
COPY ./ /usr/src/govtcareer_api
RUN pip install -r requirements.txt
CMD ["/bin/bash"]
docker-compose.yml
version: "3"
services:
govtcareer_api:
container_name: govtcareer
build: .
command: "bash -c 'python manage.py migrate --no-input && python manage.py runserver 0.0.0.0:8000'"
working_dir: /usr/src/govtcareer_api
ports:
- "8000:8000"
volumes:
- ./:/usr/src/govtcareer_api
links:
- db
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=Thinkonce
- MYSQL_USER=soubhagya
- MYSQL_PASSWORD=Thinkonce
- MYSQL_DATABASE=freejobalert
ports:
- "3306:3306"
django database connection:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'freejobalert',
'USER': 'soubhagya',
'PASSWORD': 'Thinkonce',
'HOST': 'localhost',
'PORT': '3306',
},
}
errors:
govtcareer | include_deployment_checks=include_deployment_checks,
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 59, in _run_checks
govtcareer | issues = run_checks(tags=[Tags.database])
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
govtcareer | new_errors = check(app_configs=app_configs)
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/checks/database.py", line 10, in check_database_backends
govtcareer | issues.extend(conn.validation.check(**kwargs))
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 9, in check
govtcareer | issues.extend(self._check_sql_mode(**kwargs))
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
govtcareer | with self.connection.cursor() as cursor:
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
govtcareer | return self._cursor()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
govtcareer | self.ensure_connection()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
govtcareer | self.connect()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
govtcareer | raise dj_exc_value.with_traceback(traceback) from exc_value
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
govtcareer | self.connect()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
govtcareer | self.connection = self.get_new_connection(conn_params)
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
govtcareer | return Database.connect(**conn_params)
govtcareer | File "/usr/local/lib/python3.6/site-packages/MySQLdb/__init__.py", line 85, in Connect
govtcareer | return Connection(*args, **kwargs)
govtcareer | File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
govtcareer | super(Connection, self).__init__(*args, **kwargs2)
govtcareer | django.db.utils.OperationalError: (2002, 'Can\'t connect to local MySQL server through socket \'/var/run/mysqld/mysqld.sock\' (2 "No such file or directory")')
Here is my code codes.
i am trying to integrate django with mysql inside docker container
but is is showing connection refused to host.
do i need to use environment variable to use it inside docker?
the same way i used for mongodb but in mysql i am facing this issue.
please have a look into my code.
Remove links from your docker-compose.yml.
links:
- db
You don't need them - docker will build a network and include all your declared services there.