dockered mariadb“[1045] 用户访问被拒绝” docker 网络

dockered mariadb "[1045] Access denied for user" inside docker network

我知道这个问题在 Whosebug 上有很多答案,但我没有找到适合我的案例的解决方案!

我将 Laravel 应用移至容器。

我可以连接到 DOCKER 网络外的 MariaDB 实例,但不能连接到网络内!

(我可以通过 MySQL Workbench 在本地连接(通过 docker exec),我可以从容器控制台在本地恢复转储并访问外部数据库数据)

怎么了?

为什么应用程序无法运行(PHP 无法通过内部 app_network 访问 mariadb)但同时我可以访问容器本身外部和内部的数据库???

OS: 美分OS 7.9.2009
Docker: 20.10.12 (e91ed57)
Docker-撰写:1.29.2 (5becea4c)

相同的配置在 Windows 10.

上工作正常

DOCKER COMPOSE 配置:

version: '3.9'

networks:
  app_network:
    driver: bridge
    name: ${NETWORK_NAME}

volumes:
  app:
    name: ${APP_VOLUME_NAME}
  mysql_database:
    name: ${MYSQL_DATABASE_VOLUME_NAME}
  mysql_dumps:
    name: ${MYSQL_DATABASE_DUMPS_VOLUME_NAME}

services:

  mariadb:
    image: mariadb
    env_file:
      - ./.env
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - ${MYSQL_EXTERNAL_PORT}:3306
    volumes:
      - mysql_database:/var/lib/mysql
      - mysql_dumps:/var/mysqldump
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    networks:
      app_network:
        aliases:
          - mariadb
    profiles:
      - dev
      - prod

  php:
    restart: always
    env_file:
      - ./.env
    build:
      context: ../../
      dockerfile: ./.environment/cs/php/Dockerfile
      args:
        - USER_ID=${PHP_USER_ID}
        - GROUP_ID=${PHP_GROUP_ID}
        - DEFAULT_CONFIG_FILE=${PHP_DEFAULT_CONFIG_FILE}
        - CUSTOM_CONFIG_FILE=${PHP_CUSTOM_CONFIG_FILE}
        - PROJECT_FOLDER=${PHP_PROJECT_FOLDER}
    volumes:
      - ./php/logs:/var/log
      - ../../:${PHP_PROJECT_FOLDER}
    networks:
      app_network:
        aliases:
          - php
    depends_on:
      - memcached
      - mariadb
    profiles:
      - dev
      - prod

  nginx:
    restart: always
    env_file:
      - ./.env
    build:
      context: ../../
      dockerfile: ./.environment/cs/nginx/Dockerfile
      args:
        - CONFIG_FILE=${WEB_CONFIG_FILE}
        - PROJECT_FOLDER=${WEB_PROJECT_FOLDER}
    ports:
      - ${WEB_EXTERNAL_PORT}:80
    volumes:
      - ./nginx/logs:/var/log/nginx
      - ../../public:${WEB_PROJECT_FOLDER}:cached
    networks:
      app_network:
        aliases:
          - nginx
    depends_on:
      - php
    profiles:
      - dev
      - prod

Docker.ENV

NETWORK_NAME=CS

APP_VOLUME_NAME=CS_APP_STORAGE

MYSQL_DATABASE_VOLUME_NAME=CS_DATABASE
MYSQL_DATABASE_DUMPS_VOLUME_NAME=CS_DATABASE_DUMPS
MYSQL_EXTERNAL_PORT=3317
MYSQL_ROOT_PASSWORD=root
MYSQL_USER=client
MYSQL_PASSWORD=client

PHP_USER_ID=1000
PHP_GROUP_ID=1000
PHP_DEFAULT_CONFIG_FILE=php.ini-production
PHP_CUSTOM_CONFIG_FILE=./.environment/cs/php/custom.prod.ini
PHP_PROJECT_FOLDER=/var/www/app

WEB_EXTERNAL_PORT=127.0.0.1:8091
WEB_CONFIG_FILE=./.environment/cs/nginx/nginx.dev.conf
WEB_PROJECT_FOLDER=/var/www/app/public

Laravel.ENV

DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306
DB_DATABASE=client
DB_USERNAME=client
DB_PASSWORD=client

尝试在 maria 数据库服务中添加公开配置键

mariadb:
 //...
  expose:
      - 3306

这是MariaDB容器的问题。我通过 MySQL Workbench 连接到 MariaDB,完全删除用户,创建一个新用户并授予方案权限。 之后一切正常。