docker挂载目录报错及启动mysql8.0容器秒退的解决方案

一、挂载报错:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:75: mounting “/root/mysql_slave/my.cnf” to rootfs at “/etc/mysql/my.cnf” caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.

在这里插入图片描述

1、场景:

将mysql的数据文件mysql(目录)及配置文件my.cnf(文件)挂载到外部宿主机上。

2、操作方法:

首先在root目录下自定义mysql_slave目录,切换到该目录
在这里插入图片描述
执行以下命令

[root@localhost mysql_slave]# docker run -id -v $PWD/data:/var/lib/mysql -v $PWD/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=123 --name slave1 -P mysql

3、原因:

容器中的my.cnf是文件,但是宿主机的my.cnf是目录

4、解决方案:

在自定义的目录mysql_slave下执行touch my.cnf,data可以不必实现创建,因为宿主机的文件类型和要挂载的文件类型都是目录。
在这里插入图片描述

5、再次尝试,成功创建

在这里插入图片描述

二、秒退

1、场景

上面虽然成功运行了mysql容器,但是docker ps 发现容器秒退了(mysql版本8.0)
在这里插入图片描述

2、原因:

2.1 当指定了外部配置文件与外部存储路径时(没有指定的话,不需要挂载mysql-files),也需要指定 /var/lib/mysql-files的外部目录

2.2 运行docker logs mysql查看日志,查找问题

[root@localhost mysql_slave]# docker run -id -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 --name slave66 -P mysql

如上只是挂载了数据文件,并没有将容器的mysql的配置文件挂载到宿主机上,所以没有挂载mysql-files

docker logs slave2

2022-03-18 07:04:23+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
2022-03-18 07:04:24+00:00 [Note] [Entrypoint]: Switching to dedicated user ‘mysql’
2022-03-18 07:04:24+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
2022-03-18 07:04:24+00:00 [Note] [Entrypoint]: Initializing database files
2022-03-18T07:04:24.182224Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.28) initializing of server in progress as process 43
2022-03-18T07:04:24.264504Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-03-18T07:04:24.940170Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-03-18T07:04:27.557977Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2022-03-18 07:04:31+00:00 [Note] [Entrypoint]: Database files initialized
2022-03-18 07:04:31+00:00 [Note] [Entrypoint]: Starting temporary server
mysqld: Error on realpath() on ‘/var/lib/mysql-files’ (Error 2 - No such file or directory)
2022-03-18T07:04:32.950014Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
2022-03-18 07:04:33+00:00 [ERROR] [Entrypoint]: Unable to start server.

在这里插入图片描述

3、解决方案:

3.1 在启动容器时,也将mysql容器的mysql_files目录挂载到宿主机上,完整命令如下:

[root@localhost mysql_slave]# docker run -id -v $PWD/data:/var/lib/mysql -v $PWD/my.cnf:/etc/mysql/my.cnf -v $PWD/mysql-files:/var/lib/mysql-files -e MYSQL_ROOT_PASSWORD=123 --name slave8 -P mysql

在这里插入图片描述
3.2 还有一种方案,就是启动容器前,在创建的my.cnf 文件中加入如下内容:

[client]

default-character-set=utf8

[mysql]

default-character-set=utf8

[mysqld]

init_connect='SET collation_connection = utf8_unicode_ci'

init_connect='SET NAMES utf8'

character-set-server=utf8

collation-server=utf8_unicode_ci

skip-character-set-client-handshake

secure_file_priv=/var/lib/mysql

以上两种方案都能解决问题。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值