docker msyql redis 容器创建以及备份

docker 建立 msyql 容器

      • mysql
      • docker 连接 redis
      • spring boot 构建镜像
      • nginx

mysql

  1. docker pull mysql:8.0.17

  2. 	docker run \
    	-p 3307:3306 \
    	--name mysql \
    	--restart always \
    	-v /var/docker/mysql/data/:/var/lib/mysql \
    	-v /var/docker/mysql/conf/:/etc/mysql/conf.d \
    	-v /var/docker/mysql/init/:/docker-entrypoint-initdb.d \
    	-e MYSQL_ROOT_PASSWORD=xx \
    	-d mysql:8.0.17
    

    其它参数
    -e MYSQL_USER=”fengwei”:添加用户fengwei
    -e MYSQL_PASSWORD=”pwd123”:设置fengwei的密码伟pwd123
    -e MYSQL_ROOT_PASSWORD=”rootpwd123”:设置root的密码伟rootpwd123
    –character-set-server=utf8:设置字符集为utf8
    –collation-server=utf8_general_ci:设置字符比较规则为utf8_general_ci

  3. 进入容器实例: docker exec -it mysql bash

  4. 支持远程连接数据库

       	mysql -uroot -pxx
       	ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'xx';
       	flush privileges;
    
docker run -p 3307:3306 --name mysql --restart always -v D:/docker/mysql/data/:/var/lib/mysql -v D:/docker/mysql/init/:/docker-entrypoint-initdb.d -v D:/docker/mysql/my.cnf:/etc/my.cnf -e MYSQL_ROOT_PASSWORD=hztc12345 -d mysql:8.0.17 --default-authentication-plugin=mysql_native_password
  1. 定时备份

     ```
     source /etc/profile
     . ~/.bash_profile
     #!/bin/bash
     
     DATABASE=wechat #database name
     DB_USER=root #database username
     DB_PASS="xx" #database password
     BACKUP=/var/docker/backups/mysql/ #backup path
     DATE=`date +%Y%m%d%H%M` 
     
     # 判断目录是不是已经存在,如果不存在则创建
     if [ ! -d ${BACKUP} ]; then
       mkdir -p ${BACKUP}
     fi
     
     docker exec -i mysql mysqldump -u$DB_USER -p$DB_PASS -h 127.0.0.1 -R --opt $DATABASE |gzip > ${BACKUP}\/${DATABASE}_${DATE}.sql.gz
      
     #just backup the latest 5 days 
     #测试:find /data/bak/mysqldata -name "ei_*.sql.gz" -type f -mtime +5 -exec rm {} \; > /dev/null 2>&1
     find ${BACKUP} -name "${DATABASE}_*.sql.gz" -type f -mtime +5 -exec rm {} \; > /dev/null 2>&1
     
     ```
    

docker exec -i mysql 记住一定不能加 “t”, 否则定时器执行不成功

  • 测试脚本 : sh /shell/docker_mysql_bak.sh
  • 添加可执行权限:chmod u+x /shell/docker_mysql_bak.sh
  • 查看权限 ls -l /shell/docker_mysql_bak.sh
  • 设置定时任务
    添加任务: crontab -e
     00 03 * * * /shell/docker_mysql_bak.sh
    
    查看任务:crontab -l

mysql自动执行sql
mysql docker

docker 连接 redis

  1. docker pull redis:5.0.5
  2. docker run \
    -p 6380:6379 \
    --name redis \
    -v /var/docker/redis/data:/data \
    -v /var/docker/redis/conf/redis.conf:/etc/redis/redis.conf \
    -d redis:5.0.5 redis-server /etc/redis/redis.conf \
    --appendonly yes
    
    • 添加对外端口-p 6380:6379 外面不需密码就可访问, 去掉不能访问6379端口

spring boot 构建镜像

  1. 一个镜像运行两个容器实例
    docker run \
    --name wechat8001 \
    -p 8001:8001 \
    --link mysql:mysql \
    --link redis:redis \
    -d registry.cn-beijing.aliyuncs.com/sunxin/wechat:1.0.1
    
    docker run \
    --name wechat8002 \
    -p 8002:8001 \
    --link mysql:mysql \
    --link redis:redis \
    -d registry.cn-beijing.aliyuncs.com/sunxin/wechat:1.0.1
    

nginx

  1. docker pull nginx:1.17.3

  2. 用命令:cat /etc/nginx/nginx.conf 查看nginx.conf文件内容然后拷贝出来
    用命令:cat /etc/nginx/conf.d/default.conf 查看default.conf文件内容然后拷贝出来

    mkdir -p /var/docker/nginx/conf / 
    mkdir -p /var/docker/nginx/conf.d /
    mkdir -p /var/docker/nginx/html /
    mkdir -p /var/docker/nginx/logs
    
    
     docker run \
     --name nginx \
     -p 8000:80 \
     -v /var/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
     -v /var/docker/nginx/conf.d:/etc/nginx/conf.d \
     -v /var/docker/nginx/logs:/var/log/nginx \
     -v /var/docker/nginx/html:/usr/share/nginx/html \
     --link wechat8001:wechat8001 \
     --link wechat8002:wechat8002 \
     nginx:1.17.3
    

    安全证书

     docker run \
     --name nginx \
     -p 8000:80 \
     -p 8443:443 \
     -v /var/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
     -v /var/docker/nginx/cert:/etc/nginx/cert \
     -v /var/docker/nginx/conf.d:/etc/nginx/conf.d \
     -v /var/docker/nginx/logs:/var/log/nginx \
     -v /var/docker/nginx/html:/usr/share/nginx/html \
     --link wechat8001:wechat8001 \
     --link wechat8002:wechat8002 \
     nginx:1.17.3
    

    nginx中不要使用下划线命名

你可能感兴趣的:(docker)