前言
都说不建议在docker中运行mysql,说性能不够、不便参数优化。但是对于喜欢折腾的人,这些都不是问题。 docker中跑mysql,将数据和my.cnf等文件映射持久化出来,docker仅仅作为一个mysql服务的管理使用。 就像使用systemd 或者 supervisord 对mysql的进行管理。 初次分享,请大家多动指教,欢迎一起交流切磋。
兄弟们,直接上干货。
一、docker安装
不限制docker版本
# 安装docker wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
# 安装
yum -y install docker-ce
# docker守护进程重新加载
systemctl daemon-reload
# 设置开机自启,且启动
docker systemctl enable docker && systemctl start docker
- daemon.json文件配置
cat > /etc/docker/daemon.json << EOF{
"exec-opts": ["native.cgroupdriver=systemd"],
"insecure-registries" : ["harbor.hwwt2.com","harbor.tp.hwwt2.com","172.25.216.9","swr.cn-east-2.myhuaweicloud.com"],
"bip": "192.168.0.1/24",
"log-driver": "json-file",
"log-opts": {
"max-size": "200m",
"max-file": "5"
}
}
EOF
- docker启动
# 启动
systemctl restart docker
docker info
docker --version
- download mysql镜像
# download mysql5.7镜像
docker search mysql57
#
docker pull mysql57
- 创建mysql的data、log等路径
二、mysql相关文件
# ###
# mysql的my.cnf 配置文件
[mysqld]
#
# Settings configured by the user
#
# Sets how the table names are stored and compared. Default: 0
lower_case_table_names = 0
# Sets whether queries should be logged
general_log = 0
# general_log_file = /var/lib/mysql/data/mysql-query.log
general_log_file = /var/log/mysql/mysql.log
slow_query_log = ON
slow_query_log_file = /var/log/mysql/mysql-query.log
long_query_time = 1
log_output = FILE
# The maximum permitted number of simultaneous client connections. Default: 151
max_connections = 1500
#sql_mode='NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT,ANSI_QUOTES'
# In case the native AIO is broken. Default: 1
# See http://help.directadmin.com/item.php?id=529
innodb_use_native_aio = 1
[myisamchk]
# The minimum/maximum lengths of the word to be included in a FULLTEXT index. Default: 4/20
#
# To ensure that myisamchk and the server use the same values for full-text
# parameters, we placed them in both sections.
ft_min_word_len = 4
ft_max_word_len = 20
[mysqldump] #注意账号密码需要写在此标签下
user=root
password=1234567
三、docker中创建mysql容器
# 创建mysql容器,-e 后面的参数在mysql容器中不起作用,初始化的mysql是没有密码。
docker create --name mysqlELK \
-v /data/mysql/conf/my.cnf:/etc/mysql/my.cnf:ro \
-v /data/mysql/data:/var/lib/mysql \
-v /data/mysql/log:/var/log/mysql \
-p 3307:3306 \
-e MYSQL_ROOT_PASSWORD=1234567 \
d410f4167eea
解释: 1、映射配置文件 2、映射mysql的data和log的路径 3、转发端口 4、配置mysql密码 5、d410f4167eea 为mysql57镜像的ID
四、相关命令
docker ps
#
docker imgs
#
docker ps -a
# 查看容器的日志
docker logs
五、补充
有几个比较容易踩坑的地方:
- 建议使用docker-ce 24以上
- 记得限制容器的日志大小
- docker配置开机自启
- mysql的路径权限要注意
- docker镜像加速需要注意