Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication Error #21

Closed
abhishekgahlot opened this issue Aug 16, 2013 · 23 comments
Closed

Authentication Error #21

abhishekgahlot opened this issue Aug 16, 2013 · 23 comments

Comments

@abhishekgahlot
Copy link

Hi

I am using different port and username|password . I get error
Failed to connect to: localhost:32759: Authentication failed
I am sure my username and password are correct . It connects fine via simple php mongoClient class.
Don't know why this happening.

After i reviewed source , it seems ok it should be passing password fine but its not connecting in laravel 4.

@jenssegers
Copy link
Contributor

Can you show me what connection string you used for the MongoClient class?

@abhishekgahlot
Copy link
Author

Its

'mongodb' => array(
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 32759,
'username' => 'darky',
'password' => 'password',
'database' => 'database',
)

I tried another mongoDB package which is lmongo, That giving same error too.

@jenssegers
Copy link
Contributor

Can you show me the one you used for PHP's MongoClient one?

Your configuration should generate:

mongodb://darky:password@localhost:32759/database

@abhishekgahlot
Copy link
Author

class DbConnection{

static protected $_instance;

protected $db = null;

final protected function __construct()
{
    $m = new MongoClient("mongodb://darky:password@localhost:32759");
    $this->db = $m->selectDB( "database" );
}

static public function getInstance() {
    if (!(self::$_instance instanceof self))
    {
        self::$_instance = new self();
    }
    return self::$_instance;
}

public function getConnection()
{
    return $this->db;
}

final protected function __clone() { }

}

Its simple database connection class that i generally used to connect to mongo.

@abhishekgahlot
Copy link
Author

Yes, its fine.

but here are few things i found

$m = new MongoClient("mongodb://darky:password@localhost:32759/database");
$this->db = $m;

If i use this in my php code, I get same error auth fails. I think i need to setup database name afterwards.

jenssegers added a commit that referenced this issue Aug 16, 2013
@jenssegers
Copy link
Contributor

I modified the DSN, can you try again now?

@abhishekgahlot
Copy link
Author

Thanks, I just did with

tweaking function getDsn and changed this line

return "mongodb://{$credentials}" . implode(',', $hosts) . "/{$database}";

return "mongodb://{$credentials}" . implode(',', $hosts);

Because database is already selected in constructor like this
// Select database
$this->db = $this->connection->{$config['database']};

@abhishekgahlot
Copy link
Author

Also can you tell, is this repository https://github.com/navruzm/lmongo
similar to this one.

@jenssegers
Copy link
Contributor

We both tackle the same problem in different ways, he creates a completely new eloquent/builder interface and I maintain the original eloquent/builder interface.

I wanted to be able to switch an existing project from SQL to MongoDB without having to change anything. If you switch to MongoDB with lmongo, you need to completely rewrite your existing code. Even simple things like where('age', '>', 30) are not possible with lmongo.

@abhishekgahlot
Copy link
Author

Thanks for telling and yeah your repository looks like i am using mysql way to connect to mongo. Everything fine now. :)

@jenssegers
Copy link
Contributor

From what I read on http://docs.mongodb.org/manual/reference/connection-string/ I might have to revert the last commit for some users. If you don't specify a database in the connection string it authenticates to the admin database.

Are you sure you have correctly set up permissions etc?

@abhishekgahlot
Copy link
Author

I am not sure , But let me try with mongodb setup on cloud server ie mongohq.com
Yes there could be problem with permissions.

jenssegers added a commit that referenced this issue Aug 16, 2013
@jenssegers
Copy link
Contributor

I was unable to connect to a mongolab server because I had no permissions on the admin database. I reverted the changes.

Failed asserting that exception of type "MongoConnectionException" matches expected exception "Illuminate\Database\Eloquent\ModelNotFoundException". Message was: "Failed to connect to: abc.mongolab.com:39058: Authentication failed on database 'admin' with username 'username': auth fails".

I think your problem is related to incorrect permissions.

@abhishekgahlot
Copy link
Author

Yes you are right, You will have to revert that commit , for users who have separate username and password for different databases.

@joeswann
Copy link

Not sure if this is still relevant or even related but I had the same OP problem and solved it by using

db.addUser()

after

use laravel_database;

as per the first block on http://docs.mongodb.org/manual/tutorial/add-user-to-database/

@a-ghasemi
Copy link

I have same problem on Laravel MongoDB, I'm using Laravel 4.2, Mongo 3.2, PHP 5.6
I can migrate and seed with artisan, but in php code when calling
DB::table('users')->select();
with catching errors, it shows:

Error File: /var/www/laravel/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Connection.php
**Error Line: ** 133
Error Message: Failed to connect to: localhost:27017: Permission denied
Error Code: 71

            'mongodb' => array(
                'driver' => 'mongodb',
                'host' => 'localhost',
                'port' => 27017,
                'username' => 'laravel_user',
                'password' => '********',
                'database' => 'laravel_db'
            ),

@a-ghasemi
Copy link

@rkang30
Copy link

rkang30 commented Mar 18, 2016

I have an authentication failed issue with mongodb 3.2 and laravel 5.2

I have tested it with just php and mongodb php library like below.

require 'vendor/autoload.php';
$uripotions = array();
$uripotions['username'] = 'myuser';
$uripotions['password'] = 'mypassword';
$client = new MongoDB\Client("mongodb://localhost:27017", $uripotions);
$collection = $client->client_db->accounts;

$result = $collection->find();
foreach($result as $item){
echo $item['first_name'];
echo '

';
}

And it worked.

However, it doesn't work on laravel 5.2 after following the steps that jenssegers/laravel-mongodb mentions.

I have added the service provider class to config/app.php and added my mongodb config in config/database.php like below.

    'mongodb' => [
        'driver'   => 'mongodb',
        'host'     => env('DB_HOST', 'localhost'),
        'port'     => env('DB_PORT', 27017),
        'database' => env('DB_DATABASE', 'client_db'),
        'username' => env('DB_USERNAME', 'myuser'),
        'password' => env('DB_PASSWORD', 'mypassword'),
        'options' => [
            'db' => 'admin'
        ]
    ],   

In my controller, I put a script like below:

class IntroController extends Controller
{
public function index(Account $account)
{
$user = $account->where('first_name', 'ryan')->first();
return $user;
}
}

In the Account model:

namespace App;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Account extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'accounts';
protected $fillable = ['first_name', 'last_name', 'email'];
}

I get an error message like below:

AuthenticationException in Find.php line 179: Authentication failed.

It would be appreciated if you could help me with this issue.

Thanks,
Ryan

@bke-daniel
Copy link

Just in addition to @rkang30
This is not mainly an issue but in my setup the "port" var doesn't seem to get through and is being replaced with the 3306 MySQL port. Therefore no connection at all, but changing this:
'port' => env('DB_PORT', 27017),
to this
'port' => '27017',
works like a charme.

Cheers

@likeadeckofcards
Copy link

@bke-daniel Did you change your .env file?

@bke-daniel
Copy link

@infernobass7 Sadly I have to answer that with a short and straight: Yes. :/

@yazeed
Copy link

yazeed commented Jan 21, 2017

@chinhvowili's solution worked for me, Laravel 5 should take into consideration the default connection configuration required for MongoDB 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants