在docker容器中的mysql不能在os x上运行安装的卷

在OS X上。

我试图在docker容器中通过boot2docker运行mysql,在主机上挂载volume /var/lib/mysql ,这样我就可以拥有持久的mysql数据。 我打算将来只使用一个数据容器,但现在我正在尝试使用此选项来执行此操作。

我使用下面的命令来运行容器:

docker run -v /Users/yash/summers/db:/var/lib/mysql -i -t 'image name'

/Users/yash/summers/db文件夹已经在那里。

我在这方面面临着执法问题。 使用命令行,我可以访问目录,创build/删除新文件,但是当我运行service mysql start ,我得到以下错误:

 150528 15:43:43 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 150528 15:43:43 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead. 150528 15:43:43 [Note] /usr/sbin/mysqld (mysqld 5.5.43-0ubuntu0.14.04.1) starting as process 909 ... 150528 15:43:43 [Warning] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive 150528 15:43:43 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.150528 15:43:43 [Note] Plugin 'FEDERATED' is disabled. /usr/sbin/mysqld: Table 'mysql.plugin' doesn't exist 150528 15:43:43 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it. 150528 15:43:43 InnoDB: The InnoDB memory heap is disabled 150528 15:43:43 InnoDB: Mutexes and rw_locks use GCC atomic builtins 150528 15:43:43 InnoDB: Compressed tables use zlib 1.2.8 150528 15:43:43 InnoDB: Using Linux native AIO 150528 15:43:43 InnoDB: Initializing buffer pool, size = 128.0M 150528 15:43:43 InnoDB: Completed initialization of buffer pool 150528 15:43:43 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. 150528 15:43:44 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended 

我试图解决这个过去两天,经历了这么多, 这样 ,这么多的网页。

我无法解决我的问题。 我认为我无法完美解决问题。

根据我的理解,在这些页面上列出了一些解决方法,包括更改uid和guid,但我认为它们没有很好的解释。

任何人都可以向我解释一个详细的解决方法。

更新1:我也尝试过使用数据容器,而且仍然面临同样的问题。

如果我不使用任何-v--volumes-from选项,我能够运行一切正常,所以我认为在mysql服务器中没有问题。

更新2:用于创build数据专用容器的Dockerfile:

 FROM ubuntu RUN mkdir /var/lib/mysql VOLUME /var/lib/mysql 

有两种不同的解决scheme。

  1. 解决scheme#1。 用Dockerfile

    (我不喜欢它,因为我更喜欢Docker Hub的官方镜像,没有任何改变)

    添加RUN usermod -u 1000 mysql到你的Docker文件,为用户“mysql”(像docker-machine里面的相同ID)设置ID 1000。

  2. 解决scheme#2。 用my.cnf

    仍然我使用我自己的configuration我更喜欢这个解决scheme。 我们有ID 1000的root用户,因为我们可以用这个用户运行MySQL:

    • my.cnf中

      主行是user = root (你可以使用sed来更改文件中的这一行,我更喜欢mount所有的文件)

       [client] port = 3306 socket = /var/run/mysqld/mysqld.sock default-character-set = utf8 [mysqld_safe] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock nice = 0 [mysqld] user = root pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp lc-messages-dir = /usr/share/mysql explicit_defaults_for_timestamp 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 # Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. #bind-address = 127.0.0.1 #log-error = /var/log/mysql/error.log # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # * IMPORTANT: Additional settings that can override those from this file! # The files must end with '.cnf', otherwise they'll be ignored. # !includedir /etc/mysql/conf.d/ 
    • 用这个文件更改默认my.cnf:

      docker run -it -v ./mysql/var/lib/mysql:/var/lib/mysql -v ./my.cnf::/etc/mysql/my.cnf mariadb:10.0.22

当你使用docker run -v选项时,docker不要处理文件的所有权。 因此,容器内已安装文件夹的所有者将具有与主机系统中相同的UID(用户名可以不同)。 您可以通过chmod命令更改容器内的文件或目录的所有权,但它也会更改主机系统中的所有权。 因此,您可以在运行Docker容器之前在主机系统上准备正确的所有权。 这个例子演示了它是如何工作的(在你的系统上试试):

我有我拥有的〜/ foo目录 – 我的UID是1000

 ~ $ ls -lnd foo drwxr-xr-x 2 1001 1000 4096 Nov 24 22:47 foo 

运行docker容器,挂载〜/ foo目录并显示容器内的所有权

 ~ $ docker run -it --rm -v ~/foo:/tmp/foo busybox ls -ln /tmp total 4 drwxr-xr-x 2 1000 1000 4096 Nov 24 21:47 foo 

容器内的所有者UID是一样的。 现在我更改了电脑上的目录所有权。

 ~ $ sudo chown 1001 foo 

再次启动容器,挂载foo目录并显示所有权

 ~ $ docker run -it --rm -v ~/foo:/tmp/foo busybox ls -ln /tmp total 4 drwxr-xr-x 2 1001 1000 4096 Nov 24 21:47 foo 

所有者UID也被更改了

但是有一个例外。 如果你尝试将卷挂载到不存在的目录,那么docker会自动创build这个丢失的目录(但是这是不推荐使用的function,所以不要这样做),并且这个目录将通过UID 0创build。试试看:

 ~ $ docker run -it --rm -v ~/foo:/tmp/bar/foo busybox ls -ln /tmp total 4 drwxr-xr-x 3 0 0 4096 Nov 24 22:10 bar 

我同样碰到这个问题; 我解决了我的数据容器内的chmod / chown,例如:

 FROM ubuntu # Create data directory RUN mkdir -p /data /var/lib/mysql RUN chmod -R 777 /data /var/lib/mysql RUN chown -R root:root /data /var/lib/mysql # Create /data volume VOLUME /data VOLUME /var/lib/mysql 

我相信这是有效的,因为UFS现在正确地logging了权限,而不是依靠从主机挂载直接执行,正如你发现的那样,在OS X上不起作用