繁体   English   中英

Docker mysql容器无法使用启动时定义的数据卷

[英]Docker mysql container cannot use a data volume defined on startup

尝试在主机上安装数据量的mysql容器时运行时遇到问题。

这是一个dockerfile:

from ubuntu:14.04
maintainer Tiago Pires, tandrepires@gmail.com

# Because docker replaces /sbin/init: https://github.com/dotcloud/docker/issues/1024
RUN dpkg-divert --local --rename --add /sbin/initctl

RUN echo "deb http://archive.ubuntu.com/ubuntu trusty main universe" > /etc/apt/sources.list
RUN apt-get update && apt-get upgrade -y

ADD create_database.sql /tmp/create_database.sql

RUN apt-get install -y mysql-server

RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf

RUN /usr/bin/mysqld_safe & \
    sleep 10s && \
        mysql -u root < /tmp/create_database.sql

VOLUME ["/var/lib/mysql", "/var/log/mysql"]

CMD ["mysqld_safe"]

我成功构建并运行了它。 容器已在docker主目录中创建了一个卷,所有必需的mysql数据均已存在。

ls -l /var/lib/docker/vfs/dir/262aa2622c54f3a5d28ea9c1b90e6f67c93309f73921c724354a0e0155af2f0f
total 28684
-rw-r--r-- 1 root       root              0 Jun  1 14:12 debian-5.5.flag
-rw-rw---- 1 messagebus messagebus 18874368 Jun  1 14:12 ibdata1
-rw-rw---- 1 messagebus messagebus  5242880 Sep 23 18:44 ib_logfile0
-rw-rw---- 1 messagebus messagebus  5242880 Jun  1 14:12 ib_logfile1
drwx------ 2 messagebus root           4096 Jun  1 14:12 mysql
drwx------ 2 messagebus messagebus     4096 Jun  1 14:12 performance_schema
drwx------ 2 messagebus messagebus     4096 Jun  1 14:12 sonar

现在,我想显式定义一个文件夹来装入数据卷。

docker run -d -p 3306:3306 \
        -v $HOME/data:/var/lib/mysql \
        -v $HOME/log:/var/log/mysql \
        --name=sonar-sql sonar-sql

但是这次我有一个错误:

150924  7:59:07  InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
InnoDB: File name ./ibdata1
InnoDB: File operation call: 'create'.
InnoDB: Cannot continue operation.

我看到数据文件夹已创建。 我尝试更改特权并再次运行еру容器:

chmod a+wrX data

这次我有另一个错误:

150924  8:02:20 InnoDB: Completed initialization of buffer pool
InnoDB: The first specified data file ./ibdata1 did not exist:
InnoDB: a new database to be created!
150924  8:02:20  InnoDB: Setting file ./ibdata1 size to 10 MB
InnoDB: Database physically writes the file full: wait...
150924  8:02:20  InnoDB: Log file ./ib_logfile0 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile0 size to 5 MB
InnoDB: Database physically writes the file full: wait...
150924  8:02:21  InnoDB: Log file ./ib_logfile1 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile1 size to 5 MB
InnoDB: Database physically writes the file full: wait...
InnoDB: Doublewrite buffer not found: creating new
InnoDB: Doublewrite buffer created
InnoDB: 127 rollback segment(s) active.
InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
150924  8:02:21  InnoDB: Waiting for the background threads to start
150924  8:02:22 InnoDB: 5.5.35 started; log sequence number 0
150924  8:02:22 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
150924  8:02:22 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
150924  8:02:22 [Note] Server socket created on IP: '0.0.0.0'.
150924  8:02:22 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
150924 08:02:22 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

现在,数据文件夹的内容如下(一些必需的文件不存在):

ls -l    
-rw-rw---- 1 messagebus messagebus 18874368 Sep 24 11:02 ibdata1
-rw-rw---- 1 messagebus messagebus  5242880 Sep 24 11:02 ib_logfile0
-rw-rw---- 1 messagebus messagebus  5242880 Sep 24 11:02 ib_logfile1

mysql进程是从容器内的mysql用户运行的,该用户在主机上不存在。

ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.4  0.0   4440   776 ?        Ss   08:07   0:00 /bin/sh /usr/bin/mysqld_safe
mysql      352  1.0  0.7 492520 55636 ?        Sl   08:07   0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mys
root       370  1.0  0.0  18136  1860 ?        Ss   08:08   0:00 bash
root       385  0.0  0.0  15564  1140 ?        R+   08:08   0:00 ps aux

问: Docker文档说,VOLUME命令和-v选项应该做同样的事情,请向我解释为什么我对VOLUME没问题,但对“ -v”失败。

Ubuntu 14.04.2 LTS Docker版本1.6.0,内部版本4749651

回答你的问题。

-v选项有两种形式。 一个确实与VOLUME指令相同,并且看起来像-v /var/lib/mysql -v /var/log/mysql 它定义了容器的体积。

您的示例中使用的表单是不同的,因为它将主机目录装载到容器的FS。 请参阅https://docs.docker.com/userguide/dockervolumes/#mount-a-host-directory-as-a-data-volume

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM