Configuration error with MongoDB laravel - php

I am planning to use MongoDb along with my Laravel project. I am using the jensseger/mongodb extension for it.
I have configured everything. I am getting an error saying mongodb not configured.
This is how my database.php file looks like:
'connections' => [
# Our primary database connection
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],

Double check your configuration in config/database.php
Also make sure that you fire php artisan config:cache after doing the changes.

Be sure to have DB_CONNECTION=mongodb set in your .env file or just replace 'default' => env('DB_CONNECTION', 'mysql') with 'default' => 'mongodb'. I would go with the former, as it's safer to rely only on the .env, rather than hardcoding your default database connection.

'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', 'localhost'),
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => ['database' => 'Database_Name']
],
After clear cache "php artisan config:cache"

go to .env
Add these lines:
MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=your database name
MONGO_DB_USERNAME=your database username if you have
MONGO_DB_PASSWORD=your database password if you have

Related

Connection of Laravel project ( hosted: 127.15.255.64) To pgsql (hosted: 127.15.255.13)

I am working on a laravel project and we use 2 different servers to host the code( hosted: 127.15.255.64) and the database(hosted: 127.15.255.13).
I modified the .env file as:
DB_CONNECTION=pgsql
DB_HOST=127.15.255.13
DB_PORT=5432
DB_DATABASE=db
DB_USERNAME=postgres
DB_PASSWORD=postgres
Also Modified database.php file as:
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.15.255.13'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'db'),
'username' => env('DB_USERNAME', 'postgres'),
'password' => env('DB_PASSWORD', 'postgres'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'search_path' => 'public',
'sslmode' => 'prefer',
],
and my servers php.ini file does not have any pgsql extension to uncomment so i tried running the php artisan migrate to check if the db is connected but it just didn't do anything.
I am stuck and dont know what to do next i tried calling "DB::table('public.db')->get()" where 'public' is schema and 'db' is databse name, But it gave me an error which showed could not find driver, can anybody please help me with this situation. i dont know what top do next.

Error while creating record in laravel - mongo Application

I am using a package for mongodb in laravel : "jenssegers/mongodb": "^3.8",
migrations are working fine, but when I try to create a record using controller, its throwing error
InvalidArgumentException
Database connection [mongoproject] not configured.
my database.php
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', 'localhost'),
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
my .env
DB_CONNECTION=mongodb
MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=mongoproject
MONGO_DB_USERNAME=
MONGO_DB_PASSWORD=

artisan db:seed --database not working

I want to seed some data to my database. So far, it all works well with the "normal" database.
Now i want to seed the same data (the same seeder) to another database running. So i try to use the --database= command
php artisan db:seed --database=cb4test --class=TestDataTableSeeder -v
But this will seed the data into the "normal" database, not in the one specified in cb4test. What am i doing wrong?
More info:
This a part of my config/database.php:
'connections' => [
'couchbase' => [
'driver' => 'couchbase',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 8091),
'bucket' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'n1ql_hosts' => [
'http://'.env('DB_HOST', 'localhost').':8093'
]
],
'cb4test' => [
'driver' => 'couchbase',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 8091),
'bucket' => env('DB_TEST', 'msg4test'),
'username' => env('DB_USERNAME'),
'n1ql_hosts' => [
'http://'.env('DB_HOST', 'localhost').':8093'
]
],
In my .env i do have an entry for DB_TEST pointing to the correct couchbase-bucket (in mysql that would be a mysql-table).
Still, this is not working.
I made a debug line in my TestDataTableSeeder.php that gives my the current used DB-Table and this is the one used in the default-connection (named couchbase), not the one used in connection 'cb4test'.
Using Laravel 5.4
Its common misconception, --database specifies the database connection not the name.
So in your config\database.php file you need to add up a connection which uses this different database like:
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE2', 'forge'),
'username' => env('DB_USERNAME2', 'forge'),
'password' => env('DB_PASSWORD2', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => 'InnoDB',
],
Then in your .env, define three variables for your other db:
DB_DATABASE2 = cb4test
DB_USERNAME2 = //username for cb4test
DB_PASSWORD2 = //password for cb4test
Then use:
php artisan db:seed --database=mysql2 --class=TestDataTableSeeder -v
OR
Better way of changing database is to change it in seeder itself.
In your: TestDataTableSeeder.php
use Illuminate\Support\Facades\DB;//at top
//then inside function:
DB::disconnect('mysql');
config(['database.connections.mysql.database' => env('DB_DATABASE2','')]);
config(['database.connections.mysql.username' => env('DB_USERNAME','')]);
config(['database.connections.mysql.password' => env('DB_PASSWORD2','')]);
Of course you will need those three variables of .env file intact.
I hope it helps
So, we figured out that the source of this problem was related to a bug in the couchbase driver.
Bug fixed, all works well.
Thanks for all the answers trying to help me out :D

