windows vagrant docker 安装 mysql redis

docker 安装 mysql redis

centos7 安装 docker

https://zhuanlan.zhihu.com/p/140732202

https://www.jianshu.com/p/2711c296cfce

https://cloud.tencent.com/developer/article/1701451

https://www.jianshu.com/p/7d9ff93bc89e

windows 安装 vagrant

https://zhuanlan.zhihu.com/p/259833884

centos7 安装 docker docker-compose

创建目录结构

docker/docker-compose.yml
docker/mysql/data
docker/mysql/my.cnf
docker/redis/data
docker/redis/redis.conf

编写my.cnf文件

[client]
 port = 3306
 socket = /var/lib/mysql/data/mysql.sock
[mysqld]
 # 针对5.7版本执行group by字句出错问题解决
 sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
 # 一般配置选项
 basedir = /var/lib/mysql
 datadir = /var/lib/mysql/data
 port = 3306
 socket = /var/lib/mysql/data/mysql.sock
 lc-messages-dir = /usr/share/mysql # 务必配置此项,否则执行sql出错时,只能显示错误代码而不显示具体错误消息
 character-set-server=utf8
 lower_case_table_names=1
 back_log = 300
 max_connections = 3000
 max_connect_errors = 50
 table_open_cache = 4096
 max_allowed_packet = 32M
 #binlog_cache_size = 4M
 max_heap_table_size = 128M
 read_rnd_buffer_size = 16M
 sort_buffer_size = 16M
 join_buffer_size = 16M
 thread_cache_size = 16
 query_cache_size = 64M
 query_cache_limit = 4M
 ft_min_word_len = 8
 thread_stack = 512K
 transaction_isolation = REPEATABLE-READ
 tmp_table_size = 64M
 #log-bin=mysql-bin
 long_query_time = 6
 server_id=1
 innodb_buffer_pool_size = 256M
 innodb_thread_concurrency = 16
 innodb_log_buffer_size = 16M

编写redis.conf文件

这里有个坑,通常单独下载redis都是把守护进程开启,但是这里须设置为no,因为daemonize yes,他的作用是开启守护进程模式,在该模式下,redis会在后台运行,并将进程pid号写入至redis.conf选项pidfile设置的文件中,此时redis将一直运行,除非手动kill该进程。但是在后面的命令中“-d”选项也是开启daemonize,只不过这是docker自家的方式。所以会产生冲突,这也是很多人不能正常启动的原因(当时在这个坑里呆了好久)

#注释掉,允许所有其他ip访问,真实使用最好坐下限制,只允许某些主机访问
#bind 127.0.0.1
# 若使用开机启动,生成pid,该项必须设置为诶yes,否则redis将不能够正常执行开机启动(systemctl start redis,执行后一直卡着,直到超时)
daemonize no
# 允许其他机器上的客户端连接当前redis,配置文件设置该项,则开机启动处就可以去掉--protected no 
protected-mode no
#设置密码
requirepass root  
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

编写 docker-compose.yml 文件

version: '3'
services:
  mysql:
    hostname: mysql
    image: mysql:5.7.26
    # network_mode: "host" # 如果需要容器使用宿主机IP(内网IP),则可以配置此项
    container_name: mysql # 指定容器名称,如果不设置此参数,则由系统自动生成
    restart: unless-stopped # 设置容器自启模式
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci # 设置utf8字符集
    environment:
      - TZ=Asia/Shanghai # 设置容器时区与宿主机保持一致
      - MYSQL_ROOT_PASSWORD=123456 # 设置root密码
    volumes:
       - /etc/localtime:/etc/localtime:ro # 设置容器时区与宿主机保持一致
       - ./mysql/data:/var/lib/mysql/data # 映射数据库保存目录到宿主机,防止数据丢失
       - ./mysql/my.cnf:/etc/mysql/my.cnf # 映射数据库配置文件
    ports:
        - "3306:3306"

  redis:
    hostname: redis
    image: redis:5.0.4
    container_name: redis
    restart: always
    command: redis-server /etc/redis.conf # 启动redis命令
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - /etc/localtime:/etc/localtime:ro # 设置容器时区与宿主机保持一致
      - ./redis/data:/data
      - ./redis/redis.conf:/etc/redis.conf
    ports:
        - "6379:6379"

安装docker

//使用阿里云的镜像地址
    yum install -y yum-utils
    yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

y
  1. 更新 yum 软件包索引

       yum makecache fas
    
  2. docker-ce

    yum install docker-ce docker-ce-cli containerd.io
    
  3. 运行 docker

     systemctl start docker
    
  4. 检测是否成功 sudo docker run hello-world

安装docker docker-compose

curl -L https://get.daocloud.io/docker/compose/releases/download/1.27.4/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

添加国内加速

[https://gist.github.com/y0ngb1n/7e8f16af3242c7815e7ca2f0833d3ea6]:

vi /etc/docker/daemon.json

{
    "registry-mirrors": [
        "https://1nj0zren.mirror.aliyuncs.com",
        "https://docker.mirrors.ustc.edu.cn",
        "http://f1361db2.m.daocloud.io",
        "https://dockerhub.azk8s.cn"
    ]
}

systemctl daemon-reload
 systemctl restart docker

创建并启动容器

docker-compose -f docker-compose.yml up -d

up选项启动容器,-d选项以守护模式运行,镜像如果没有,docker会先拉取镜像,有则直接创建并启动容器,注意文件运行前不能有其==同名容器==,还有须注意==防火墙设置==

如果要批量停止容器并清除容器,可以使用down命令

docker-compose -f docker-compose.yml down

如果要重新构建容器,可以使用--build选项

docker-compose -f docker-compose.yml up --build -d

https://segmentfault.com/q/1010000002392472


列出所有的容器 ID
1
docker ps -aq
停止所有的容器
1
docker stop $(docker ps -aq)
删除所有的容器
1
docker rm $(docker ps -aq)
删除所有的镜像
1
docker rmi $(docker images -q)
复制文件
1
2
docker cp mycontainer:/opt/file.txt /opt/local/
docker cp /opt/local/file.txt mycontainer:/opt/




管理

docker

systemctl start docker        // 启动
systemctl stop docker        // 关闭docker
systemctl status docker     // 查看docker的运行状态
systemctl restart  docker   // 重启docker

centoros 7

reboot 重启
halt 关机

vagrant

https://vagrant.ninghao.net/vm--manage.html

vagrant status // 查看状态
vagrant halt   // 关闭
vagrant suspend  //休眠
vagrant destroy  //销毁
vagrant up  //启动
vagrant ssh //连接