chown: changing ownership of ‘/var/lib/mysql/‘: Permission denied
今天在搭建mysql集群的时候发现一个问题,我创建的docker master实例为下面的语句:docker run -p 3307:3306 --name mysql-master-v /mydata/mysql/master/log:/var/log/mysql-v /mydata/mysql/master/data:/var/lib/mysql-v /mydata/mysql/master/
今天在搭建mysql集群的时候发现一个问题,我创建的docker master实例为下面的语句:
docker run -p 3307:3306 --name mysql-master
-v /mydata/mysql/master/log:/var/log/mysql
-v /mydata/mysql/master/data:/var/lib/mysql
-v /mydata/mysql/master/conf:/etc/mysql
-e MYSQL_ROOT_PASSWORD=root
-d mysql:5.7
运行这个语句之后,使用docker ps命令查看,发现docker并没有启动起来,然后再运行docker ps -a 发现有mysql容器,但是端口并没有映射,具体查询方式如下:
[root@centos3 master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14f4385c4ac9 mysql "docker-entrypoint..." About a minute ago Exited (0) 39 seconds ago mysql-test
37957343c5b4 mysql:5.7 "docker-entrypoint..." 14 minutes ago Exited (1) 9 seconds ago mysql-master
然后根据容器id去查询日志,发现显示的日志如下:
[root@centos3 master]# docker logs 379
2022-01-09 07:41:19+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
chown: changing ownership of '/var/lib/mysql/': Permission denied
2022-01-09 07:56:06+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
chown: changing ownership of '/var/lib/mysql/': Permission denied
然后就发现运行mysql的这个容器失败了,没有运行起来,为什么会提示挂载权限失败呢?
因为我的虚拟机系统是centos7的版本,而Centos7的安全Selinux禁止了一些安全权限,导致挂载信息时出现权限不足,网上查询资料之后给出了如下3种解决方案:
1.在docker run中加入 –privileged=true 给容器加上特定权限
2.关闭selinux
3.在selinux添加规则,修改挂载目录的特定权限
为了不影响Centos7的安全Selinux禁止规则,在这里我引用第一种解决方案,将运行mysql容器命令修改如下:
docker run -p 3307:3306 --name mysql-master --privileged=true
-v /mydata/mysql/master/log:/var/log/mysql
-v /mydata/mysql/master/data:/var/lib/mysql
-v /mydata/mysql/master/conf:/etc/mysql
-e MYSQL_ROOT_PASSWORD=root
-d mysql:5.7
然后在用docker ps检查容器运行是否正常,结果如下:
[root@centos3 lib]# docker run -p 3307:3306 --name mysql-master --privileged=true \
> -v /mydata/mysql/master/log:/var/log/mysql \
> -v /mydata/mysql/master/data:/var/lib/mysql \
> -v /mydata/mysql/master/conf:/etc/mysql \
> -e MYSQL_ROOT_PASSWORD=root \
> -d mysql:5.7
fe5b208835fddf5a8da27e53b291111f78ff90204e214378524b1d9bc9879714
[root@centos3 lib]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe5b208835fd mysql:5.7 "docker-entrypoint..." 3 seconds ago Up 2 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp mysql-master
从上面可以看出,mysql容器运行成功。
现在将上面的命令做如下详细解释:
docker run -p 3307:3306 --name mysql-master --privileged=true
-v /mydata/mysql/master/log:/var/log/mysql
-v /mydata/mysql/master/data:/var/lib/mysql
-v /mydata/mysql/master/conf:/etc/mysql
-e MYSQL_ROOT_PASSWORD=root
-d mysql:5.7
参数说明
-p 3307:3306 将容器的3306端口映射到主机的3307端口
-v /mydata/mysql/master/conf:/etc/mysql ;将配置文件夹挂载到主机上
-v /mydata/mysql/master/log:/var/log/mysql:将日志文件夹挂载到主机上
-v /mydata/mysql/master/data:/var/lib/mysql:将数据文件夹挂载到主机上
-e MYSQL_ROOT_PASSWORD=root:初始化root用户的密码
更多推荐
所有评论(0)