skip to Main Content

I’ve installed MariaDB in a Docker container and it worked just fine. I wanted to move the data directory to an external volume to separate the databases from the container lifecycle. I followed the instructions here, but when I now try to connect to the engine I get:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/www/databases/mysqld/mysqld.sock' (2)

“/var/www/” is the mount point for the external volume. I wondered if it was a permissions thing, so I set the owner of the databases folder in the MariaDB container to be ‘myqsl’, the name of the MySQL account.

drwxr-xr-x 3 mysql mysql 4096 Jan 15 16:38 databases
drwxr-xr-x 3  1000  1000 4096 Jan 15 13:59 files
drwxr-xr-x 2  1000  1000 4096 Jan 15 15:20 html
drwxr-xr-x 3  1000  1000 4096 Jan 15 13:59 src

NB: in the host file system the owner is listed as 999:docker. Does this matter??

drwxr-xr-x 3       999 docker    4096 Jan 15 16:38 databases

WHat am I missing to allow me to store my databases in the volume?

Here is a partial dump of my /etc/MySQL/my.cnf file (the bit that defines various folders):

[client]
port            = 3306
#socket         = /var/run/mysqld/mysqld.sock
socket          = /var/www/databases/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
#socket         = /var/run/mysqld/mysqld.sock
socket          = /var/www/databases/mysqld/mysqld.sock
nice            = 0

[mysqld]
#
# * Basic Settings
#
#user           = mysql
pid-file        = /var/run/mysqld/mysqld.pid
#socket         = /var/run/mysqld/mysqld.sock
socket          = /var/www/databases/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
#datadir                = /var/lib/mysql
datadir         = /var/www/databases
tmpdir          = /tmp
lc_messages_dir = /usr/share/mysql
lc_messages     = en_US
skip-external-locking

docker-compose.yml:

Here is the contents of my yml file. I’m a beginner with this stuff, so it’s probably full of holes. Please be kind. 🙂

version: '3.2'
volumes:
  mwApache2Data:
    external: true
  webContent:
    external: true

networks:
  frontend:
  backend:

services:
  php:
    build: './php7.1/'
    image: php7.1.26-fpm:1.0
    restart: always
    container_name: php7.1.26-fpm
    networks:
      - backend
#    volumes:
#      - mwApache2Data:/app
  web:
    build: ./apache2/
    image: apache2:1.0
    restart: always
    container_name: AOW_apache2Server
    depends_on:
      - php
      - mariadb
    networks:
      - frontend
      - backend
    expose:
     - "80"
     - "81"
     - "443"
     - "8083"
    ports:
     - "80:80"
     - "81:81"
     - "443:443"
     - "8083:8083"
    volumes:
      - mwApache2Data:/app
      - webContent:/var/www
  mariadb:
    build: ./mariaDB/
    image: mariadb_10.4.0
    container_name: mariaDB_10.4.0
    restart: always
    networks:
      - backend
    environment:
      - MYSQL_DATABASE=lg_wiki_db
      - MYSQL_USER=wikiuser
      - MYSQL_PASSWORD=****
      - MYSQL_ROOT_PASSWORD=****
    volumes:
      - webContent:/var/www
  mediawiki:
    image: mediawiki:1.31.1
    container_name: mediawiki_1.31.1
    restart: always
    depends_on:
      - web
    networks:
      - backend
#    ports:
 #     - 8080:80
    links:
      - mariadb
    volumes:
      - webContent:/var/www/
      # After initial setup, download LocalSettings.php to the same directory as
      # this yaml and uncomment the following line and use compose to restart
      # the mediawiki service
      # - ./LocalSettings.php:/var/www/html/LocalSettings.php
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    restart: always
    depends_on:
      - web
    expose:
      - 80
    networks:
      - frontend
      - backend
    ports:
      - 8080:80
    links:
      - mariadb

2

Answers


  1. Chosen as BEST ANSWER

    Thank you everyone for your advice. In the end I found some in house support on this.

    The trick was to mount default database folder as an external volume:

    volumes:
      - databases:/var/lib/mysql
    

    This mostly worked off-the-bat. The only caveat was that my compose file defined the MySQL root credentials. These were overwritten when the mount took hold so I had to manually reset the root password the first time the container was spun-up. Actually this should now be fixed since the data directory is now external to the volume, so a new image should pick up the same root password. Should be able to script this change into the setup files.


  2. Have you tried mounting volume /var/www/databases in your docker-compose?
    Following with the example you’ve linked, I should suggest:

      ...
      mysql:
        image: mysql:5.6.40
        networks:
          - backend
        environment:
          - MYSQL_ROOT_PASSWORD=rootpassword
        volumes:
          - ./your_persistent_db_data_dir:/var/www/databases
    

    Another thing to be considered is a network problem. In that case, I should try sharing docker network between all of them adding network_mode: host to every single service you want to communicate among them.here`

    Finally, another thing that I’d try if nothing of these are helping you, is trying to install MariaDB in your host and mount file /var/www/databases/mysqld/mysqld.sock as a volume. This is not the recommended one, is not very elegant and furthermore, the target is to have DB in a docker with persistent data outside, I know.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search