Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql in docker container can't run through a mounted volume on os x

On OS X.

I am trying to run mysql in a docker container through boot2docker, by mounting the volume /var/lib/mysql on the host, so that I can have persistant mysql data. I plan to use a data only container in future, but right now I was trying to do this using this option.

I use the following command to run the container :

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

The /Users/yash/summers/db folder is already there.

I am facing persmission issues in this. Using commandline, I am able to access the directory, create/remove new files, but when I am running service mysql start, I get the following error:

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 

I have trying to solve this for the past 2 days, going through so many pages like this, this, and this.

I can't solve my problem. I think I am not able to perfectly do what the solutions are suggesting.

From my understanding, there are some workarounds listed on those pages which include changing the uids and guids, but I think they are not well explained.

Can anybody please explain to me a detailed workaround for this.

Update 1 : I tried the it with using a data only container also, and still facing the same issue.

I am able to run everything fine if I don't use any -v or --volumes-from option, so I think there is no prob in mysql server.

Update 2 : Dockerfile used for creating the data-only container:

FROM ubuntu

RUN mkdir /var/lib/mysql

VOLUME /var/lib/mysql
like image 526
Yash Avatar asked May 28 '15 15:05

Yash


1 Answers

There are two different solutions.

  1. Solution #1. With Dockerfile

    (I don't like it because I prefer official image from Docker Hub without any changes)

    Add RUN usermod -u 1000 mysql to your Docker file to set ID 1000 for user "mysql" ( the same ID like inside docker-machine).

  2. Solution #2. With my.cnf.

    Still I am using my own config I prefer this solution. We alredy have root user with ID 1000, because of that we can run MySQL with this user:

    • my.cnf

      Main row is user = root (you can use sed to change only this row in file. I prefer mount all file)

      [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/
      
    • Change default my.cnf with this file:

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

like image 195
ashatrov Avatar answered Sep 28 '22 03:09

ashatrov