微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将MongoDB连接到项目-错误

如何解决将MongoDB连接到项目-错误

我正在尝试使用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_USERNAME DB_PASSWORD .env 文件中,在 database.PHP 文件中,我在”之间留了一个空格,您必须在其中输入用户名密码,我开始连接。

现在的问题是,当我想输入 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。