一、挂载报错:
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
以上两种方案都能解决问题。