一、安装
1、查看可用的 MySQL 版本
访问 MySQL 镜像库地址:https://hub.docker.com/_/mysql?tab=tags 。
2、拉取 MySQL 镜像
这里我们拉取官方的最新版本的镜像:
$ docker pull mysql:8.0.26
3、查看本地镜像
使用以下命令来查看是否已安装了 mysql:
$ docker images
4、创建挂载目录
mkdir -p /usr/local/mysql/conf
mkdir -p /usr/local/mysql/data
mkdir -p /usr/local/mysql/logs
5、创建my.cnf
vim /usr/local/mysql/conf/my.cnf
把下面配置复制到文件中:
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[client]
default-character-set = utf8mb4
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Custom config should go here
# 字符集
character_set_server=utf8
collation-server=utf8_general_ci
# 是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names = 1
# 最大连接数
max_connections = 1000
# Innodb缓存池大小
innodb_buffer_pool_size = 4G
# 表文件描述符的缓存大小
table_open_cache_instances=1
table_open_cache=2000
table_definition_cache=2000
!includedir /etc/mysql/conf.d/
6、创建启动容器
docker run --restart=always --name mysql8.0 --privileged=true -d -p 3306:3306 -v /usr/local/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /usr/local/mysql/logs:/logs -v /usr/local/mysql/data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.26
参数解释:
--restart=always -> 开机启动容器,容器异常自动重启
--name -> 指定容器名称
-d -> 以守护进程的方式启动容器
-p -> 映射宿主机端口
-v -> 映射配置文件、日志、数据
-e -> 写入配置root密码
7、查看是否启动正常
$ docker ps
使用Navicat连接: