Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using docker-compose in order to create a MySQL schema/database

I am trying to create a mysql database/schema if it doesn't already exist.

Here is what I have tried:

docker-compose.yml

mysql:
  image: mysql:5.6.26
  environment:
   - MYSQL_ROOT_PASSWORD=root
  command: "mysql -uroot -proot < createDB.sql"
  ports:
    - "3306:3306"

createDB.sql

CREATE DATABASE IF NOT EXISTS bignibou;

It does not work. What would be the best way to use docker/docker-compose in order to create a schema if it does not exist?

like image 702
balteo Avatar asked Aug 15 '15 14:08

balteo


People also ask

How do I create a new database in MySQL?

Open the MySQL Workbench as an administrator (Right-click, Run as Admin). Click on File>Create Schema to create the database schema. Enter a name for the schema and click Apply. In the Apply SQL Script to Database window, click Apply to run the SQL command that creates the schema.

What is Docker compose used for?

Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

How do I run a MySQL schema in a docker container?

Here's how I solved it. 1) Dump your MySQL schema to a file. 2) Use the ADD command to add your schema file to the /docker-entrypoint-initdb.d directory in the Docker container. The docker-entrypoint.sh file will run any files in this directory ending with ".sql" against the MySQL database.

How do I change the database in Docker Compose?

If you want to change databases from MySQL to something else, all you have to do is change the docker-compose.yml and restart Docker Compose. Here’s a simple Postgres DB example: As you can see, using Docker Compose provides us with a ton a flexibility and configurations.

How to configure MySQL server in Docker?

Step 1: Stop the MySQL Server for this instance using the code snippet mentioned below: Step 2: Next, download the MySQL 8.0 Server Docker image and make sure that you have the right tag for MySQL 8.0. Step 3: Start a new MySQL 8.0 Docker Container with the old configuration and server data that have been applied on the host.

What is Docker Compose and how does it work?

What Docker Compose adds is orchestration and organization of multiple containers. While this tutorial will only spin up a single container for our MySQL instance, Docker Compose can also be used to run all of your various services at once when your project begins to grow out. Just follow the installation guide for Docker for Mac or Windows here.


2 Answers

I finally found the beginning of a solution.

The MySQL image takes an environment variable i.e. MYSQL_DATABASE that initialize the container with the name of the database on image startup See here for full documentation.

Or read the excerpt below:

MYSQL_DATABASE

This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access (corresponding to GRANT ALL) to this database.

Here is what I came up with:

mysql:
  image: mysql:5.6.26
  environment:
   - MYSQL_ROOT_PASSWORD=root
   - MYSQL_DATABASE=bignibou
  ports:
    - "3306:3306"

I now need a way to specify the default collation but that is another story...

edit: For those interested in specifying a different collation from the default, here are the instructions to use another config file that will override the default one. See below:

Using a custom MySQL configuration file The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.

If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):

$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.

like image 114
balteo Avatar answered Oct 01 '22 16:10

balteo


To not lost your data better use volumes as well:

version: '3'
services:
  db:
    image: mysql:5.7
    volumes:
        - mysql-db:/var/lib/mysql
    environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: my_db_name
    ports:
        - "3307:3306"
volumes:
  mysql-db:
like image 21
Codenator81 Avatar answered Oct 01 '22 16:10

Codenator81