下载镜像

  1. docker pull mysql

创建挂载目录

  1. #挂载目录
  2. mkdir -p /data/docker/mysql/conf
  3. mkdir -p /data/docker/mysql/data
  4. mkdir -p /data/docker/mysql/logs

MySQL配置文件

  1. vim /data/docker/mysql/conf/mysql.conf

内容

  1. [mysql]
  2. port = 3306
  3. character-set-server=utf8

docker-compose.yml文件

  1. mkdir -p /data/docker-compose/mysql/
  2. vim /data/docker-compose/mysql/docker-compose.yml

内容

  1. version: '3.1'
  2. services:
  3. db:
  4. image: mysql
  5. restart: always
  6. container_name: mysql
  7. environment:
  8. MYSQL_ROOT_PASSWORD: 123456
  9. command:
  10. --default-authentication-plugin=mysql_native_password
  11. --character-set-server=utf8mb4
  12. --collation-server=utf8mb4_general_ci
  13. --explicit_defaults_for_timestamp=true
  14. --lower_case_table_names=1
  15. ports:
  16. - 3306:3306
  17. volumes:
  18. - /data/docker/mysql/data:/var/lib/mysql
  19. - /data/docker/mysql/conf:/etc/mysql/conf.d
  20. - /data/docker/mysql/logs:/logs

运行

  1. docker-compose up -d

测试

  1. docker ps
  2. docker logs -f mysql
  3. docker exec -it mysql bash
  4. mysql -u root -p
  5. 输入密码