docker部署MySQL5.7

 docker run直接部署

docker run -d \
  -p 3306:3306 \
  --name mysql \
  --restart=always \
  --privileged=true \
  -v /data/myql/data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD="123456" \
  mysql:5.7 --character-set-server=utf8

docker-compose方式部署

部署docker-compose

wget https://github.com/docker/compose/releases/download/v2.22.0/docker-compose-linux-x86_64
\mv docker-compose-linux-x86_64 /usr/bin/docker-compose
chmod +x /usr/bin/docker-compose
docker-compose -v

 

docker-compose.yml

version: '3'

services:
  mysql:
    restart: always
    image: mysql:5.7.22
    container_name: mysql
    ports:
      - 3306:3306
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: "12345%"
    command:
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M
      --sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
    volumes:
      - /data/mysql/data:/var/lib/mysql
    logging:
        driver: "json-file"
        options:
            max-size: "200m"
            max-file: "10"

 

启动脚本

cat >/data/mysql/start.sh <<'EOF
#!/bin/bash

docker-compose -f /data/mysql/docker-compose.yml up -d
docker ps -a | grep mysql
exit 0
EOF

cat >/data/jumpserver-mysql/stop.sh <<'EOF
#!/bin/bash

docker-compose -f /data/mysql/docker-compose.yml down
docker ps -a | grep mysql
exit 0
EOF

chmod +x /data/mysql/*.sh
bash /data/mysql/start.sh

# 创建数据库授权用户 docker exec -it mysql bash mysql -u root -p12345% create database jumpserver; grant all on test.* to test@"%" identified by '12345%'; flush privileges; exit exit

 

 

 
posted @ 2022-05-03 23:09  缺个好听的昵称  阅读(20)  评论(0编辑  收藏  举报