Skip to content

Instantly share code, notes, and snippets.

@tomoh1r
Last active Aug 29, 2015
Embed
What would you like to do?
雑な MariaDB の Docker コンテナ作成手順

説明

MariaDB のコンテナは以下の手順で組める。

本当は Oracle MySQL で手順を作りたかったが途中の MariaDB に書き換えたバージョン。

コンテナ構築手順

$ sudo mkdir /var/lib/mysql_01_docker
$ sudo chcon -Rt svirt_sandbox_file_t /var/lib/mysql_01_docker
$ sudo groupadd -g 27 mysql
$ sudo useradd -c "MySQL Server" -d /var/lib/mysql -g 27 -M -N -s /sbin/nologin -u 27 mysql
$ ls -lht
total 36K
-rw-r--r--. 1 tomo tomo  130 2015/01/01 14:55:06 README.md
-rw-r--r--. 1 tomo tomo  999 2015/01/01 14:45:22 my.cnf
-rw-r--r--. 1 tomo tomo  488 2015/01/01 14:40:56 Dockerfile
-rw-r--r--. 1 tomo tomo 5.9K 2015/01/01 14:38:33 mysql_pubkey.asc
-rw-r--r--. 1 tomo tomo  245 2015/01/01 14:30:49 me-docker-mysql.service
-rw-r--r--. 1 tomo tomo 9.8K 2015/01/01 11:26:11 mysql-community-release-fc21-5.noarch.rpm
$ cat my.cnf
; vim:ft=dosini:

[client]
socket = /var/lib/mysql/mysql.sock

default-character-set = utf8

[mysqld]
datadir = /var/lib/mysql
;datadir = /var/lib/mysql/tmp
pid-file = /var/lib/mysql/mysql.pid
socket = /var/lib/mysql/mysql.sock
port = 3306

log-error = /dev/stdout

character_set_server = utf8

max_connections = 1024
table_open_cache = 4000
table_definition_cache = 400
key_buffer_size = 16M
read_buffer_size = 256K
read_rnd_buffer_size = 512K
join_buffer_size = 256K
log_bin = mysql-bin
binlog_format = MIXED
max_binlog_size = 128M
expire_logs_days = 15
server_id = 1
slow_query_log
long_query_time = 3
log_queries_not_using_indexes
open_files_limit = 5500

innodb_buffer_pool_size = 2G
innodb_additional_mem_pool_size = 16M
innodb_file_per_table
innodb_autoextend_increment = 64M
innodb_log_files_in_group = 2
innodb_log_file_size = 128M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
;innodb_flush_method = O_DIRECT
innodb_thread_concurrency = 16
innodb_commit_concurrency = 10
$ cat Dockerfile
# vim:ft=dockerfile:

# metadata
FROM fedora
MAINTAINER Me

# install key
#COPY mysql_pubkey.asc /tmp/
#RUN ["rpm", "--import", "/tmp/mysql_pubkey.asc"]

# install mysql
#COPY mysql-community-release-fc21-5.noarch.rpm /tmp/
#RUN ["yum", "install", "-y", "/tmp/mysql-community-release-fc21-5.noarch.rpm"]
#RUN ["yum", "update", "-y"]
#RUN ["yum", "install", "-y", "mysql-community-server"]
RUN ["yum", "install", "-y", "mariadb-server"]
COPY my.cnf /tmp/

# start
ENTRYPOINT ["/usr/bin/mysqld_safe"]
CMD ["--defaults-extra-file=/tmp/my.cnf"]
$ cat me-docker-mysql.service
; vim:ft=dosini:

[Unit]
Description=MySQL container
Author=Me
After=docker.service

[Service]
Restart=always
ExecStart=/usr/bin/docker start -a me-docker-mysql
ExecStop=/usr/bin/docker stop -t 2 me-docker-mysql

[Install]
WantedBy=local.target
$ sudo docker build -t some/mysql .
$ sudo docker create --name=me-docker-mysql -p 3306:3306 -v /var/lib/mysql_01_docker:/var/lib/mysql "some/mysql"

DB 初期化手順

コンテナの中に入り、 mysql_install_db とか mysql_secure_installation とか利用して datadir の初期化を行う。

$ sudo docker run -v /var/lib/mysql_01_docker:/var/lib/mysql -P -it --entrypoint="/bin/bash" some/mysql

systemd の設定手順

$ sudo cp me-docker-mysql.service /etc/systemd/system/.
$ sudo systemctl enable me-docker-mysql.service
$ sudo systemctl start me-docker-mysql

後は mysql -h 127.0.0.1 -P 3306 -u root とか mysql -S /var/lib/mysql_01_docker/mysql.sock -u root とかで接続確認できると思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment