Error: database is uninitialized and password option is not specified
This tutorial you were following is incomplete. It didn't tell you that you must wait until the db is initialized if you want to use it.
Just after running the database container via run
command, you should check the logs of this container and wait until the DB initialization
process is complete
You can do it with:
$ docker logs -f <container name>
Where container name
in your case is mysql
. When you see that db is initialized and DB is started, just detach (ctrl+c) from the logs and continue on.
Your DB is ready to use now.
important note considering your compose file
This compose file is not going to work because the other services like bitpal
/worker
are not waiting for the DB service to initialize.
Initially download a wait-for-it.sh
script, that'd allow other servies to wait for your database when using compose file to setup your application. The script, made by vishnubob, is available here, then copy it to all the catalogs where your services requiring database are.
In the same catalogs create a docker-entrypoint.sh
files and write them like this:
#!/bin/bash
set -e
sh -c './wait-for-it.sh mysql:3306 -t 30'
exec "$@"
Then, in your compose file add entries in every service that require DB (and where you places wait-for-it.sh
script) that will execute the waiting script:
entrypoint: ["./docker-entrypoint.sh"]
Then, your services will wait for the DB until it's initialized and ready to accept connections.
In the edits I'll add straight forward catalog tree so that you can more clearly see how these files should be placed.
This is one of the only efficient methods because depends_on
is not waiting for the db service to be initialized as it's clearly stated in the official docs.
edit with files location explanation
root
- bitpal
+ *some service files*
+ wait-for-it.sh
+ docker-entrypoint.sh
- some_service_requiring_db
+ *some service files*
+ wait-for-it.sh
+ docker-entrypoint.sh
- docker-compose.yml
And your compose file should be like:
version: '2'
services:
# MySQL service definition
mysql:
# like you have
# some services
# Django web server
bitpal:
# ...
entrypoint: ["./docker-entrypoint.sh"]
# further declarations
Related videos on Youtube
Norseback
Updated on June 04, 2022Comments
-
Norseback almost 2 years
I'm new to docker. I've been following this tutorial: https://medium.com/coderscorner/connecting-to-mysql-through-docker-997aa2c090cc . I've set up the root password but once I tried to access the mysql command, it throws out this database is uninitialized error. Also, when I do
docker-compose up
command to pull the needed modules, it gives out an django.db.utils.InternalError: (1049, "Unknown database 'bitpal'"). The command I placed was:docker run --name=mysql -e MYSQL_USER=root MYSQL_ROOT_PASSWORD=password -d mysql
I reckon I've searched for answers here but I couldn't be sure of what's wrong.
docker-compose.yml
version: '2' services: # Redis mysql: image: mysql:5.7 restart: always hostname: mysql container_name: mysql environment: - MYSQL_USER=root - MYSQL_ROOT_PASSWORD=password - MYSQL_DB=bitpal ports: - "3306:3306" # Redis redis: image: redis:latest restart: always hostname: redis container_name: redis ports: - "6379:6379" # Django web server bitpal: image: python:3.5 restart: always hostname: bitpal container_name: bitpal working_dir: /bitpal command: ./bin/start_dev.sh volumes: - ./bitpal:/bitpal - ./etc/config:/etc/config - ./log:/log ports: - "80:80" links: - mysql - redis depends_on: - mysql environment: # Database - DB_NAME=bitpal - DB_USER=root - DB_PASSWORD=password - DB_HOST=mysql - DB_PORT=3306 # Celery worker worker: image: python:3.5 restart: always container_name: worker command: bash -c "./bin/install.sh && ./bin/celery_worker.sh" working_dir: /bitpal volumes: - ./bitpal:/bitpal - ./etc/config:/etc/config - ./log:/log links: - mysql - redis depends_on: - redis # Bitshares websocket listener websocket_listener: image: python:3.5 restart: always container_name: websocket_listener command: bash -c "./bin/install.sh && ./bin/websocket_listener.sh" working_dir: /bitpal volumes: - ./bitpal:/bitpal - ./etc/config:/etc/config - ./log:/log links: - mysql - redis depends_on: - redis # Nginx nginx: image: nginx:1.12.1 container_name: nginx ports: - "8000:80" volumes: - ./bitpal:/home/bitpal/bitpal/bitpal - ./nginx:/etc/nginx/conf.d depends_on: - bitpal
My directory looks like this.
`**ROOT** `root: .gitignore, docker-compose.yml, docker-compose-production.yml... /bitpal /etc /log /nginx /public_html` **ROOT/bitpal** `.gitignore, Dockerfile, Makefile, manage.py... /bin /bitpal /media /static /tests`
All the project's
.sh
files are stored under root/bitpal/bin. Do I placewait-for-it.sh
there instead or place it in bitpal and nginx folders? -
Norseback almost 6 yearsMy directory looks like this. ROOT
root: .gitignore, docker-compose.yml, docker-compose-production.yml... /bitpal /etc /log /nginx /public_html
ROOT/bitpal.gitignore, Dockerfile, Makefile, manage.py... /bin /bitpal /media /static /tests
All the project's .sh files are stored under root/bitpal/bin. Do I placewait-for-it.sh
there instead or place it in bitpal and nginx folders? -
Norseback almost 6 yearsUpdate: the DB seems initialized, but now I still couldn't connect to it? It gives me an error of
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on 'mysql' ([Errno -2] Name or service not known)")'.
When runningdocker exec -ti bitpal mysql
without params,caused exec file not found in $PATH": unknown
error