天天看点

mysql安装(docker)

mkdir /opt/mysql

vim /opt/mysql/Dockerfile

5.7

FROM mysql:5.7.26
EXPOSE 3306      

8.0

FROM mysql:latest
EXPOSE 3306      

创建文件夹

mkdir /opt/mysql/var/lib/mysql -p

vim /opt/mysql/docker-compose.yml

version: '3.6'
services:
  mysql: 
    image: v-mysql:5.7
    container_name: mysql
    network_mode: "host"
    build: 
      context: .
      dockerfile: Dockerfile
    environment: 
      MYSQL_ROOT_HOST: '%'
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_USER: 'test'
      MYSQL_PASS: '123456'
      MYSQL_ALLOW_EMPTY_PASSWORD: "no"
    restart: always
    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: 
      - "./var/lib/mysql:/var/lib/mysql"
      - "./etc/my.cnf:/etc/my.cnf"
      - "./docker-entrypoint-initdb.d/:/docker-entrypoint-initdb.d/"
    ports:
      - "3306:3306"      

不用Dockerfile时

version: '3.6'
services:
  mysql: 
    image: mysql:5.7.26
    container_name: mysql
    network_mode: "host"
    environment: 
      MYSQL_ROOT_HOST: '%'
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_USER: 'test'
      MYSQL_PASS: '123456'
      MYSQL_ALLOW_EMPTY_PASSWORD: "no"
    restart: always
    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: 
      - "./var/lib/mysql:/var/lib/mysql"
      - "./etc/my.cnf:/etc/my.cnf"
      - "./docker-entrypoint-initdb.d/:/docker-entrypoint-initdb.d/"
    ports:
      - "3306:3306"      
version: '3.6'
services:
  mysql: 
    image: v-mysql:8.0
    container_name: mysql
    network_mode: "host"
    build: 
      context: .
      dockerfile: Dockerfile
    environment: 
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_USER: 'test'
      MYSQL_PASS: '123456'
      MYSQL_ALLOW_EMPTY_PASSWORD: "no"
    restart: always
    command: 
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
    volumes: 
      - "./var/lib/mysql:/var/lib/mysql"
      - "./etc/my.cnf:/etc/my.cnf"
      - "./docker-entrypoint-initdb.d/:/docker-entrypoint-initdb.d/"
    ports:
      - "3306:3306"      

mysql配置文件

mkdir /opt/mysql/etc

vim /opt/mysql/etc/my.cnf

[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8      

/docker-entrypoint-initdb.d/init.sql

sql脚本(略)

build和启动

cd /opt/mysql

docker-compose build

docker-compose up -d

docker-compose down

docker-compose restart

进入容器

docker exec -it mysql /bin/bash