Centos7安装Mongodb数据库

原文链接:NoSQL——MongoDB

mongodb安装

按照官方网站 https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ , 编辑 repo 文件。

1
2
3
4
5
6
7
8
9
[root@ying01 ~]# cd /etc/yum.repos.d/
[root@ying01 yum.repos.d]# vim mongodb-org-4.0.repo

[mongodb-org-4.0]
name = MongoDB Repository
baseurl = https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck = 1
enabled = 1
gpgkey = https://www.mongodb.org/static/pgp/server-4.0.asc

查看 yum list 当中新生成的 mongodb-org,并用 yum 安装所有的 mongodb-org。

1
2
3
4
5
6
7
8
[root@ying01 yum.repos.d]# yum list|grep mongodb-org
mongodb-org.x86_64 4.0.1-1.el7 @mongodb-org-4.0
mongodb-org-mongos.x86_64 4.0.1-1.el7 @mongodb-org-4.0
mongodb-org-server.x86_64 4.0.1-1.el7 @mongodb-org-4.0
mongodb-org-shell.x86_64 4.0.1-1.el7 @mongodb-org-4.0
mongodb-org-tools.x86_64 4.0.1-1.el7 @mongodb-org-4.0

[root@ying01 yum.repos.d]# yum install -y mongodb-org

连接mongodb

连接配置

在 mongod.conf 文件中,添加本机 ip,用逗号隔开。

1
2
3
[root@ying01 ~]# vim /etc/mongod.conf

bindIp: 127.0.0.1,192.168.112.136 //增加IP

启动,并查看进程和端口。

1
2
3
4
5
6
7
8
9
[root@ying01 ~]# systemctl start mongod

[root@ying01 ~]# ps aux |grep mongod
mongod 8169 0.8 3.2 1074460 60764 ? Sl 09:51 0:01 /usr/bin/mongod -f /etc/mongod.conf
root 8197 0.0 0.0 112720 984 pts/0 S+ 09:54 0:00 grep --color=auto mongod
[root@ying01 ~]# netstat -lntp |grep mongod
tcp 0 0 192.168.112.136:27017 0.0.0.0:* LISTEN 8169/mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 8169/mongod
[root@ying01 ~]#

连接数据库

直接连接

1
[root@ying01 ~]# mongo

指定端口(配置文件没有明确指定)

1
[root@ying01 ~]# mongo --port 27017

远程连接,需要IP 和端口

1
[root@ying01 ~]# mongo --host 192.168.112.136  --port 27017

mongodb 用户管理

进入数据库,创建用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@ying01 ~]# mongo


> use admin //切换到admin
switched to db admin
> db.createUser( { user: "admin", customData: {description: "superuser"}, pwd: "www123", roles: [ { role: "root", db: "admin" } ] } ) //创建admin用户
Successfully added user: {
"user" : "admin",
"customData" : {
"description" : "superuser"
},
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}

语句释义:

1
2
3
4
5
6
7
db.createUser( { user: "admin", customData: {description: "superuser"}, pwd: "admin122", roles: [ { role: "root", db: "admin" } ] } )

db.createUser 创建用户的命令
user: "admin" 定义用户名
customData: {description: "superuser"}
pwd: "admin122" 定义用户密码
roles: [ { role: "root", db: "admin" } ] 规则:角色 root,数据库admin

列出所有用户,需要切换到admin库

1
2
> db.system.users.find()
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "NRoDD1kSxLktW8vyDg4mpw==", "storedKey" : "yX+2kSbCl1bPpsh+0ZeE7QlcW6A=", "serverKey" : "XM9NgrMNOwXAvuWusY6iVhpyuFw=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "MOokBWPCOobBeNwHnhm/2QagzAT8h2yIuCzROg==", "storedKey" : "tAqs7zMF8InT0FU09lCgq2ZVB9wRgeIyoa1UONgRDM0=", "serverKey" : "lN2TYZX5Snik4gMthUNZE7jw71Nkxo13LAChh9K8ZiI=" } }, "customData" : { "description" : "superuser" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }

