0


docker安装Mysql?保姆级教学,几个步骤轻松实现!

docker安装Mysql

1.上docker仓库找到想要的镜像版本

docker镜像仓库

2.安装Mysql

找到所要安装的镜像版本,复制命令

输入命令,下载Mysql镜像

命令作用docker pull mysql:latest拉取最新的mysql镜像docker pull mysql:XXX拉取版本号为XXX的mysql镜像

镜像拉取成功后,输入 docker images 查看镜像是否存在

上述步骤都执行没问题后,创建mysql容器并运行,复制如下命令:

docker run \
--name mysql \
-d \
-p 3306:3306 \
--restart unless-stopped \
-v /home/mysql/log:/var/log/mysql \
-v /home/mysql/data:/var/lib/mysql \
-v /home/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-e TZ=Asia/Shanghai \
-e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.38

各个命令解释:
命令作用docker run创建并启动一个容器--name mysql将容器取名为mysql--network my-net接入my-net网络(适情况添加)-d设置后台运行-p 3306:3306映射端口(宿主机端口:容器端口)--restart unless-stopped容器重启策略-v /home/mysql/log:/var/log/mysql将日志文件夹挂载到宿主机(宿主机路径:容器路径)-v /home/mysql/data:/var/lib/mysql将mysql储存文件夹挂载到主机(宿主机路径:容器路径)-v /home/mysql/conf/my.cnf:/etc/mysql/my.cnf将配置文件夹挂载到主机(宿主机路径:容器路径)-e TZ=Asia/Shanghai指定时区-e MYSQL_ROOT_PASSWORD=123456设置mysql密码mysql:5.7.38指定以哪个镜像创建容器(镜像名或镜像ID)

为什么说 --network my-net 这个命令是非必须的呢?

执行完上述命令后,命令:docker ps 查看正在运行的容器,如果出现名为mysql的容器,就是说明安装成功了,但是有的会出现 PORTS 为空,如下图。

这时候就需要用到上述命令了,但在添加前要执行以下步骤,感兴趣的可以自行了解一下docker网络配置

** 具体步骤如下:**
1.查看docker的网络配置

docker inspect 容器名或容器id |grep IPAddress  

使用命令查看容器的网络配置,若返回为空,如图。就是docker的网络配置有问题

2.查看docker的虚拟网络

docker network ls 

docker安装是会默认创建三种不同模式的网络,其中一种是bridge模型的虚拟网络。如果没有的 话则创建。

3.创建一个bridge的虚拟网络

docker network create -d bridge my-net

以上步骤都执行成功后,删除之前创建的容器,重新创建,并加入命令--network my-net,PORTS就会有端口映射关系了。

到这为止,docker安装Mysql就成功了,可以Navicat进行连接测试。(编辑不易,给个赞再走~)

附上my.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 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

[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

Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

max_connections = 2000
max_user_connections = 1900
max_connect_errors = 100000
max_allowed_packet = 20M
lower_case_table_names=1
[mysqld]
skip-name-resolve
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

标签: docker 容器 linux

本文转载自: https://blog.csdn.net/aaabbcq/article/details/125297350
版权归原作者 爱睡觉的小张 所有, 如有侵权,请联系我们删除。

“docker安装Mysql?保姆级教学,几个步骤轻松实现!”的评论:

还没有评论