3

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.
2
  • Are you sure you are connecting to the right port? Commented Jan 16, 2017 at 18:23
  • Yes, I had it working before but then I reinstalled mongodb and now I can´t get it to work. Commented Jan 16, 2017 at 18:28

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

19 Comments

Ok, thanks. So when I login I have to first log in as admin to the admin db? I have worked all day with this and Im start to be really confused now, not knowing what I have tested and not.
With what user am I suppose to connect with? Any that has readWrite permission?
Ok, so I loged in as admin and the:use chat and there I created a new user with "readWrite" in the "chat" db. It was created as it should. and when I test with db.auth("testuser","testuser") I get 1. I then tried standing at the bin folder: mongo -u testuser -p testuser chat; And then I get an error saying that ; is missing haha...
you can connect with any user that has readWrite permission. Even though your user "test" has read write permissions in the chat db, was the actual user created in the admin database or the chat database?
I made a new one just now, and I loged in as admin and then I went to the chat db with use chat and there I created a new user. I have updated my question with the latest I did just now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.