使用数据量的MariaDB Docker容器-错误2002,无法通过套接字连接

时间:2019-01-16 10:42:45

标签: docker mariadb docker-volume

我已经将MariaDB安装在Docker容器中,并且工作正常。我想将数据目录移动到外部卷,以将数据库与容器生命周期分开。我遵循了here的说明,但是当我现在尝试连接到引擎时,我得到了:

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

“ / var / www /”是外部卷的安装点。我想知道这是否是权限,因此我将MariaDB容器中的数据库文件夹的所有者设置为“ myqsl”,即MySQL帐户的名称。

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

注意:在主机文件系统中,所有者列出为999:docker。这有关系吗?

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

我想让我将数据库存储在该卷中的原因是什么?

这是我的/etc/MySQL/my.cnf文件(定义各种文件夹的位)的部分转储:

[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:

这是我的yml文件的内容。我是这个东西的初学者,所以可能充满了空洞。请客气。 :)

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 个答案:

答案 0 :(得分:0)

您是否尝试过在docker-compose中安装卷/ var / www / databases? 在您链接的示例之后,我建议:

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

要考虑的另一件事是网络问题。在这种情况下,我应该尝试在所有人之间共享docker网络,并将network_mode: host添加到您要在他们之间进行通信的每个服务中。

最后,如果这些都不起作用,我尝试尝试的另一件事是尝试在主机中安装MariaDB并将文件/var/www/databases/mysqld/mysqld.sock挂载为卷。我不建议这样做,也不是很优雅,此外,我的目标是将数据库放置在具有外部持久性数据的docker中。

答案 1 :(得分:0)

谢谢大家的建议。最后,我发现了一些在这所房子里的支持。

诀窍是将默认数据库文件夹安装为外部卷:

volumes:
  - databases:/var/lib/mysql

大多数情况下都是即时运行的。唯一的警告是我的撰写文件定义了MySQL根凭据。当安装挂起时,这些被覆盖,因此我必须在第一次启动容器时手动重置根密码。其实这个现在应该是固定的,因为数据目录现在是外部体积,所以一个新的形象应该拿起相同的root口令。应该能够脚本此更改安装文件。