ubuntu下使用mongo

 mongo: 

无法远程登录mongo:

修改/etc/mongo.conf中的bind_ip=127.0.0.1改为:

bind_ip=0.0.0.0,然后重启mongo:

sudo /etc/init.d/mongodb restart 

先使用robo3t-gui工具连接了未配置认证(mongo.conf中auth=true处于注释状态)的mongo,在admin数据库下创建用户,发现有9中类型的role,创建root之后执行show users:

其中role类型有: 

read,readWrite,

dbAdmin,userAdmin,clusterAdmin,

readAnyDatabase,readWriteAnyDatabase,

userAdminAnyDatabase,dbAdminAnyDatabase



首先不要开启认证,确保mongo.conf里的auth=true处于注释状态,先创建用户:

#db.createUser({'user':'myusr','pwd':'mypwd',roles:['readWrite','dbAdmin']})

如在admin下创建一个拥有所有权限的用户名:myusr,密码是mypwd,则方法是:

db.createUser({'user':'root','pwd':'123',"roles" : [ { "role" : "read", "db" : "admin" }, { "role" : "readWrite", "db" : "admin" }, { "role" : "dbAdmin", "db" : "admin" }, { "role" : "userAdmin", "db" : "admin" }, { "role" : "clusterAdmin", "db" : "admin" }, { "role" : "readAnyDatabase", "db" : "admin" }, { "role" : "readWriteAnyDatabase", "db" : "admin" }, { "role" : "userAdminAnyDatabase", "db" : "admin" }, { "role" : "dbAdminAnyDatabase", "db" : "admin" } ]})

格式化下看:

db.createUser({'user':'root','pwd':'123',"roles" : [ { "role" : "read", "db" : "admin" }, { "role" : "readWrite", "db" : "admin" }, { "role" : "dbAdmin", "db" : "admin" }, { "role" : "userAdmin", "db" : "admin" }, { "role" : "clusterAdmin", "db" : "admin" }, { "role" : "readAnyDatabase", "db" : "admin" }, { "role" : "readWriteAnyDatabase", "db" : "admin" }, { "role" : "userAdminAnyDatabase", "db" : "admin" }, { "role" : "dbAdminAnyDatabase", "db" : "admin" } ]})

然后执行show users:

show users;

{

"_id" : "admin.root",

"userId" : UUID("8d5b12e7-830b-4464-b14f-b56167e7a8ed"),

"user" : "root",

"db" : "admin",

"roles" : [

{

"role" : "read",

"db" : "admin"

},

{

"role" : "readWrite",

"db" : "admin"

},

{

"role" : "dbAdmin",

"db" : "admin"

},

{

"role" : "userAdmin",

"db" : "admin"

},

{

"role" : "clusterAdmin",

"db" : "admin"

},

{

"role" : "readAnyDatabase",

"db" : "admin"

},

{

"role" : "readWriteAnyDatabase",

"db" : "admin"

},

{

"role" : "userAdminAnyDatabase",

"db" : "admin"

},

{

"role" : "dbAdminAnyDatabase",

"db" : "admin"

}

]

}



先不执行auth方法,将mongo.conf里面的auth=true打开,重启mongo,我们看下执行mongo -u myusr -p能否连接成功:
mongo -u myusr -p
发现failed了:exception: login failed

use admin;
db.auth('root','123')

再执行: mongo -u myusr --authenticationDatabase admin -p 

此时可以连接成功


创建数据库:

use database_name


在数据库mydb上创建用户testusr,密码:123

use mydb

db.createUser({'user':'testusr','pwd':'123',"roles" : [ { "role" : "read", "db" : "mydb" }, { "role" : "readWrite", "db" : "mydb" }, { "role" : "dbAdmin", "db" : "mydb" }]})


> use mydb
switched to db mydb
> db.createUser({'user':'testusr','pwd':'123',"roles" : [ { "role" : "read", "db" : "mydb" }, { "role" : "readWrite", "db" : "mydb" }, { "role" : "dbAdmin", "db" : "mydb" }]})
Successfully added user: {
"user" : "testusr",
"roles" : [
{
"role" : "read",
"db" : "mydb"
},
{
"role" : "readWrite",
"db" : "mydb"
},
{
"role" : "dbAdmin",
"db" : "mydb"
}
]
}
> show users;
{
"_id" : "mydb.testusr",
"userId" : UUID("eb560fc2-90d2-4600-9814-f9bd3fbedf33"),
"user" : "testusr",
"db" : "mydb",
"roles" : [
{
"role" : "read",
"db" : "mydb"
},
{
"role" : "readWrite",
"db" : "mydb"
},
{
"role" : "dbAdmin",
"db" : "mydb"
}
]
}

用testusr登录mongo:

mongo -u testusr --authenticationDatabase mydb -h your_ip -p 


mongo -u testusr your_ip:port/mydb -p

MongoDB shell version v4.2.2

Enter password:

connecting to: mongodb://your_ip:port/mydb?compressors=disabled&gssapiServ

iceName=mongodb

Implicit session: session { "id" : UUID("df811c7d-bcc2-4d48-9ac9-38c2e8f96b23")

}

MongoDB server version: 3.6.8

WARNING: shell and server versions do not match

Welcome to the MongoDB shell.

For interactive help, type "help".

For more comprehensive documentation, see

        http://docs.mongodb.org/

Questions? Try the support group

        http://groups.google.com/group/mongodb-user

Server has startup warnings:

2021-09-02T11:00:47.179+0800 I STORAGE  [initandlisten]

2021-09-02T11:00:47.179+0800 I STORAGE  [initandlisten] ** WARNING: Using the XF

S filesystem is strongly recommended with the WiredTiger storage engine

2021-09-02T11:00:47.179+0800 I STORAGE  [initandlisten] **          See http://d

ochub.mongodb.org/core/prodnotes-filesystem

2021-09-02T11:00:48.130+0800 I CONTROL  [initandlisten]

2021-09-02T11:00:48.130+0800 I CONTROL  [initandlisten] ** WARNING: Access contr

ol is not enabled for the database.

2021-09-02T11:00:48.130+0800 I CONTROL  [initandlisten] **          Read and wri

te access to data and configuration is unrestricted.

2021-09-02T11:00:48.130+0800 I CONTROL  [initandlisten]


启动mongo:

mongo -u testusr localhost:27017/testdb -p


关闭mongo:

> use admin

switched to db admin

> db.shutdownServer()

server should be down...

2021-09-02T11:22:54.203+0800 I NETWORK  [thread1] tryingreconnect to localhost:47017 (127.0.0.1) failed

2021-09-02T11:22:54.203+0800 W NETWORK  [thread1] Failedto connect to 127.0.0.1:47017, in(checking socket for error after poll), reason: Connection refused

2021-09-02T11:22:54.203+0800 I NETWORK  [thread1] reconnect localhost:47017 (127.0.0.1) failed failed

> db.shutdownServer()

2021-09-02T11:22:58.312+0800 I NETWORK  [thread1] trying reconnect to localhost:47017 (127.0.0.1) failed

2021-09-02T11:22:58.312+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:47017, in(checking socket for error after poll), reason: Connection refused

2021-09-02T11:22:58.312+0800 I NETWORK  [thread1] reconnect localhost:47017 (127.0.0.1) failed failed

server should be down...