Help us understand the problem. What is going on with this article?

【Docker】 MySQLを柔軟にカスタムできるテンプレート

More than 3 years have passed since last update.

【docker】 MYSQLを柔軟にカスタムできるテンプレート

他テンプレート

背景

  • 2年ほど前からdockerでサービスを動かしてきて、作りたいと思ってた
  • dockerの設定が記事によってまちまち
  • バージョン指定やinitするときにsqlを走らせたい
  • my.cnfなどカスタムしたい
  • dataVolumeを指定したい
  • それぞれググるのが面倒

使いどころ

  • ローカルのお試し開発環境で試すもよし
  • カスタムしたいところだけコメントアウトを外せばいい

試してみる

  • 下にあるDockerfileとかの内容をコピる or https://github.com/vvdocker/solo-mysqlにアクセスする or cloneする git clone https://github.com/vvdocker/solo-mysql
    スクリーンショット 2016-11-09 11.22.08.png

  • Dockerfileで必要なところをコメントインとかする

# Dockerfile

##########################
# base sql version
##########################
FROM mysql:5.7
#FROM mysql:5.6
#FROM mysql:latest

##########################
# ENV
##########################
ENV MYSQL_ROOT_USER root
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE sample_db
ENV MYSQL_USER test
ENV MYSQL_PASSWORD password

##########################
# my.cnf
##########################
#COPY conf.d/my.cnf /etc/mysql/conf.d/

##########################
# start up shell command
##########################
VOLUME ./init.d:/docker-entrypoint-initdb.d

my.cnf
[client]                           
port=3306
socket=/tmp/mysql.sock

[mysqld]                           
port=3306
socket=/tmp/mysql.sock
key_buffer_size=16M
max_allowed_packet=8M

[mysqldump]                        
quick

[mysqld_safe]                      
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
startup.sh
#! /bin/bash
CREATE_USER='mysql'
CREATE_PASSWORD='secret'
GRANT="GRANT ALL ON ${MYSQL_DATABASE}.* TO ${CREATE_USER}@'%' IDENTIFIED BY ${CREATE_PASSWORD}; FLUSH PRIVILEGES"
echo $GRANT | mysql -u ${MYSQL_ROOT_USER} -p${MYSQL_ROOT_PASSWORD}
startup.sql
DROP TABLE IF EXISTS `personal`;
CREATE TABLE `personal` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(10) DEFAULT NULL,
        KEY `id` (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
      LOCK TABLES `personal` WRITE;
      /*!40000 ALTER TABLE `personal` DISABLE KEYS */;
INSERT INTO `personal` (`id`, `name`)
SELECT * FROM (SELECT 1, 'root@local') AS tmp
WHERE NOT EXISTS (
      SELECT `id` FROM `personal` WHERE `id` = 1
    ) LIMIT 1;
  • これらのサンプルをもとに好きに書き換える

  • docker build -t vvdocker-mysql . でイメージを作って

  • docker run --name app-db -d vvdocker-mysql で起動する

  • docker run --name app-db --volumes-from strage_container -d vvdocker-mysqlのようにbusyboxなども対応

その他追加事項

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away