1、Dockerfile
init_database.sql为初始化脚本
FROM mysql:5.7
MAINTAINER hsshy(457030599@qq.com)
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#定义会被容器自动执行的目录
ENV AUTO_RUN_DIR /docker-entrypoint-initdb.d
#定义初始化sql文件
ENV INSTALL_DB_SQL init_database.sql
COPY ./$INSTALL_DB_SQL $AUTO_RUN_DIR/
RUN chmod a+x $AUTO_RUN_DIR/$INSTALL_DB_SQL
my.conf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#忽略大小写
lower_case_table_names =1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
character_set_server=utf8mb4
max_connections = 1024
skip-name-resolve
pid-file=/var/run/mysqld/mysqld.pid
#log-bin=mysql-bin
server-id=1
2、运行命令
docker run -p 3306:3306 --name mysql-5.7 \
-v /home/docker/mysql/conf:/etc/mysql \
-v /home/docker/mysql/log:/var/log/ \
-v /home/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=password \
-d mysql:5.7
3、在/home/docker/mysql/log中创建mysqld.log,给读写权限。
vim /home/docker/mysql/log/mysqld.log
chmod 777 /home/docker/mysql/log/mysqld.log
参考地址
Docker Mysql官方github地址
MySQL的Docker官方映像