fiware quantumleap insert into cratedb not working (schema missing) - fiware

goal
Use qunatumleap to move data into a crate_db to display later using Grafana.
what I did
follow tutorial to setup Docker images
setup opc-agent to provide data to the orion broker
setup quantumleap to move data from broker to crate_db on change
checked that a subscription is present in the contextBroker
Expected behavior
on subscription of a new item quantumleap will create a entry in a table in the crate_db to store the provided values
what actually happens
Instead of creating a entry in the Crate_db quantumleap throws a "schema not existing" fault.
The provided tutorials do not talk about setting those schema up myself, therefore I assume that quantumleap normally sets them up.
Right now I do not know why this is failing, most likely it is a configuration mistake on my side
additional information
subscription present in contextBroker:
curl -X GET \
'http://localhost:1026/v2/subscriptions/' \
-H 'fiware-service: openiot' \
-H 'fiware-servicepath: /'
[
{"id":"60360eae34f0ca493f0fc148",
"description":"plc_id",
"status":"active",
"subject":{"entities":[{"idPattern":"PLC1"}],
"condition":{"attrs":["main"]}},
"notification":{"timesSent":1748,
"lastNotification":"2021-02-24T08:59:45.000Z",
"attrs":["main"],
"onlyChangedAttrs":false,
"attrsFormat":"normalized",
"http":{"url":"http://quantumleap:8668/v2/notify"},
"metadata":["dateCreated","dateModified"],
"lastSuccess":"2021-02-24T08:59:45.000Z",
"lastSuccessCode":500},
"throttling":1}
]
Orion log:
orion_1 | INFO#09:07:55 logTracing.cpp[130]: Request received: POST /v1/updateContext, request payload (327 bytes): {"contextElements":[{"type":"plc","isPattern":"false","id":"PLC1","attributes":[{"name":"main","type":"Number","value":"12285","metadatas":[{"name":"SourceTimestamp","type":"ISO8601","value":"2021-02-24T09:07:55.033Z"},{"name":"ServerTimestamp","type":"ISO8601","value":"2021-02-24T09:07:55.033Z"}]}]}],"updateAction":"UPDATE"}, response code: 200
Quantum Leap log:
quantumleap_1 | time=2021-02-24 09:07:55.125 | level=ERROR | corr=c7df320c-767f-11eb-bbb3-0242ac1b0005; cbnotif=1 | from=172.27.0.5 | srv=openiot | subserv=/ | op=_insert_entity_rows | comp=translators.crate | msg=Failed to insert entities because of below error; translator will still try saving original JSON in "mtopeniot"."etplc".__original_ngsi_entity__ | payload=[{'id': 'PLC1', 'type': 'plc', 'main': {'type': 'Number', 'value': '12285', 'metadata': {'dateCreated': {'type': 'DateTime', 'value': '2021-02-24T08:28:59.917Z'}, 'dateModified': {'type': 'DateTime', 'value': '2021-02-24T09:07:55.115Z'}}}, 'time_index': '2021-02-24T09:07:55.115000+00:00'}] | thread=140262103055136 | process=67
Traceback from Qunatumleap
quantumleap_1 | Traceback (most recent call last): quantumleap_1 | File "/src/ngsi-timeseries-api/src/translators/sql_translator.py", line 365, in _insert_entity_rows
quantumleap_1 | self.cursor.executemany(stmt, rows) quantumleap_1 | File "/usr/local/lib/python3.8/site-packages/crate/client/cursor.py", line 67, in executemany quantumleap_1 | self.execute(sql, bulk_parameters=seq_of_parameters)
quantumleap_1 | File "/usr/local/lib/python3.8/site-packages/crate/client/cursor.py", line 53, in execute quantumleap_1 | self._result = self.connection.client.sql(sql, parameters,
quantumleap_1 | File "/usr/local/lib/python3.8/site-packages/crate/client/http.py", line 331, in sql quantumleap_1 | content = self._json_request('POST', self.path, data=data)
quantumleap_1 | File "/usr/local/lib/python3.8/site-packages/crate/client/http.py", line 458, in _json_request quantumleap_1 | _raise_for_status(response)
quantumleap_1 | File "/usr/local/lib/python3.8/site-packages/crate/client/http.py", line 187, in _raise_for_status
quantumleap_1 | raise ProgrammingError(error.get('message', ''),
quantumleap_1 | crate.client.exceptions.ProgrammingError: SQLActionException[SchemaUnknownException: Schema 'mtopeniot' unknown] quantumleap_1 | quantumleap_1 | During handling of the above exception, another exception occurred: quantumleap_1 |
quantumleap_1 | Traceback (most recent call last):
quantumleap_1 | File "/src/ngsi-timeseries-api/src/reporter/reporter.py", line 195, in notify quantumleap_1 | trans.insert(payload, fiware_s, fiware_sp)
quantumleap_1 | File "/src/ngsi-timeseries-api/src/translators/sql_translator.py", line 221, in insert
quantumleap_1 | res = self._insert_entities_of_type(et,
quantumleap_1 | File "/src/ngsi-timeseries-api/src/translators/sql_translator.py", line 354, in _insert_entities_of_type
quantumleap_1 | self._insert_entity_rows(table_name, col_names, entries, entities) quantumleap_1 | File "/src/ngsi-timeseries-api/src/translators/sql_translator.py", line 381, in _insert_entity_rows
quantumleap_1 | self._insert_original_entities_in_failed_batch(
quantumleap_1 | File "/src/ngsi-timeseries-api/src/translators/sql_translator.py", line 437, in _insert_original_entities_in_failed_batch
quantumleap_1 | self.cursor.executemany(stmt, rows)
quantumleap_1 | File "/usr/local/lib/python3.8/site-packages/crate/client/cursor.py", line 67, in executemany
quantumleap_1 | self.execute(sql, bulk_parameters=seq_of_parameters)
quantumleap_1 | File "/usr/local/lib/python3.8/site-packages/crate/client/cursor.py", line 53, in execute
quantumleap_1 | self._result = self.connection.client.sql(sql, parameters,
quantumleap_1 | File "/usr/local/lib/python3.8/site-packages/crate/client/http.py", line 331, in sql
quantumleap_1 | content = self._json_request('POST', self.path, data=data)
quantumleap_1 | File "/usr/local/lib/python3.8/site-packages/crate/client/http.py", line 458, in _json_request
quantumleap_1 | _raise_for_status(response) quantumleap_1 | File "/usr/local/lib/python3.8/site-packages/crate/client/http.py", line 187, in _raise_for_status
quantumleap_1 | raise ProgrammingError(error.get('message', ''),
quantumleap_1 | crate.client.exceptions.ProgrammingError: SQLActionException[SchemaUnknownException: Schema 'mtopeniot' unknown]
Tables in cratedb after running qunatumleap for a while:
screenshot of cratedb tables
docker-compose file
version: "3"
services:
iotage:
hostname: iotage
image: iotagent4fiware/iotagent-opcua
networks:
- hostnet
- iotnet
ports:
- "4001:4001"
- "4081:8080"
extra_hosts:
- "iotcarsrv:192.168.2.16"
# - "PLC1:192.168.2.57"
depends_on:
- iotmongo
- orion
volumes:
- ./certificates:/opt/iotagent-opcua/certificates
- ./AGECONF:/opt/iotagent-opcua/conf
command: /usr/bin/tail -f /var/log/lastlog
iotmongo:
hostname: iotmongo
image: mongo:3.4
volumes:
- iotmongo_data:/data/db
- iotmongo_conf:/data/configdb
crate-db:
image: crate
hostname: crate-db
ports:
- "4200:4200"
- "4300:4300"
command:
crate -Clicense.enterprise=false -Cauth.host_based.enabled=false -Ccluster.name=democluster
-Chttp.cors.enabled=true -Chttp.cors.allow-origin="*"
networks:
- hostnet
quantumleap:
hostname: quantumleap
image: smartsdk/quantumleap
ports:
- "8668:8668"
depends_on:
- crate-db
environment:
- CRATE_HOST=crate-db
networks:
- hostnet
grafana:
image: grafana/grafana
depends_on:
- crate-db
ports:
- "3003:3000"
networks:
- hostnet
################ OCB ################
orion:
hostname: orion
image: fiware/orion:latest
networks:
- hostnet
- ocbnet
ports:
- "1026:1026"
depends_on:
- orion_mongo
#command: -dbhost mongo
entrypoint: /usr/bin/contextBroker -fg -multiservice -ngsiv1Autocast -statCounters -dbhost mongo -logForHumans -logLevel DEBUG -t 255
orion_mongo:
hostname: orion_mongo
image: mongo:3.4
networks:
ocbnet:
aliases:
- mongo
volumes:
- orion_mongo_data:/data/db
- orion_mongo_conf:/data/configdb
command: --nojournal
volumes:
iotmongo_data:
iotmongo_conf:
orion_mongo_data:
orion_mongo_conf:
networks:
hostnet:
iotnet:
ocbnet:
edits
added docker compose file

after changing the database to a more recent version (for example crate-db:3.1.2) the data arrives at the database nicely

Related

Failed to connect python and MySQL using Docker Compose

I am trying to connect to MySQL database using Docker Compose and I get the following error
Traceback (most recent call last):
| File "/usr/local/lib/python3.9/site-packages/mysql/connector/connection_cext.py", line 268, in _open_connection
| self._cmysql.connect(**cnx_kwargs)
| _mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on '172.22.0.2:3306' (110)
|
| The above exception was the direct cause of the following exception:
| Traceback (most recent call last):
| File "//script.py", line 19, in <module>
| cnx = mysql.connector.connect(host='172.22.0.2', user='user', password='pass', database='db', port=3306)
| File "/usr/local/lib/python3.9/site-packages/mysql/connector/pooling.py", line 286, in connect
| return CMySQLConnection(*args, **kwargs)
| File "/usr/local/lib/python3.9/site-packages/mysql/connector/connection_cext.py", line 101, in __init__
| self.connect(**kwargs)
| File "/usr/local/lib/python3.9/site-packages/mysql/connector/abstracts.py", line 1108, in connect
| self._open_connection()
| File "/usr/local/lib/python3.9/site-packages/mysql/connector/connection_cext.py", line 273, in _open_connection..
| raise get_mysql_exception(mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '172.22.0.2:3306'
In my programm I have to containers: for python script and for MySQL database. Both containers are built successfully and db container starts just fine.
172.22.0.2 stands for ip address in network I've created (see docker-compose).
My python script simply connects to the database using mysql-connector.
The code is the following
Dockerfile for script
FROM python:3.9
COPY script.py script.py
RUN pip install mysql-connector-python
script.py
import mysql.connector
cnx = mysql.connector.connect(host='172.22.0.2', user='user', password='pass', database='db', port=3306)
docker-compose.yaml
version: "3"
services:
db:
image: mysql
container_name: db
command: '--init-file /data/app/init.sql'
ports:
- "3306:3306"
volumes:
- "./init.sql:/data/app/init.sql"
environment:
MYSQL_DATABASE: "db"
MYSQL_USER: "user"
MYSQL_PASSWORD: "pass"
MYSQL_ROOT_PASSWORD: "pass"
networks:
net:
ipv4_address: 172.22.0.2
script:
build:
context: ./
dockerfile: Dockerfile
depends_on:
- db
links:
- db
container_name: script
command: sh -c "sleep 10s ; python script.py"
networks:
net:
ipv4_address: 172.22.0.3
networks:
net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.22.0.0/24
gateway: 172.22.0.1
init.sql
CREATE DATABASE IF NOT EXIST db;
USE db;

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;

Hypercorn Name does not resolve in Docker

I am trying to test my Quart application (pager) that connects to a MySQL instance in a docker container called master-db, but after a few retries I get a hypercorn error:
pager | Traceback (most recent call last):
pager | File "/usr/local/bin/hypercorn", line 8, in <module>
pager | sys.exit(main())
pager | File "/usr/local/lib/python3.9/site-packages/hypercorn/__main__.py", line 267, in main
pager | run(config)
pager | File "/usr/local/lib/python3.9/site-packages/hypercorn/run.py", line 34, in run
pager | worker_func(config)
pager | File "/usr/local/lib/python3.9/site-packages/hypercorn/asyncio/run.py", line 187, in asyncio_worker
pager | _run(
pager | File "/usr/local/lib/python3.9/site-packages/hypercorn/asyncio/run.py", line 229, in _run
pager | loop.run_until_complete(main(shutdown_trigger=shutdown_trigger))
pager | File "/usr/local/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
pager | return future.result()
pager | File "/usr/local/lib/python3.9/site-packages/hypercorn/asyncio/run.py", line 69, in worker_serve
pager | sockets = config.create_sockets()
pager | File "/usr/local/lib/python3.9/site-packages/hypercorn/config.py", line 177, in create_sockets
pager | insecure_sockets = self._create_sockets(self.bind)
pager | File "/usr/local/lib/python3.9/site-packages/hypercorn/config.py", line 240, in _create_sockets
pager | sock.bind(binding)
pager | socket.gaierror: [Errno -2] Name does not resolve
pager exited with code 1
The code works locally and has been fully tested, but I don't know where I have gone wrong :(
The docker-compose file is:
version: "3.8"
networks:
localdev:
driver: bridge
services:
master-db:
image: mysql:8.0
container_name: master-db
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "4000:3306"
environment:
MYSQL_ROOT_PASSWORD: password_for_stackoverflow
volumes:
- ./database/docker:/etc/mysql/conf.d
networks:
- localdev
pager:
build:
context: .
dockerfile: Dockerfile.pager
container_name: pager
ports:
- "2020:2020"
networks:
- localdev
depends_on:
- master-db
Docker.pager:
FROM python:3-alpine
RUN pip install --upgrade pip
RUN pip install hypercorn
RUN pip install mysql-connector
RUN pip install quart
COPY src/common /app/common
COPY src/pager /app/pager
WORKDIR /app
CMD ["hypercorn", "pager:app", "--bind", "'0.0.0.0:2020'"]
The problem is not that you can’t connect to the MySQL instance, the problem is that Hypercorn is trying to listen on a socket and failing.
CMD ["hypercorn", "pager:app", "--bind", "'0.0.0.0:2020'"]
Have you tried removing the single quotes from the bind parameter?
CMD ["hypercorn", "pager:app", "--bind", "0.0.0.0:2020"]

Failing to connect to mysql server when I change port number

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:.

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.