Search code examples
phpmongodblaravel-5laravel-5.4jenssegers-mongodb

Laravel switch mysql to mongodb


After completed my project I deiced switch mysql to mongodb. I am using Laravel 5.4 version and for mongodb use jenssegers. For laravel mongdb installation I follow jenssegers installation documentations and installation successfully completed. But after installation I go for migration and use command php artisan migrate. When I use this command shown error InvalidArgumentException in /vendor/mongodb/mongodb/src/Database.php line 83: $databaseName is invalid:

My database.php

'default' => env('DB_CONNECTION', 'mongodb'),

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
    ],

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

    'pgsql' => [
        'driver' => 'pgsql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'schema' => 'public',
        'sslmode' => 'prefer',
    ],
    'mongodb' => [
        'driver'   => 'mongodb',
        'host'     => env('DB_HOST', 'localhost'),
        'port'     => env('DB_PORT', 27017),
        'database' => env('DB_DATABASE', 'project_db'),
        'username' => env('DB_USERNAME'),
        'password' => env('DB_PASSWORD'),
        'options'  => [
            'database' => 'admin' // sets the authentication database required by mongo 3
        ]
    ],

],

My .env

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

And I also facing one more problem if whatever I change in .env and database.php like if I change port 27017 to 3303 but on browser shows 27017. I cleared cache and all. I don't know what is problem in my code.


Solution

  • These are the steps Envoyer (made by Laravel's creator) takes to deploy an app on production -- I've annotated them below:

    # Clear the old boostrap/cache/compiled.php
    php artisan clear-compiled
    
    # Recreate boostrap/cache/compiled.php
    php artisan optimize
    
    # Migrate any database changes
    php artisan migrate