docker相关命令:
docker ps:查看启动成功的容器
docker ps -a:查看所有启动的容器(包括启动异常的容器)
docker rm 容器id:删除容器
docker stop 停止容器
docker-compose:通过加载配置文件的方式启动容器
1、拉取mysql镜像,采用网易加速地址
docker pull hub.c.163.com/library/mysql:5.7
2、重命名镜像名
docker tag hub.c.163.com/library/mysql:5.7 mysql:5.7
3、创建文件备份目录
3.1、mkdir /Users/trusause/mysql/conf
3.2、mkdir /Users/trusause/mysql/logs
3.3、mkdir /Users/trusause/mysql/data/mysql
4、/Users/trusause/mysql/conf下新建my.cnf文件(5.7.18以后需要)
# 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/
---------------------
作者:yan_zuoyu
来源:CSDN
原文:https://blog.csdn.net/yan_zuoyu/article/details/80736507
版权声明:本文为博主原创文章,转载请附上博文链接!
5、启动
方式一:通过docker命令启动
docker run -p 3306:3306 --name mysql:5.7 -v /Users/trusause/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /Users/trusause/mysql/logs:/logs -v /Users/trusause/mysql/data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mysql:5.7
方式二:通过docker-compose命令加载配置文件启动
注意:一定需要加tty: true否则启动会报错:Exited (0) About a minute ago
启动失败是因为缺失了控制终端的配置,这里有两种方式修复:
1、增加一个配置tty: true
version: '2'
services:
mysql:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: "password"
ports:
- 3306:3306
restart: always
logging:
options:
max-size: "10m"
max-file: "10"
tty: true
2、使用exec重新创建容器(不推荐)
这种方式并不推荐,因为这样做虽然可以启动容器,但是只能重新创建一个容器,具体方法如下:
1. 使用docker-compose up -d命令启动后,由于没有tty:true的配置,容器就退出了;
2. 这时候执行命令docker-compose run master /bin/bash,会创建一个容器,并且进入这个容器;
3. 在当前电脑再打开一个控制台,执行docker ps命令,发现新建了一个容器,状态正常