查看当前库下所有的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
> show users
{
"_id" : "admin.admin",
"user" : "admin",
"db" : "admin",
"customData" : {
"description" : "superuser"
},
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}

创建新用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
> db.createUser({user:"ying",pwd:"www123",roles:[{role:"read",db:"testdb"}]})
Successfully added user: {
"user" : "ying",
"roles" : [
{
"role" : "read",
"db" : "testdb"
}
]
}
> show users
{
"_id" : "admin.admin",
"user" : "admin",
"db" : "admin",
"customData" : {
"description" : "superuser"
},
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
{
"_id" : "admin.ying",
"user" : "ying",
"db" : "admin",
"roles" : [
{
"role" : "read",
"db" : "testdb"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}

删除用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
> db.dropUser('ying')
true
> show users
{
"_id" : "admin.admin",
"user" : "admin",
"db" : "admin",
"customData" : {
"description" : "superuser"
},
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}

切换到 testdb 库,若此库不存在,会自动创建

1
2
3
4
5
6
> use testdb
switched to db testdb
> show users //当前库,无用户
> db.system.user.find()
> ^C
bye

要使用用户生效,需要编辑启动服务脚本,添加 –auth,以后登录需要身份验证

1
2
3
4
[root@ying01 ~]# vim /usr/lib/systemd/system/mongod.service


Environment="OPTIONS=-f /etc/mongod.conf" 改为 Environment="OPTIONS=--auth -f /etc/mongod.conf"

重启 mongod 服务

1
2
3
4
5
6
7
[root@ying01 ~]# systemctl restart mongod
Warning: mongod.service changed on disk. Run 'systemctl daemon-reload' to reload units.
[root@ying01 ~]# systemctl daemon-reload
[root@ying01 ~]# systemctl restart mongod
[root@ying01 ~]# ps aux |grep mongod
mongod 8611 12.6 2.8 1068324 52744 ? Sl 11:42 0:01 /usr/bin/mongod --auth -f /etc/mongod.conf
root 8642 0.0 0.0 112720 980 pts/0 S+ 11:42 0:00 grep --color=auto mongod

现在直接访问,不加密码,不会查看数据库,因为需要认证

1
2
3
4
5
6
7
8
[root@ying01 ~]# mongo --host 192.168.112.136 --port 27017
MongoDB shell version v4.0.1
connecting to: mongodb://192.168.112.136:27017/
MongoDB server version: 4.0.1
> use admin
switched to db admin
> show users
2018-08-27T11:43:36.654+0800 E QUERY [js] Error: command usersInfo requires authentication : //需要身份验证

使用密码,身份认证,登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@ying01 ~]# mongo --host 192.168.112.136 --port 27017 -u admin -p 'admin122' --authenticationDatabase "admin"

> use admin
switched to db admin
> show users
{
"_id" : "admin.admin",
"user" : "admin",
"db" : "admin",
"customData" : {
"description" : "superuser"
},
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}

切换到db1下,创建新用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
> use db1
switched to db db1
> show users
> db.createUser( { user: "test1", pwd: "www123", roles: [ { role: "readWrite", db: "db1" }, {role: "read", db: "db2" } ] } )
Successfully added user: {
"user" : "test1",
"roles" : [
{
"role" : "readWrite",
"db" : "db1"
},
{
"role" : "read",
"db" : "db2"
}
]
}

> show users //查看在db1库下,创建的用户
{
"_id" : "db1.test1",
"user" : "test1",
"db" : "db1",
"roles" : [
{
"role" : "readWrite",
"db" : "db1"
},
{
"role" : "read",
"db" : "db2"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}

> use db1
switched to db db1
> db.auth('test1','www123') //验证用户test1,返回值为1,则验证成功
1

MongoDB用户角色

1
2
3
4
5
6
7
8
9
10
Read:允许用户读取指定数据库
readWrite:允许用户读写指定数据库
dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
root:只在admin数据库中可用。超级账号,超级权限

mongodb创建集合及数据管理

在db1下,创建 mycol的集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@ying01 ~]# mongo --host 192.168.112.136 --port 27017 -u admin -p 'www123' --authenticationDatabase "admin"


> use db1
switched to db db1
> show users
{
"_id" : "db1.test1",
"user" : "test1",
"db" : "db1",
"roles" : [
{
"role" : "readWrite",
"db" : "db1"
},
{
"role" : "read",
"db" : "db2"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}

> db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
{
"ok" : 0,
"errmsg" : "too many users are authenticated",
"code" : 13,
"codeName" : "Unauthorized"
}

语法:db.createCollection(name,options)

1
2
3
4
5
6
name就是集合的名字
options可选,用来配置集合的参数,参数如下:
capped true/false (可选)如果为true,则启用封顶集合。封顶集合是固定大小的集合,当它达到其最大大小,会自动覆盖最早的条目。如果指定true,则也需要指定尺寸参数。
autoindexID true/false (可选)如果为true,自动创建索引_id字段的默认值是false
size (可选)指定最大大小字节封顶集合。如果封顶如果是 true,那么你还需要指定这个字段。单位B
max (可选)指定封顶集合允许在文件的最大数量

查看集合:show collections 或者使用show tables

1
2
> show collections
mycol

在集合Account中,直接插入数据。如果该集合不存在,则mongodb会自动创建集合

1
2
3
4
5
6
7
8
9
10
> db.Account.insert({AccountID:1,UserName:"123",password:"123456"})
WriteResult({ "nInserted" : 1 })
> show tables //多了一个
Account
mycol
> db.Account.insert({AccountID:2,UserName:"ying",password:"abcdef"}) //再插入一条信息
WriteResult({ "nInserted" : 1 })
> show tables
Account
mycol

在集合中更新信息数据

1
2
> db.Account.update({AccountID:1},{"$set":{"Age":20}})  //在集合Account中,第一条中,增加一项信息
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

查看所有文档:db.Account.find(),此时在第一条 有更新的信息

1
2
3
> db.Account.find()
{ "_id" : ObjectId("5b83bf9209eb45c97dce1c4c"), "AccountID" : 1, "UserName" : "123", "password" : "123456", "Age" : 20 }
{ "_id" : ObjectId("5b83bfe509eb45c97dce1c4d"), "AccountID" : 2, "UserName" : "ying", "password" : "abcdef" }

查看指定的文档, db.Account.find({AccountID:2})

1
2
> db.Account.find({AccountID:2})  //查看数据库集合Account中,ID为2的文档
{ "_id" : ObjectId("5b83bfe509eb45c97dce1c4d"), "AccountID" : 2, "UserName" : "ying", "password" : "abcdef" }

移除指定的文档:db.Account.remove({AccountID:1})

1
2
3
4
> db.Account.remove({AccountID:1})    //移除id为1d 的文档
WriteResult({ "nRemoved" : 1 })
> db.Account.find() //查看所有文档,此时已经没有ID为1的文档
{ "_id" : ObjectId("5b83bfe509eb45c97dce1c4d"), "AccountID" : 2, "UserName" : "ying", "password" : "abcdef" }

删除集合:db.Account.drop()

1
2
3
4
5
6
7
8
> db.Account.drop()  //删除Account集合
true
> show tables
mycol

> db.mycol.drop() //删除mycol集合
true
> show tables

重新创建集合col2

1
2
> db.col2.insert({AccountID:1,UserName:"123",password:"123456"})
WriteResult({ "nInserted" : 1 })

查看所有集合的状态:db.printCollectionStats()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> db.printCollectionStats()
col2
{
"ns" : "db1.col2",
"size" : 80,
"count" : 1,
"avgObjSize" : 80,
"storageSize" : 16384,
"capped" : false,
"wiredTiger" : {
"metadata" : {
"formatVersion" : 1
},

......

-------------本文结束感谢您的阅读-------------

本文标题:Centos7安装Mongodb数据库

文章作者:Tang

发布时间:2019年02月17日 - 02:02

最后更新:2019年02月18日 - 10:02

原始链接:https://tangx1.com/mongodb/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%