以mysql 为例

  • 查看docker 容器挂载和映射

docker inspect -f '{{range .Mounts}}{{.Source}} -> {{.Destination}}{{"\n"}}{{end}}' <容器ID或容器名称>

  • 查看容器的环境变量

docker inspect -f '{{range .Config.Env}}{{.}}{{"\n"}}{{end}}' <容器ID或容器名称>

  • 创建 mysql.yml 文件

vim mysql8.0.17.yml

version: '3'
services:
  mysql: # 服务名称
    image: mysql:8.0.17 # 或其它mysql版本
    container_name: mysql8 # 容器名称
    environment:
      - MYSQL_ROOT_PASSWORD=1221 # root用户密码
#      - TZ=Asia/Shanghai # 设置容器时区 我这里通过下面挂载方式同步的宿主机时区和时间了,这里忽略
    volumes:
      # 映射日志目录,宿主机:容器
      - /opt/mysql8/conf/my.cnf:/etc/mysql/my.cnf
      - /opt/mysql8/data:/opt/mysql/data
    ports:
      - 3306:3306 # 指定宿主机端口与容器端口映射关系,宿主机:容器
    restart: always # 容器随docker启动自启

记得备份数据库数据

记得备份数据库数据

记得备份数据库数据

  • 停止并删除当前的mysql容器,删除镜像

  • 根据docker-compose.yml 文件创建容器

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

  • 测试连接

    • 失败报错:缺少认证

  • 修改mysql.cnf :default_authentication_plugin=mysql_native_password

  • 失败报错:lower_case_table_names 属性与文件属性大小写不匹配

 [root@VM-16-12-centos dockerCompose]# docker logs mysql8 

ERROR: mysqld failed while attempting to check config
command was: "mysqld --verbose --help"

2023-10-16T02:07:06.429757Z 0 [ERROR] [MY-010158] [Server] The server option 'lower_case_table_names' is configured to use case sensitive table names but the data directory is on a case-insensitive file system which is an unsupported combination. Please consider either using a case sensitive file system for your data directory or switching to a case-insensitive table name mode.
2023-10-16T02:07:06.439151Z 0 [ERROR] [MY-010119] [Server] Aborting

ERROR: mysqld failed while attempting to check config
command was: "mysqld --verbose --help"

2023-10-16T02:07:07.129928Z 0 [ERROR] [MY-010158] [Server] The server option 'lower_case_table_names' is configured to use case sensitive table names but the data directory is on a case-insensitive file system which is an unsupported combination. Please consider either using a case sensitive file system for your data directory or switching to a case-insensitive table name mode.
2023-10-16T02:07:07.131760Z 0 [ERROR] [MY-010119] [Server] Aborting

ERROR: mysqld failed while attempting to check config
command was: "mysqld --verbose --help"

2023-10-16T02:07:07.908188Z 0 [ERROR] [MY-010158] [Server] The server option 'lower_case_table_names' is configured to use case sensitive table names but the data directory is on a case-insensitive file system which is an unsupported combination. Please consider either using a case sensitive file system for your data directory or switching to a case-insensitive table name mode.
2023-10-16T02:07:07.909959Z 0 [ERROR] [MY-010119] [Server] Aborting

ERROR: mysqld failed while attempting to check config
command was: "mysqld --verbose --help"

2023-10-16T02:07:08.832323Z 0 [ERROR] [MY-010158] [Server] The server option 'lower_case_table_names' is configured to use case sensitive table names but the data directory is on a case-insensitive file system which is an unsupported combination. Please consider either using a case sensitive file system for your data directory or switching to a case-insensitive table name mode.
2023-10-16T02:07:08.833785Z 0 [ERROR] [MY-010119] [Server] Aborting

ERROR: mysqld failed while attempting to check config
command was: "mysqld --verbose --help"

2023-10-16T02:07:10.121277Z 0 [ERROR] [MY-010158] [Server] The server option 'lower_case_table_names' is configured to use case sensitive table names but the data directory is on a case-insensitive file system which is an unsupported combination. Please consider either using a case sensitive file system for your data directory or switching to a case-insensitive table name mode.
2023-10-16T02:07:10.123161Z 0 [ERROR] [MY-010119] [Server] Aborting

ERROR: mysqld failed while attempting to check config
command was: "mysqld --verbose --help"

2023-10-16T02:07:12.543762Z 0 [ERROR] [MY-010158] [Server] The server option 'lower_case_table_names' is configured to use case sensitive table names but the data directory is on a case-insensitive file system which is an unsupported combination. Please consider either using a case sensitive file system for your data directory or switching to a case-insensitive table name mode.
2023-10-16T02:07:12.545153Z 0 [ERROR] [MY-010119] [Server] Aborting
  • - 修改:映射的mysql.cnf

    • lower_case_table_names=1

G 数据没了

原因:上个用镜像创建的 容器 都没有配置映射

最好进容器内部看下真实的路径