php 如何正确连接mongo db和laravel sail?

pdtvr36n  于 2023-01-19  发布在  PHP
关注(0)|答案(1)|浏览(134)

我尝试让mongo和laravel sail一起工作,但是当我尝试调用migration时,我得到这个错误:

(我已经安装了jenssegers/mongodb和mongodb.so扩展)。

MongoDB\\Driver\\Exception\\ConnectionTimeoutException

No suitable servers found (`serverSelectionTryOnce` set): \[Failed to resolve 'mongo'\]

at vendor/mongodb/mongodb/src/functions.php:520
516▕         // TODO: PHPLIB-476: Read transaction read preference once PHPC-1439 is implemented
517▕         $readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
518▕     }
519▕
➜ 520▕     return $manager-\>selectServer($readPreference);
521▕ }
522▕
523▕ /\*\*
524▕  \* Performs server selection for an aggregate operation with a write stage. The

      +26 vendor frames 

27  artisan:37
Illuminate\\Foundation\\Console\\Kernel::handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
# My mongo env :
DB_CONNECTION=mongodb
DB_HOST=mongo
DB_PORT=27017
DB_DATABASE=example
DB_USERNAME=
DB_PASSWORD=
# My docker-compose.yml file:
version: '3'
services:
laravel.test:
build:
context: ./docker/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
\- 'host.docker.internal:host-gateway'
ports:
\- '${APP_PORT:-80}:80'
\- '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
\- '.:/var/www/html'
networks:
\- sail
depends_on:
\# - mysql
\- mongo

    # mysql:
    #     image: 'mysql/mysql-server:8.0'
    #     ports:
    #         - '8106:3306'
    #     environment:
    #         MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
    #         MYSQL_ROOT_HOST: "%"
    #         MYSQL_DATABASE: '${DB_DATABASE}'
    #         MYSQL_USER: '${DB_USERNAME}'
    #         MYSQL_PASSWORD: '${DB_PASSWORD}'
    #         MYSQL_ALLOW_EMPTY_PASSWORD: 1
    #     volumes:
    #         - 'sail-mysql:/var/lib/mysql'
    #         - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
    #     networks:
    #         - sail
    #     healthcheck:
    #         test: [ "CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}" ]
    #         retries: 3
    #         timeout: 5s
    
    mongo:
        image: mongo
        restart: always
        environment:
            MONGO_INITDB_ROOT_USERNAME: root
            MONGO_INITDB_ROOT_PASSWORD: example
    
    mongo-express:
        image: mongo-express
        restart: always
        ports:
            - 8081:8081
        environment:
            ME_CONFIG_MONGODB_ADMINUSERNAME: root
            ME_CONFIG_MONGODB_ADMINPASSWORD: example
            ME_CONFIG_MONGODB_URL: mongodb://localhost:27017/

networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
# My config/database.php(only mongo part):
'mongodb' =\> \[
'driver' =\> 'mongodb',
'host' =\> env('DB_HOST', '127.0.0.1'),
'port' =\> env('DB_PORT'),
'database' =\> env('DB_DATABASE'),
'username' =\> env('DB_USERNAME'),
'password' =\> env('DB_PASSWORD'),
'options' =\> \[
'database' =\> env('DB_AUTHENTICATION_DATABASE', 'admin'),
\],
\],

我尝试将env文件中的DB_HOST更改为localhost,127.0.0.1,但也不起作用。我不知道如何正确地将它们连接在一起。

huwehgph

huwehgph1#

我的工作正常(连接到Atlas,也许先试试这个?):
database.php

'mongodb' => [
            'driver' => 'mongodb',
            'dsn' => env('DB_DSN'),
            'database' => env('DB_DATABASE', 'homestead'),
    ],

在. env中,您需要
DB_DSN-这是您的连接字符串。
数据库_数据库

相关问题