mongodb 使用Docker在Mongo和Laravel之间没有连接

nfs0ujit  于 2023-11-17  发布在  Go
关注(0)|答案(2)|浏览(109)

我正在尝试将laravel项目与mongodb对接,但我得到了
找不到合适的服务器(serverselectiontryonce set):[连接到'127.0.0.1:27107'失败:连接拒绝在27017上调用inmaster]
这是我Docker编写的文件

version: '3.6'
services:
    mongodb:
        image: mongo:latest
        volumes:
          - ./data/db:/data/db
        ports:
          - 27017:27017

    web:
        build: .
        volumes:
          - ./tmp/db:/var/lib/mongodb/data
        ports:
          - 8000:8000
        links:
          - mongodb
        depends_on:
          - mongodb

字符串

mfpqipee

mfpqipee1#

将mongodb连接字符串从'127.0.0.1:27107'更改为'mongodb:27107',因为mongo在单独的容器中运行。可以使用链接的服务名称访问Mongodb示例。

vxbzzdmp

vxbzzdmp2#

不知道它是否会帮助你或没有,但你可以尝试这一次。我也面临着同样的问题,并没有得到任何解决方案从互联网上。然后我只是随便改变了这在我的.env文件和繁荣它为我工作。
.env(previously)

# Mongo DB Settings
MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=dev
MONGO_DB_USERNAME=root
MONGO_DB_PASSWORD=mongosupersecret
MONGO_DB_AUTHDB=admin

字符串
.env(已更改为)

# Mongo DB Settings
MONGO_DB_HOST=mongo //( put the value whatever written in your docker compose file )
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=dev
MONGO_DB_USERNAME=root
MONGO_DB_PASSWORD=mongosupersecret
MONGO_DB_AUTHDB=admin


Docker compose文件

version: '3.7'
services:
    laravel:
        ports:
            - "8000:80"
        volumes:
            - ./php/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
            - ./php/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
    database:
        ports:
            - "3307:3307"
    mongo:
        ports:
            - "27017:27017"
    mongo-express:
        ports:
            - "8001:8081"
    redis:
        ports:
            - "6379:6379"
    websocket:
        ports:
            - "3000:3000"
    minio:
        ports:
            - "9000:9000"
            - "9001:9001"
    adminer:
        image: adminer
        restart: always
        ports:
            - "8080:8080"

相关问题