Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoError: not authorized on db to execute command? When connection with socket.io

I can't connect to my mongodb from socket.io. When I run my server.js it says that it is connected with mongodb and that it is connected to a socket but then it shows the error

MongoError: not authorized on db to execute command find.

I have loged in as an admin user into the mongo admin database, then went to the "chat" db and there created a new user in the database with the role "readWrite" like this.

use chat

and then:

db.createUser({user: "testuser", pwd: "testuser", roles: [{role: "readWrite", db: "chat" }]})
Successfully added user: {
    "user" : "testuser",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "chat"
        }
    ]
}

The user gets created. And when I test it with db.auth("testuser","testuser") I get a 1. I then exit and go to my bin folder and there I write:mongo.exe -u testuser -p testuser chat;

And then it connects to mongodb://127.0.0.1:127017/chat

I then open a new prompt and go to my folder where I have the server.js that connects with the db and run it, it then first says that the server is running and that it is connected with the mongodb.

When I the go to my chat page and load it then it says that: MongoError not authorized...

I connect my server.js file like this

var port = process.env.PORT || 3000;
server.listen(port);
console.log('Server running *:'+port);

var io = require('socket.io').listen(server);
var mongoose = require('mongoose');

//mongoose.connect('mongodb://127.0.0.1:27017/chat', function(err){
mongoose.connect('mongodb://localhost/chat', function(err){

I don´t know what Im missing, I have followed the docs on mongodb and looked all over the net, but not sure where I go wrong.

Any input really appreciated thanks.

When I load the chat page and look in the console I get this error.

https://manmade.se:3000/socket.io/?EIO=3&transport=polling&t=Lceu0Ot&sid=wbFhDl7yylt4_4a1AAAA net::ERR_CONNECTION_RESET

index_webb.html:1 WebSocket connection to 'wss://manmade.se:3000/socket.io/?EIO=3&transport=websocket&sid=wbFhDl7yylt4_4a1AAAA' failed: WebSocket is closed before the connection is established.
like image 469
Claes Gustavsson Avatar asked Mar 13 '26 10:03

Claes Gustavsson


1 Answers

The error seems to suggest that through your program you are not authenticating on the right database.

When connecting from the shell, in your particular case, this would result in an error:

mongo -u test -p test

you would have to specify the chat database:

mongo chat -u test -p test

I don't think it is a socket error because you are connecting, the error message seems to suggest that you are just not authenticating on the right database (in this case the chat database)

like image 53
masterforker Avatar answered Mar 16 '26 05:03

masterforker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!