一、拉取镜像
docker pull mysql:5.7
二、创建挂载
mkdir -p /data/mysql5.7/{conf,data,log}
三、启动镜像
1、创建配置文件
vim /data/mysql5.7/conf/my.cnf
[mysqld]
server-id=1
pid-file= /var/run/mysqld/mysqld.pid
socket= /var/run/mysqld/mysqld.sock
datadir= /var/lib/mysql
log-error= /var/log/mysqld-error.log
slow-query-log-file= /var/log/slow-query.log
user = mysql
port = 3306
lower_case_table_names=1
max_allowed_packet=400M
log_bin_trust_function_creators=1
max_connections=1000
# 时区
default-time_zone = '+8:00'
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
innodb_buffer_pool_size=1G
innodb_log_file_size=256M
innodb_flush_log_at_trx_commit=1
innodb_flush_method=O_DIRECT
innodb_autoinc_lock_mode=2
innodb_flush_log_at_trx_commit=0
#binlog
expire_logs_days=7
log-bin=mysql-bin
binlog-format=ROW
#指定默认引擎
disabled_storage_engines=MyISAM,BLACKHOLE,FEDERATED,CSV,ARCHIVE
default_storage_engine=innodb
symbolic-links=0
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
2、创建日志文件
touch /data/mysql5.7/log/mysqld-error.log
touch /data/mysql5.7/log/slow-query.log
# mysql镜像中,mysql用户和组默认是999
chown 999:999 /data/mysql5.7/log/mysqld-error.log
chmod 755 /data/mysql5.7/log/mysqld-error.log
chown 999:999 /data/mysql5.7/log/slow-query.log
chmod 755 /data/mysql5.7/log/slow-query.log
3、启动
docker run -p 3306:3306 --name mysql5.7 \
-v /data/mysql5.7/conf:/etc/mysql/mysql.conf.d \
-v /data/mysql5.7/log:/var/log/ \
-v /data/mysql5.7/data:/var/lib/mysql \
-e TZ="Asia/Shanghai" \
-e MYSQL_ROOT_PASSWORD=CWCcwy12 \
-d mysql:5.7