mysql_user not created in user table when using docker-compose

docker-compose not creating database
docker-compose mysql create database
docker-compose sql server create database
docker mysql
mariadb docker-compose
docker compose database create
docker-compose mysql import database
docker mysql create database

This is what I see when I am in the container created by docker-compose:

mysql> SELECT user FROM mysql.user;
+------+
| user |
+------+
| root |
+------+
1 row in set (0.00 sec)

mysql> exit
Bye
root@541e4d686184:/# echo $MYSQL_USER
dbuser

dbuser is not present in the users table.

In docker-compose.yml I have this:

version: '2'
services:
  db:
    image: mysql:latest
    environment:
      MYSQL_DATABASE: mydb
      MYSQL_USER: dbuser
      MYSQL_PASSWORD: userpass
      MYSQL_ROOT_PASSWORD: password
    ports:
      - "3306"
    volumes:
      - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
      - my-datavolume:/var/lib/mysql
  web:
#    build: .  # Look in the current directory for a Dockerfile and build images using it
    image: me/tomcat:v1
    depends_on:    # Link web container to db container
      - db
    environment:  # Set up environment variables
      MYSQL_DATABASE: mydb
      MYSQL_ROOT_PASSWORD: password
    ports:
      - "8080:8080"  # Forwards the exposed port 8080 on the container to port 8080 on the host machine
volumes:
  my-datavolume:

I expected dbuser to be created automatically, but that didn't happen.

I also have a sql file to create my database and tables if they don't already exist, but right now tomcat can't connect to my database.

Same symptoms as this question, but I am already using a dictionary for my usernames/passwords.

UPDATE:

I am getting close. If I do:

docker-compose exec db /bin/bash

And then do:

/docker-entrypoint-initdb.d/create_users.sh 

Then when I do this I see:

mysql> SHOW GRANTS for dbuser;
+---------------------------------------------+
| Grants for dbuser@%                         |
+---------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'%' |
+---------------------------------------------+

Then I was able to deploy my application to my tomcat server and I didn't get an error about dbuser being denied access.

So, why did I have to run this command myself, it should be run by docker-compose, according to the mysql docker docs under Initializing a fresh instance.

How about:

docker-compose down -v

From the documentation:

-v - Remove volumes declared in the volumes section of the Compose file.

Your database has been already created inside a volume, so any changes in your docker-compose.yml won't be reflected.

In case you want to remove just a single volume, you may use docker volume ls to list all existing volumes and then docker volume rm <VOLUME NAME> to remove it.

Note: Bind mounts are not removed with the -v flag, so in case you are using them instead of volumes, you'll have to manually delete folders containing MySQL data.

Docker compose not creating db, I have set these vars in the docker-compose.yml. MYSQL_DATABASE: laravel MYSQL_USER: laraveluser MYSQL_PASSWORD: B; I; H1; H2; H3; UL; OL; Link; Code; Highlight; Table My guess would be that the password that you are using is too weak, I would suggest trying to use a stronger� When I spin up a fresh mysql image using docker-compose up -d utilizing the following docker-compose.yml file, the MYSQL_RANDOM_ROOT_PASSWORD env var does not generate a random password services: db: image: mysql:latest environment: MYSQL_RANDOM_ROOT_PASSWORD: 1 MYSQL_DATABASE: database MYSQL_USER: username MYSQL_PASS: password

Worked for me : stop docker and remove manually all the folder containing MySQL data from previous builds.

Also : don't forget to add a MYSQL_DATABASE environment var or it won't create the user you specified.

mysql_user not created in user table when using docker-compose, This is what I see when I am in the container created by docker-compose: docker-compose --version. And you should get something similar to: docker-compose version 1.22.0, build 1719ceb Setting Up. After installing Docker run it and create file calleddocker-compose.yml

Github issue

Important to note that the image entrypoint script will never make changes to an existing database. If you mount an existing data directory into var/lib/mysql, options like MYSQL_ROOT_PASSWORD will have no effect

Official Image for "mysql:5.7" not creating user , Official Image for "mysql:5.7" not creating user correctly � Docker Issue filed with image maintainers. -e MYSQL_USER=testuser \ -e MYSQL_PASSWORD= testpassword --publish 3307:3306 -d mysql:5.7 Here is my “docker-compose. yml” file (sensitive data substituted, but the env vars are working): MySQL is a widely used, open-source relational database management system (RDBMS).

MYSQL_DATABASE does not create database � Issue #68 � docker , I have this in my docker-compose.yml file: librenms_db: image: mariadb 17:58: 43 140123011254208 [Note] InnoDB: Compressed tables use zlib 1.2.8 MYSQL_USER=sswsuitecrm - MYSQL_PASSWORD=suitepwd. and Now create a connection to MySQL with lb datasource: And enter the connection information: The connection string is mysql://root:pwd@localhost:8083/mysql in this case because we are using the container created by docker-compose up -d, which explicitly set port 8083 and the password pwd. Next we’ll create our model with lb model: And add

How to Create a MySql Instance with Docker Compose, With Docker Compose, we get all the benefits of Docker plus more. Docker works by creating a virtual environment(or container) for your code to run. What Docker MYSQL_USER: 'user' # You can use In your terminal, press ctrl + c , this may or may not kill the container gracefully 🤷. Either way, run� The table needs to contain one row with an employee (first name, last name, department, and email). Write a CreateTable.sql . This file contains the SQL statement to create a table called employees.

Deploying MySQL using Docker-Compose – Linux Hint, to set up user account, open ports, set passwords, create databases and tables , etc. Disclaimer: In no way is this compose file “production ready”. Create a file docker-compose.yml in it using your favorite text editor, and write the following: some environment variables to setup MySQL user, database and password. The IF NOT EXISTS option conditionally create a new user only if it does not exist. Note that the CREATE USER statement creates a new user without any privileges. To grant privileges to the user, you use the GRANT statement. MySQL CREATE USER example. First, connect to the MySQL Server using the mysql client tool:

Comments
  • Did you check the output of docker logs? The mysql logs? Did you consider user permissions issues? Did you consider using the mysql client for running your sql script? (e.g. /usr/bin/mysql < /path/to/script.sql)
  • @mlg - I did see that it tried to create this new user, but the root user password worked. I can add the user manually, through the Dockerfile, I just didn't want to do that if possible.
  • sql scripts are good for more than creating users; it might end up being a good thing having a startup script. Do you foresee needing some extra startup actions? It might save you dealing with this altogether.
  • @mig - No, I don't see any other use, but at the moment it works this way. I can save the docker image with the database set up and then just deploy that so everything stays correct. I don't like this option, but it appears it may be my best one.
  • I'm having the same problem here. It used to work great before, and now, this. I don't know why.
  • Great suggestion. Saved my day! Use docker-compose down -v docker-compose up --build to rebuild image
  • Note: Bind mounts aren't removed with the -v flag, so you'll have to manually delete the mount's contents. This caught me off guard for far longer than I'd like to admit..
  • After hours of struggle, I'm glad I came across your answer! Many thanks.