快速搭建以下的环境:

VirtualBox

VM环境

Vagrant

成品镜像

$ vagrant box add centos-7 ./centos-7.0-x86_64.box
$ vagrant init centos-7
$ vagrant up
$ vagrant ssh

修改端口
20200411145929

管理员

$ su root
# 密码 vagrant
$ whoami

入门案例:https://www.cnblogs.com/lawsssscat/p/12676477.html
修改语言:https://blog.csdn.net/liupeifeng3514/article/details/79005568

docker

虚拟容器

docker常用命令
另外:
docker 容器重启策略: docker inspect 容器ID

删除旧版本

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

安装:官网教程 https://docs.docker.com/engine/install/centos/ (不推荐)

# 推荐
$ sudo yum install docker

开启

$ sudo systemctl start docker
$ sudo systemctl enable docker

阿里云容器镜像服务:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

$ sudo mkdir -p /etc/docker
$ sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://eslh5xx9.mirror.aliyuncs.com"]
}
EOF
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

MySQL

启动mysql

异常处理:error creating overlay mount to /var/lib/docker/overlay2

$ sudo docker pull mysql:5.7
$ sudo docker images 

$ sudo docker run --name mysql --privileged=true -p 3306:3306 \
-v /usr/mydata/mysql/log:/var/log/mysql \
-v /usr/mydata/mysql/data:/var/lib/mysql \
-v /usr/mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7 
  • -v 文件目录(卷)映射
  • /usr/mydata/mysql/log:/var/log/mysql 日志映射到本地
  • /usr/mydata/mysql/data:/var/lib/mysql 数据库数据映射到本地
  • /usr/mydata/mysql/conf:/etc/mysql 配置文件映射到本地
  • MYSQL_ROOT_PASSWORD=root (必须)设置数据库密码

<mark>映射的目录可以自定义,但是不要映射到 /dev 目录,因为每次重启会重置该目录</mark>
(/dev 目录一般用于存储设备文件。)
参考:https://www.cnblogs.com/yudar/p/5809154.html

20200411141732

20200411142940

进入mysql

docker exec -it mysql /bin/bash

20200411143318

Docker官网关于MySQL:5.7:https://hub.docker.com/_/mysql
Docker官网关于MySQL:5.7的Dockerfile:https://github.com/docker-library/mysql/blob/d284e15821ac64b6eda1b146775bf4b6f4844077/5.7/Dockerfile

修改编码

vim  /usr/mydata/mysql/conf/my.cnf
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
init_connect='SET collation_connection=utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve

重启mysql ,并进入

 docker restart mysql
 docker exec -it mysql /bin/bash

20200411145025

开启随docker启动而启动

$ sudo docker update mysql --restart=always

docker update:更新一个或多个容器的配置

redis

安装

$ sudo docker pull redis

开启数据持久化

mkdir -p /usr/mydata/redis/conf
vim /usr/mydata/redis/conf/redis.conf

输入配置

# 启用AOF持久化
appendonly yes

启动

$ sudo docker run --name redis \
-v /usr/mydata/redis/data:/data \
-v /usr/mydata/redis/conf:/etc/redis \
-p 6379:6379  \
-d redis redis-server /etc/redis/redis.conf

注意:这里的路径映射不能是文件,因为会被覆盖为目录,再次启动会报错

开启随docker启动而启动

$ sudo docker update redis --restart=always

客户端登录

docker exec -it redis redis-cli

20200411190534

docker restart redis