MongoDB和关系型数据库对比:
关系型数据库数据结构:
MongoDB数据结构:
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%]
[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: …… ##警告咱们忽略
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
小说明:建立用户只针对库;数组