Tag Archives: mongodb

mongodb:E QUERY [thread1] SyntaxError: missing ; before statement @(shell):1:4

Mongodb error: e query [thread1] syntax error: missing; before statement @(shell):1:4

March 27, 2019 22:55:53 zhangpeters read 544 more

Personal classification: mongodb

After installing mongodb at night and setting the password, if you directly connect to mongodb without password, you can connect to it, but you cannot switch to the specified database

-> # mongo                            
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
> 1+1
2
> ues admin
2019-03-27T22:42:21.838+0800 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:4

1

2

3

4

5

6

7

8

9

The solution is to add a password when connecting:

-> # mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.6.3
> use test
switched to db test

Error occurred when using java to connect mongodb: command failed with error 18 (authentication failed): “authentication failed.”

Error information:

Caused by: com.mongodb.MongoCommandException: Command failed with error 18: ‘Authentication failed.’ on server XXXXXXX:27017. The full response is { “ok” : 0.0, “errmsg” : “Authentication failed.”, “code” : 18, “codeName” : “AuthenticationFailed” }

After consulting the data, it is found that the wrong authorized account number has been used

Each database in mongodb is independent of each other and has independent permissions. The correct way is to use the root account to create a sub account in the database to be operated, and then use this sub account to connect Mongo

>use admin

switched to db admin

>db.auth("root","123456")

1

>show dbs

admin   0.000GB
config  0.000GB
good    0.000GB
local   0.000GB
test    0.000GB

>use good

switched to db good

> db.createUser({user:"daniel",pwd:"123456",roles:[{role:"dbOwner",db:"good"}]})
Successfully added user: {
        "user" : "daniel",
        "roles" : [
                {
                        "role" : "dbOwner",
                        "db" : "good"
                }
        ]
}

Finally, in the Java code into the account, password and so on.

Failed to load list of database Failed to execute “listdatabases” command.

How to Solve Failed to load list of database
Failed to execute “listdatabases” command.
use Robo 3T  connect mongodb error: Failed to load list of database

detail: Failed to execute “listdatabases” command.

Simply set the account password in the second tab Authentication field, and then select Auth Mechanism.