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

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;

Related

fiware quantumleap insert into cratedb not working (schema missing)

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

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

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.