第一步
docker pull mysql
第二步
在宿主机创建放置mysql的配置文件的目录,和数据目录,然后创建my.cnf配置文件
mkdir /usr/local/mysql_conf
mkdir /usr/local/mysql_data
vi /usr/local/mysql_conf/my.cnf
附上mysql8.0默认的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[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
!includedir /etc/mysql/conf.d/
第三步
执行创建容器命令
docker run -d --name mysql -v /usr/local/mysql_conf/my.cnf:/etc/mysql/my.cnf -v /usr/local/mysql_data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
-v 挂载宿主机目录和 docker容器中的目录
-d 后台运行
-p 映射容器端口号和宿主机端口号
-e 环境参数
----------------------------------分割线-----------------------------------------------------------------------
这里说个坑
之前博主用不挂载宿主机目录的方式来部署mysql 然后通过
docker exec -it mysql bash
进入容器内部的
/etc/mysql
目录来修改my.cnf
注:docker 容器内部默认没有文本编辑器(后面有介绍为容器安装vim)
通过 这种方式来配置mysql的配置有一个致命的缺点 ,my.cnf改错了会导致容器无法启动然后无法通过命令行的方式来修该mysql配置文件来恢复容器,相当扯淡!!!
另外容器中默认 是没有安装vi 或者vim的编辑器的
先用 docker exec -it xxxx bash 进入容器命令行 然后
apt update //更新容器软件列表
apt install vim //安装vim编辑器 注:软件源中并没有vi编辑器 却有更强大的vim编辑器 用法相同
这样就可以在容器中用编辑器修改文本文件了相当好用!!