mysql 如何使用docker-compose从主机连接到mariadb

vfh0ocws  于 2023-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(221)

我有一个docker-compose文件:

version: "3"

services:
  mariadb:
    image: mariadb:latest
    container_name: mariadb
    command: --default-authentication-plugin=mysql_native_password
    restart: unless-stopped
    env_file: .env
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - internal

  drupal:
    image: drupal:9.3.9-fpm-alpine
    container_name: drupal
    depends_on:
      - mariadb
    restart: unless-stopped
    networks:
      - internal
      - external
    volumes:
      - /var/www/html/:/var/www/html/

  webserver:
    image: nginx:latest
    container_name: webserver
    depends_on:
      - drupal
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/www/html/:/var/www/html/
      - ./nginx-conf:/etc/nginx/conf.d
    networks:
      - external

networks:
  external:
    driver: bridge
  internal:
   driver: bridge

volumes:
  db-data:

我运行docker-compose up -d和一切工作正常,但我不能从主机访问mariadb服务器,我尝试了这些

# mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

# mysql -uroot -p -h mariadb
Enter password: 
ERROR 2005 (HY000): Unknown MySQL server host 'mariadb' (-2)

# mysql -uroot -p -h 127.0.0.1
Enter password: 
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)

编辑:.env文件:

MYSQL_ROOT_PASSWORD=abc1234
MYSQL_DATABASE=my_db
MYSQL_USER=drupal
MYSQL_PASSWORD=cdf1234
thigvfpy

thigvfpy1#

MariaDB Docker Library documentation;
需要MARIADB_ROOT_PASSWORD、MARIADB_ALLOW_EMPTY_ROOT_PASSWORD或MARIADB_RANDOM_ROOT_PASSWORD(或等效项,包括 *_FILE)之一。其他环境变量是可选的。
这是用于db-data卷的初始化。初始化后,这些环境变量是可选的。

xzv2uavs

xzv2uavs2#

mysql端口尚未打开/从主机转发端口到mariadb容器:

version: "3"
    
    services:
      mariadb:
...
        networks:
          - internal
        ports:
          - "3306:3306"
    
      drupal:
...

相关问题