21.26 mongodb介绍

21.26 mongodb介绍

  • 官网www.mongodb.com, 当前最新版3.4
  • C++编写,基于分布式的,属于NoSQL的一种
  • 在NoSQL中是最像关系型数据库的
  • MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档相似于 JSON 对象。字段值能够包含其余文档、数组及文档数组。
  • 关于JSON http://www.w3school.com.cn/json/index.asp
  • 由于基于分布式,因此很容易扩展

MongoDB和关系型数据库对比:
mark
关系型数据库数据结构:
mark
MongoDB数据结构:
markmongodb

21.27 mongodb安装

分析:该安装步骤就是 按照官方提供的方法建立yum源;而后yum安装
这里因为下载太慢,咱们直接把rpm分享到网上直接安装!shell

[root@Dasoncheng src]# cat /etc/yum.repos.d/mongodb-org-3.4.repo    ##自定义yum源;
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/ 
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
[root@Dasoncheng src]# ll
total 402384
-rw-r--r--  1 root root      5976 Oct 17 16:17 mongodb-org-3.4.9-1.el7.x86_64.rpm
-rw-r--r--  1 root root  12187830 Oct 17 16:17 mongodb-org-mongos-3.4.9-1.el7.x86_64.rpm
-rw-r--r--  1 root root  20614326 Oct 17 16:17 mongodb-org-server-3.4.9-1.el7.x86_64.rpm
-rw-r--r--  1 root root  11772258 Oct 17 16:17 mongodb-org-shell-3.4.9-1.el7.x86_64.rpm
-rw-r--r--  1 root root  51154180 Oct 17 16:17 mongodb-org-tools-3.4.9-1.el7.x86_64.rpm
[root@Dasoncheng src]# rpm -ih mongodb-org*
warning: mongodb-org-3.4.9-1.el7.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID a15703c6: NOKEY
################################# [100%]
Updating / installing...
################################# [ 20%]
################################# [ 40%]
################################# [ 60%]
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
################################# [ 80%]
################################# [100%]

21.28 链接mongodb

  • systemctl start mongod //启动服务
  • 在本机能够直接运行命令mongo进入到mongodb shell中
  • 若是mongodb监听端口并非默认的27017,则在链接的时候须要加--port 选项,例如:
    mongo --port 27018
  • 链接远程mongodb,须要加--host,例如:
    mongo --host 127.0.0.1
  • 若是设置了验证,则在链接的时候须要带用户名和密码:
    mongo -uusername -ppasswd --authenticationDatabase db //这个和MySQL挺像
[root@Dasoncheng src]# ll /etc/mongod.conf     ##MongoDB的配置文件;
-rw-r--r-- 1 root root 782 Oct 17 16:20 /etc/mongod.conf
[root@Dasoncheng src]# systemctl start mongod    ##启动mongod
[root@Dasoncheng src]# ps aux |grep mongod   
mongod    38914  2.3  3.5 972180 35500 ?        Sl   16:20   0:00 /usr/bin/mongod -f /etc/mongod.conf
root      38935  0.0  0.0 112664   968 pts/1    S+   16:20   0:00 grep --color=auto mongod
[root@Dasoncheng src]# netstat -lntp |grep mongo
tcp        0      0 192.168.60.11:27017     0.0.0.0:*               LISTEN      38914/mongod        
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      38914/mongod        
[root@Dasoncheng src]# mongo  ##输入mongo直接进去mongo shell里面;
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
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:
……
##警告咱们忽略
[root@Dasoncheng src]# mongo --host 192.168.60.11 --port 27017
##远程登陆mongo,--host 指定主机ip,--port指定端口;
MongoDB shell version v3.4.9
connecting to: mongodb://192.168.60.11:27017/
MongoDB server version: 3.4.9
Server has startup warnings: 
……
##警告咱们忽略

21.29 mongodb用户管理

use admin//须要切换到admin库
db.createUser( { user: "admin", customData: {description: "superuser"}, pwd: "admin122", roles: [ { role: "root", db: "admin" } ] } )
user指定用户,customData为说明字段,能够省略,pwd为密码,roles指定用户的角色,db指定库名
use admin //切换到admin库
db.system.users.find() //列出全部用户,须要切换到admin库
show users //查看当前库下全部的用户
db.dropUser('admin') //删除用户
若要用户生效,还须要编辑启动脚本vim /usr/lib/systemd/system/mongod.service,在OPTIONS=后面增--auth
重启服务systemctl restart mongod
mongo -u "admin" -p "admin122" --authenticationDatabase "admin"数据库

