tmvnty tmvnty - 3 months ago 31
MySQL Question

Docker MySQL ERROR 1049 (42000): Unknown database 'users'

Hi I'm trying to create a MySQL database in Docker, here's my code

in start.sh

#!/bin/sh

echo "Starting DB..."
docker run --name db -d \
-e MYSQL_ROOT_PASSWORD=123 \
-e MYSQL_DATABASE=users -e MYSQL_USER=root -e MYSQL_PASSWORD=123 \
-p 3306:3306 \
mysql:latest

echo "Waiting for DB to start up..."
docker exec db mysqladmin --silent --wait=30 -uroot ping || exit 1

echo "Setting up initial data..."
docker exec -i db mysql -uroot users < setup.sql


and in setup.sql

create table users (
user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) NOT NULL,
department VARCHAR(100),
name VARCHAR(100),
access_token VARCHAR(30)
);

insert into users (username, department, name, access_token) values ('bzh01', 'Research and Development One', 'Mario1', NULL);


in the terminal, this is the result:

$ bash start.sh
Starting DB...
c7a066b70fa4ad3e02acc3165cfe34ce6a48964cb1190df399404b728cb6059f
Waiting for DB to start up...
mysqld is alive
Setting up initial data...
ERROR 1049 (42000): Unknown database 'users'

Answer

Okay, i've finally got this solved, but in a slightly different way.

in my Dockerfile for the database:

FROM mysql

ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE users
ENV MYSQL_USER root_user
ENV MYSQL_PASSWORD abcd123

ADD setup.sql /docker-entrypoint-initdb.d

and then run docker-compose.yml to build

version: '2'
services:
  app:
    build: ./app
    volumes:
      - "./app:/src/app"
    ports:
     - "3030:3000"
    depends_on:
     - db
    environment:
     - DATABASE_HOST=db
  db:
    build: ./test-database
Comments