Configure Database Connections Automatically base on Environment

I know this is so wrong, but I've tried google for this kind of task, and I found nothing.
Goal
I'm trying to make an if check, base on my Laravel environment, I will set the database connection appropriately between my local environment and my Heroku production environment.
connections' => [
'mysql' => [
'driver' => 'mysql',
if(env('APP_ENV') == 'local'){
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'unix_socket' => env('UNIX_SOCKET'),
}else{
'host' => parse_url(getenv("DATABASE_URL"))["host"],
'database' => substr(parse_url(getenv("DATABASE_URL"))["path"], 1),
'username' => parse_url(getenv("DATABASE_URL"))["user"],
'password' => parse_url(getenv("DATABASE_URL"))["pass"],
}
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
],
you can define another connection, named for exmaple mysql-heroku, and then in your models you can do protected $connection = (env('APP_ENV') == 'local') ? 'mysql' : 'mysql-heroku';.
Sounds like the Cascading config package would work great for you. Laravel 4 had something very similar to that package.
You can have a config.local folder and config.production folder for your Heroku environment.
Example folder configuration
config/ (production)
├── database.php
config.local/ (local)
├── database.php
config/database.php (production)
...
connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => 'Heroku Host',
'database' => 'Heroku Database',
'username' => 'Heroku Username',
'password' => 'Heroku Password',
'unix_socket' => 'Heroku Socket',
],
],
...
config.local/database.php (local)
...
connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
],
],
...

Laravel : random error : SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected

I'm getting this error randomly when using my laravel application:
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
If I refresh the browser, I'm not getting this error. It's really weird because it's working correctly before and after.
My database credentials are stored in .env file
EDIT
Here the content of my file :
APP_ENV=local
APP_DEBUG=true
APP_KEY=6cYKHzpblHGfE3H0n6j2tSRjoqJsgcqd
DB_HOST=localhost
DB_DATABASE=gestion-inscriptions-v2
DB_USERNAME=root
DB_PASSWORD=
EDIT 2
And here the database.php : http://laravel.io/bin/bE95X
EDIT 3
Still got this error, even on other computer (working with wamp)
Thanks in advance
PS : I know there is already the same questions on SO but there is no correct answer that works for me so ...
According config/database.php and .env file that You show us everything is ok.
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
this means that database is not selected,
seems like have problem with existence of database,
or DB_NAME does not get in config database param.
There is some steps You've to check:
1) Check Your config/database.php file contents to have such content:
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_ACADEMY'),
'database' => env('DB_DATABASE_ACADEMY'),
'username' => env('DB_USERNAME_ACADEMY'),
'password' => env('DB_PASSWORD_ACADEMY'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false
],
'mysql3' => [
'driver' => 'mysql',
'host' => env('DB_HOST_ACADEMY'),
'database' => env('DB_DATABASE_ACADEMY_DATE'),
'username' => env('DB_USERNAME_ACADEMY'),
'password' => env('DB_PASSWORD_ACADEMY'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false
]
],
'migrations' => 'migrations',
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0
]
]
];
2) Check Your .env file exist in Your app root and it's not .env.example
3) Check .env file to have this line, if Yes so delete and copy-paste it from here (maybe spelling issue):
DB_DATABASE=gestion-inscriptions-v2
p.s. because of in Your question files are correct need more debug info, screenshot and etc. to see that problem is not with .env or file system (maybe .env file cannot be read).
Just now i see that problem, and problem was in not saved .env file (was used VS code)

Categories