Docker安装MySQL
1 查找并确定安装版本
网址:https://hub.docker.com/_/mysql?tab=tags
确定版本为8.0.29,复制下镜像拉取语句(如下)
docker pull mysql:8.0.29
2 安装指定版本Mysql
2.1 拉取指定版本镜像
docker pull mysql:8.0.29
2.2 查看拉取(下载)的镜像
docker images mysql
2.3 创建本地数据目录和配置
为了便于修改mysql配置信息,同时为了防止mysql容器删除后MySQL数据库数据也随之被删除的风险,需要在宿主机上创建mysql数据存储目录和配置文件目录进行挂载。
mkdir -p /data/mysql/data
cd /data/mysql/data
pwd
mkdir -p /data/mysql/mysql.conf.d
cd /data/mysql/mysql.conf.d/
pwd
增加配置文件mysqld.cnf
vi mysqld.cnf
内容如下
# copyright (c) 2014, 2016, 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, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1r
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
2.4 运行容器
docker run --name mysql8 \
-d -p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=123456 \
--network nacos_mysql \
-v /data/mysql/data:/var/lib/mysql \
-v /data/mysql/mysql.conf.d:/etc/mysql/mysql.conf.d \
mysql:8.0.29
参数说明:
-
–name 自定义的容器名称
-
-p 3306:3306 映射端口, -p 宿主机端口:docker中mysql端口
-
-v /data/mysql/data:/var/lib/mysql 映射本地数据目录,-v 宿主机文件路径(需要映射到的路径):docker中的路径
-
-e MYSQL_ROOT_PASSWORD=123456 设置ROOT密码
2.6 查看容器运行情况
docker ps
此处是查看运行中容器的情况。
还可以使用命令 docker ps -a 查看所有容器运行情况(包括未运行的容器)
2.7 外部连接测试
使用Navicat连接测试