[root@Dasoncheng src]# mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
> use admin    ##须要切换到admin库里面才能建立用户;切换到库才能建立用户(用户针对库)
switched to db admin
> db.createUser( { user: "admin", customData: {description: "superuser"}, pwd: "admin122", roles: [ { role: "root", db: "admin" } ] } )  
##user: "admin"  //用户名
##customData: {description: "superuser"}  //描述,可不要
##pwd: "admin122" //密码
##roles:  //角色,里面又包含了两个键值对;role: "root"角色是root、db: "admin"针对的是admin库;
Successfully added user: {
	"user" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
> db.system.users.find()  
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "Z69r/apOkJK2zq56nktG3w==", "storedKey" : "vpka49IDqjDTb7tFeRK+YqyCmvA=", "serverKey" : "NNcnjtMeXqmn6SOVspyHtKz/mqU=" } }, "customData" : { "description" : "superuser" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
> db.createUser({user:"aming",pwd:"p@ssw0rd",roles:[{role:"read",db:"testdb"}]})
##建立用户aming 并设为只读;
Successfully added user: {
	"user" : "aming",
	"roles" : [
		{
			"role" : "read",
			"db" : "testdb"
		}
	]
}
> show users   ##查看用户,已经包含了aming
{
	"_id" : "admin.admin",
	"user" : "admin",
	"db" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
{
	"_id" : "admin.aming",
	"user" : "aming",
	"db" : "admin",
	"roles" : [
		{
			"role" : "read",
			"db" : "testdb"
		}
}
> db.dropUser('aming')    ##删除用户aming
true
> use testdb   ##切换库,若库不存在 则建立!
switched to db testdb
> show users   ##没法查看用户,在哪一个库里面建立的用户 就去哪一个库查看;
> use admin
switched to db admin
> show users
{
	"_id" : "admin.admin",
	"user" : "admin",
	"db" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

若是要使建立的用户生效,则须要编辑启动脚本:
vim /usr/lib/systemd/system/mongod.service 在OPTIONS=后面增--authjson

[root@Dasoncheng src]# vim /usr/lib/systemd/system/mongod.service
Environment="OPTIONS=--auth -f /etc/mongod.conf"
……
[root@Dasoncheng src]# systemctl restart mongod
Warning: mongod.service changed on disk. Run 'systemctl daemon-reload' to reload units.
##看清楚提示!
[root@Dasoncheng src]# systemctl daemon-reload
[root@Dasoncheng src]# systemctl restart mongod
[root@Dasoncheng src]# ps aux |grep mongo
mongod    39100  7.4  3.5 972180 35668 ?        Sl   18:53   0:01 /usr/bin/mongod --auth -f /etc/mongod.conf
##进程这里多了一个--auth验证,用户才会生效
root      39125  0.0  0.0 112664   968 pts/1    S+   18:53   0:00 grep --color=auto mongo
[root@Dasoncheng src]# mongo --host 192.168.60.11 --port 27017 -u admin -p 'admin122' --authenticationDatabase "admin"  
##登陆;
MongoDB shell version v3.4.9
connecting to: mongodb://192.168.60.11:27017/
MongoDB server version: 3.4.9
> use admin
switched to db admin
> show users
{
	"_id" : "admin.admin",
	"user" : "admin",
	"db" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

权限说明:

use db1
db.createUser( { user: "test1", pwd: "123aaa", roles: [ { role: "readWrite", db: "db1" }, {role: "read", db: "db2" } ] } )
test1用户对db1库读写,对db2库只读。
之因此先use db1,表示用户在 db1 库中建立,就必定要db1库验证身份,即用户的信息跟随随数据库。好比上述 test1虽然有 db2 库的读取权限,可是必定要先在db1库进行身份验证,直接访问会提示验证失败。
use db2
db.auth("test1", "123aaa")vim

> use db1
switched to db db1
> db.createUser( { user: "test1", pwd: "123aaa", roles: [ { role: "readWrite", db: "db1" }, {role: "read", db: "db2" } ] } )
Successfully added user: {
	"user" : "test1",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "db1"
		},
		{
			"role" : "read",
			"db" : "db2"
		}
	]
}
> use db2
switched to db db2
> db.auth('test1','123aaa')
Error: Authentication failed.
0
> use db1    ##只有先在db1里面验证身份以后,才能对db2有该有的权限;
switched to db db1
> db.auth('test1','123aaa')
1

MongoDB用户角色

  • Read:容许用户读取指定数据库
  • readWrite:容许用户读写指定数据库
  • dbAdmin:容许用户在指定数据库中执行管理函数,如索引建立、删除,查看统计或访问system.profile
  • userAdmin:容许用户向system.users集合写入,能够找指定数据库里建立、删除和管理用户
  • clusterAdmin:只在admin数据库中可用,赋予用户全部分片和复制集相关函数的管理权限。
  • readAnyDatabase:只在admin数据库中可用,赋予用户全部数据库的读权限
  • readWriteAnyDatabase:只在admin数据库中可用,赋予用户全部数据库的读写权限
  • userAdminAnyDatabase:只在admin数据库中可用,赋予用户全部数据库的userAdmin权限
  • dbAdminAnyDatabase:只在admin数据库中可用,赋予用户全部数据库的dbAdmin权限。
  • root:只在admin数据库中可用。超级帐号,超级权限

MongoDB库管理

  • db.version() //查看版本
  • use userdb //若是库存在就切换,不存在就建立
  • show dbs //查看库,此时userdb并无出现,这是由于该库是空的,尚未任何集合,只须要建立一个集合就能看到了
  • db.createCollection('clo1') //建立集合clo1,在当前库下面建立
  • db.dropDatabase() //删除当前库,要想删除某个库,必须切换到那个库下
  • db.stats() //查看当前库的信息
  • db.serverStatus() //查看mongodb服务器的状态

小说明:建立用户只针对库;数组

相关文章
相关标签/搜索