Hello all,
I received a Authentication failed message with Laravel 5.2 when I ran my following controller. My MongoDB server don't require an authentication for a connection to any databases. How should I do to fix it?
Many thanks for your help,
My TestController controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class TestController extends Controller
{
public function test() {
return \App\Models\Test::all();
}
}
My config\database.php:
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'myproject'),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'options' => [
'db' => 'admin' // sets the authentication database required by mongo 3
]
],
My .env file:
DB_HOST=127.0.0.1
DB_DATABASE=myproject
DB_USERNAME=
DB_PASSWORD=
My Test model:
namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Test extends Eloquent
{
//protected $connection = 'myproject';
}
My environment:
I just had this same problem, solved it by remove username, password from the config array.
// 'username' => env('MONGO_USERNAME', ''),
// 'password' => env('MONGO_PASSWORD', ''),
Thanks @dkstar88, it's working now
Thanks @dkstar88 , it's working
Thanks @dkstar88 , i was having same issue
But if you have username and password for your mongodb then
there is an issue with this plugin,
You can find further details in this ticket:
https://github.com/jenssegers/laravel-mongodb/issues/772
Try changing your configuration to:
'username' => env('MONGO_USERNAME'),
'password' => env('MONGO_PASSWORD'),
hi @jenssegers
On my live system i don't have .env
and am using config as below
'username' => env('MONGO_USERNAME', 'somevalue'),
'password' => env('MONGO_PASSWORD', 'somepass'),
I have used following config and problem solved.
'options' => array(
'database' => 'admin' // sets the authentication database required by mongo 3
)
for latest extension use database instead of db in options array .
@jaffar-citytwig Thanks!!!!
Upgrade mongo driver dependence mongodb/mongodb 1.0.3 to 1.0.4 and update database.php
```php
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => env('DB_DATABASE') // sets the authentication database required by mongo 3
]
],
I have config like this that didn't work.
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'database' => env('DB_DATABASE'),
'options' => [
'database' => env('DB_DATABASE') // sets the authentication database required by mongo 3
]
],
After changing it like this
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => env('DB_DATABASE') // sets the authentication database required by mongo 3
]
],
It worked
i have same issue. But in app/Console/Commands. I can't query to database when set pasword in config/database: 'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE','crm_manage'),
'username' => env('DB_USERNAME','admin'),
'password' => env('DB_PASSWORD','Vmo__2017##'),
'options' => [
'database' => 'admin' // sets the authentication database required by mongo 3
]
],
When i run command, In Aggregate.php line 219:
Authentication failed.
Help me!
@changha8888 change the database option to the current base name
'options' => [
'database' => 'crm_manage' // sets the authentication database required by mongo 3
]
@uriel2707 I trying follow you. But still not run. :(
@changha8888 You can provide more information about the configuration you specify.
the configuration of mongodb that you use, php version, driver version, laravel version, as well as copy of the .env file
@uriel2707 I use php 7.2.0. mongodb": "^3.2". Laravel 5.4.
my .env: PP_NAME=IncallSystem
APP_ENV=local
APP_KEY=base64:qR7DYILfoRz0mgUWUGOEcnR9dSmScrtmfXClrEiunhE=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
If i run project in local, everything's fine. When i deploy to server that use username, password.
I try debug in app/Console/Commands :
$test = ScheduleSms::count();
print_r($test);die;
In local will return true, In serve ( has username, password ) return Authentication failed.
hi @jenssegers i have faced issued plz solve my issue i configure my database like that
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE','rfg'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'password'),
'options' => [
'database' => 'admin' // sets the authentication database required by mongo 3
]
],
and env file are that
DB_CONNECTION=mongodb
DB_HOST=mongodb
DB_PORT=27017
DB_DATABASE=rfg
DB_USERNAME=root
DB_PASSWORD=password
but if i try to register the user and store data in mongodb but i show thats error how i can solve that issue plz help me
[2018-02-14 07:46:19] local.ERROR: Authentication failed. {"exception":"[object] (MongoDB\Driver\Exception\AuthenticationException(code: 11): Authentication failed. at /srv/laravel/vendor/mongodb/mongodb/src/Operation/InsertOne.php:106)
[stacktrace]
@changha8888
I recommend trying the configuration in server through a program, studio 3T is an option. Make sure that the user is correctly linked to the database.




@jenssegers @cjmling plz some1 help me i succesfully register user in my mongodb but if i want to login user it cannot be login show me error
{"message":"Type error: Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of App\User given, called in \/srv\/laravel\/vendor\/tymon\/jwt-auth\/src\/JWTAuth.php on line 54","status_code":500}
my login code are here
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use JWTAuth;
use App\User;
use JWTAuthException;
use DB;
class UserController extends Controller
{
private $user;
public function __construct(User $user){
$this->user = $user;
}
public function register(Request $request){
$user = $this->user->create([
'firstname' => $request->get('firstname'),
'lastname' => $request->get('lastname'),
'email' => $request->get('email'),
'password' => bcrypt($request->get('password'))
]);
return response()->json(['status'=>true,'message'=>'User created successfully','data'=>$user]);
}
public function login(Request $request){
$credentials = $request->only('email', 'password');
$token = null;
try {
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json(['invalid_email_or_password'], 422);
}
} catch (JWTAuthException $e) {
return response()->json(['failed_to_create_token'], 500);
}
return response()->json(compact('token'));
}
public function getAuthUser(Request $request){
$user = JWTAuth::toUser($request->token);
return response()->json(['result' => $user]);
}
}
plz some1 con help what ican do
'options' => [
'authSource' => 'admin',
]
thank you @uriel2707
I have used following config and problem solved.
'options' => array(
'database' => 'admin' // sets the authentication database required by mongo 3
)
for latest extension use database instead of db in options array .
Thank you, that's work for me!
I have config like this that didn't work.
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'database' => env('DB_DATABASE'),
'options' => [
'database' => env('DB_DATABASE') // sets the authentication database required by mongo 3
]
],After changing it like this
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => env('DB_DATABASE') // sets the authentication database required by mongo 3
]
],It worked
Thanks!!!!
'options' => [
'authSource' => 'admin',
]
Its working, thankyou soo muchh
Its works
Upgrade mongo driver dependence mongodb/mongodb 1.0.3 to 1.0.4 and update
database.php'mongodb' => [ 'driver' => 'mongodb', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', 27017), 'database' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'password' => env('DB_PASSWORD'), 'options' => [ 'database' => env('DB_DATABASE') // sets the authentication database required by mongo 3 ] ],
You saved me with options params.
how if using mongodb 4.0.12 ?
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'databaseName' => env('DB_DATABASE')
]
],
it worked for me !
Most helpful comment
I just had this same problem, solved it by remove username, password from the config array.