将 MongoDB 连接到项目 - 错误

Connecting MongoDB to the project - error

我正在尝试使用 Docker 将 MongoDB 数据库连接到我在 Laravel 7.0 中的项目。我已遵循此指南

LINK

不幸的是,并非一切都按计划进行。我无法 运行 命令

php artisan migrate

因为发生错误

   MongoDB\Driver\Exception\ConnectionTimeoutException 

  No suitable servers found (`serverSelectionTryOnce` set): [Invalid reply from server. calling ismaster on '127.0.0.1:50003']

  at vendor/mongodb/mongodb/src/functions.php:431
    427|         // TODO: PHPLIB-476: Read transaction read preference once PHPC-1439 is implemented
    428|         $readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
    429|     }
    430| 
  > 431|     return $manager->selectServer($readPreference);
    432| }
    433| 

      +24 vendor frames 
  25  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

使用网站上的注册或登录选项尝试连接数据库时出现同样的错误。

我的 .env 文件

DB_CONNECTION=mongodb
DB_HOST=localhost
DB_PORT=50003
DB_DATABASE=dbname
DB_USERNAME=username
DB_PASSWORD=password

我的database.php文件

'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('DB_HOST', 'localhost'),
            'port' => 50003,
            'database' => env('DB_DATABASE', 'dbname'),
            'username' => env('DB_USERNAME', 'username'),
            'password' => env('DB_PASSWORD', 'password'),
            'options' => [
                // here you can pass more settings to the Mongo Driver Manager
                // see https://www.php.net/manual/en/mongodb-driver-manager.construct.php under "Uri Options" for a list of complete parameters that you can use
        
                'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // required with Mongo 3+
            ],
        ],

我的User.php文件

namespace App;

use Illuminate\Notifications\Notifiable;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract
{
    use AuthenticableTrait;
    use Notifiable;
    use CanResetPassword;

    protected $connection = 'mongodb';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];
...

我的docker-compose.yml文件

version: '3.1'

services:
  mongo:
    image: mongo
    restart: always
    container_name: Mongo_DB
    environment:
      MONGO_INITDB_ROOT_USERNAME: username
      MONGO_INITDB_ROOT_PASSWORD: password
    ports:
      - '27018:27017'

  mongo-express:
    image: mongo-express
    restart: always
    container_name: Mongo_Express
    ports:
      - 50003:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: username
      ME_CONFIG_MONGODB_ADMINPASSWORD: password

编辑:当我将 .env 和 database.php 文件中的端口更改为 27017 并删除 DB_USERNAMEDB_PASSWORD.env 文件中,在 database.php 文件我在你必须输入 用户名 密码 [=] 之间留了一个空白 space =61=],我开始连接了。

现在的问题是,当我想输入 127.0.0.1: 27017 我有这个消息

It looks like you are trying to access MongoDB over HTTP on the native driver port.

怎么了?

答案:

端口 .env & database.php 必须是 27018 而不是 50003