2
votes

While trying to run MongoDB 4.0 with laravel I am getting the following error

PHP Fatal error: Uncaught MongoDB\Driver\Exception\InvalidArgumentException: Failed to parse MongoDB URI: 'mongodb://'. Invalid host string in URI. in /var/www/html/valueableapp/valueable_rest_app/api_src/vendor/mongodb/mongodb/src/Client.php:87`

I used the command mongo to run the mongo shell and it's working fine. also sudo service mongod status and its also showing the status running. I am not able to start my project even anyone please. here is the complete stack trace of the issues I am getting php artisan serve --port=8080

Laravel development server started: http://127.0.0.1:8080 [Wed May 22 10:00:54 2019] PHP Fatal error: Uncaught MongoDB\Driver\Exception\InvalidArgumentException: Failed to parse MongoDB URI: 'mongodb://'. Invalid host string in URI. in /var/www/html/valueable-app/valueable_rest_app/api_src/vendor/mongodb/mongodb/src/Client.php:87 Stack trace: #0 /var/www/html/valueable-app/valueable_rest_app/api_src/vendor/mongodb/mongodb/src/Client.php(87): MongoDB\Driver\Manager->__construct('mongodb://', Array, Array) #1 /var/www/html/valueable-app/valueable_rest_app/api_src/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Connection.php(141): MongoDB\Client->__construct('mongodb://', Array, Array) #2 /var/www/html/valueable-app/valueable_rest_app/api_src/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Connection.php(41): Jenssegers\Mongodb\Connection->createConnection('mongodb://', Array, Array) #3 /var/www/html/valueable-app/valueable_rest_app/api_src/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/MongodbServiceProvider.php(30): Jenssegers\Mongodb\Connection->__construct(Array) #4 [internal function]: Jenssegers\Mongo in /var/www/html/valueable-app/valueable_rest_app/api_src/vendor/mongodb/mongodb/src/Client.php on line 87

Config/database.php

<?php

return [
    'fetch' => PDO::FETCH_CLASS,
    'default'=>'mongodb',
    'connections' => array(

        'mongodb' => array(
            'driver'   => 'mongodb',
            'host'     => env('DB_HOST'),
            'port'     => env('DB_PORT'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'database' => env('DB_DATABASE'),
            'options' => [
                'database' => env('DB_DATABASE')
            ]
        ),

    ),
    'migrations' => 'migrations',
    'redis' => [

        'cluster' => false,

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],

];

.env

APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:q0DOPnhqUzhmMrqryyyN6SDOi6NHKh/BbmJGHw35e3E=

DB_CONNECTION=mongodb
DB_HOST=localhost
DB_PORT=27017
DB_DATABASE=abc
DB_USERNAME=
DB_PASSWORD=



CACHE_DRIVER=array
SESSION_DRIVER=file
QUEUE_DRIVER=sync

CRITERIA_MAX_SCORE=5
CRITERIA_MIN_SCORE=1
MAX_SCORE_APP=1000

Thanks in advance. I have look for the solution on this site
Laravel 5.2 Mongo MonogDB Failed to parse MongoDB URI but this question is not having answer.

2
It's hard to help you without seeing your config files (like relevant sections of config/database.php or .env). Can you post them and edit your question? - Leonardo Rossi
I have added those - Adesh Kumar

2 Answers

1
votes

In my case, I just resolved it but changing the name of the file from .env to .env.local. Thanks.

1
votes

I had this error because of a wrong port number. Kindly confirm first