mongodb 3.4
window7html
[root@snails ~]# ps -ef|grep mongod [root@snails ~]# mongo --host=127.0.0.1 --port=27017 MongoDB shell version: 3.2.7 connecting to: 127.0.0.1:27017/test > show dbs #显示数据库列表 > show collections #显示当前数据库中的集合(相似关系数据库中的表) > show users #显示用户 > use <db name> #切换当前数据库,若是数据库不存在则建立数据库。 > db.help() #显示数据库操做命令,里面有不少的命令 > db.foo.help() #显示集合操做命令,一样有不少的命令,foo指的是当前数据库下,一个叫foo的集合,并不是真正意义上的命令 > db.foo.find() #对于当前数据库中的foo集合进行数据查找(因为没有条件,会列出全部数据) > db.foo.find( { a : 1 } ) #对于当前数据库中的foo集合进行查找,条件是数据中有一个属性叫a,且a的值为1
MongoDB没有建立数据库的命令,但有相似的命令。 如:若是你想建立一个“myTest”的数据库,先运行use
myTest命令,以后就作一些操做(如:db.createCollection(‘user’)),这样就能够建立一个名叫“myTest”的数据库。mongodb
> db.dropDatabase() #删除当前使用数据库 > db.cloneDatabase("127.0.0.1") #将指定机器上的数据库的数据克隆到当前数据库 > db.copyDatabase("mydb", "temp", "127.0.0.1") #将本机的mydb的数据复制到temp数据库中 > db.repairDatabase() #修复当前数据库 > db.getName() #查看当前使用的数据库,也能够直接用db > db.stats() #显示当前db状态 > db.version() #当前db版本 > db.getMongo() #查看当前db的连接机器地址 > db.serverStatus() #查看数据库服务器的状态
MongoDB安装完成后,默认是不须要输入用户名密码便可登陆的,可是每每数据库方面咱们会出于安全性的考虑而设置用户名密码,本篇文章主要介绍了MongoDB添加管理员/普通用户的方法。shell
In the admin database, add a user with the userAdminAnyDatabase role.
For example, the following creates the user myUserAdmin in the admin
database:数据库
在admin数据库中,添加一个用户并赋予userAdminAnyDatabase
角色。
例如,下面是在admin数据库中建立一个名为myUserAdmin
用户。安全
注意: The database where you create the user (in this example, admin) is the user’s authentication database. Although the user would authenticate to this database, the user can have roles in other databases; i.e. the user’s authentication database does not limit the user’s privileges.bash
注意:你建立用户的这个数据库(这里就是admin数据库)是用户认证数据库。
尽管用户是在这个数据库认证,而用户又有其余数据库的角色;即,用户认证数据库不限制用户权限。服务器
在window
管理员下启动cmd
,而且链接上mongodb
,
链接命令:函数
D:\Program Files\MongoDB\Server\3.4\bin>mongo.exe
建立用户命令:学习
use admin db.createUser( { user: "myUserAdmin", pwd: "abc123", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } ) #结果 Successfully added user: { "user" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
执行如下命令,看看结果ui
> show users > db.system.users.find()
window中:
D:\Program Files\MongoDB\Server\3.4\mongod.cfg
这个配置文件是我本身手动配置,关于mongodb配置,能够参考:
mongodb3.4的安装和配置
在配置文件中添加.
security: authorization: enabled
所有配置:
systemLog: destination: file path: D:\mongodbdata\log\mongod.log logAppend: true storage: journal: enabled: true dbPath: D:\mongodbdata\db net: bindIp: 127.0.0.1 port: 27017 security: authorization: enabled
liunx:
[root@snails ~]# echo "auth = true" >> /root/mongodb/bin/mongodb.conf [root@snails ~]# systemctl restart systemd-mongodb
接着就是重启mongod
实例。说明了就是重启mongodb服务。
D:\Program Files\MongoDB\Server\3.4\bin>mongo.exe MongoDB shell version v3.4.1 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.1 > use admin switched to db admin > db.auth('myUserAdmin', 'abc123') 1 > show dbs admin 0.000GB local 0.000GB
Once authenticated as the user administrator, use db.createUser() to create additional users. You can assign any built-in roles or user-defined roles to the users.
一旦通过认证的用户管理员,可使用
db.createUser()
去建立额外的用户。
你能够分配mongodb内置的角色或用户自定义的角色给用户。The myUserAdmin user only has privileges to manage users and roles. As myUserAdmin, if you attempt to perform any other operations, such as read from a foo collection in the test database, MongoDB returns an error.
这个
myUserAdmin
用户仅仅只有特权去管理用户和角色,myUserAdmin
,若是你试图执行其余任何操做,例如在test数据库中的foo集合中去读数据,mongodb将返回错误。注意:The database where you create the user (in this example, test) is that user’s authentication database. Although the user would authenticate to this database, the user can have roles in other databases; i.e. the user’s authentication database does not limit the user’s privileges.
你建立用户的数据库(这里就是test数据库)是该用户认证数据库。尽管用户认证是这个数据库,用户依然能够有其余数据库的角色。即用户认证数据库不限制用户权限。
建立普通用户:
>use test > db.createUser( ... { ... user:"test1", ... pwd: "test1", ... roles: [{ role: "readWrite", db: "test"}] ... } ... ) Successfully added user: { "user" : "test1", "roles" : [ { "role" : "readWrite", "db" : "test" } ] } > exit bye D:\Program Files\MongoDB\Server\3.4\bin>mongo.exe MongoDB shell version v3.4.1 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.1 > use test switched to db test > db.auth('test1','test1') 1
window中的cmd中执行:
use admin db.createUser( { user: "root", pwd: "root", roles: [ { role: "root", db: "admin" } ] } );
内建的角色
数据库用户角色:read、readWrite;
数据库管理角色:dbAdmin、dbOwner、userAdmin;
集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
备份恢复角色:backup、restore;
全部数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
超级用户角色:root // 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
内部角色:__system
角色说明:
Read:容许用户读取指定数据库
readWrite:容许用户读写指定数据库
dbAdmin:容许用户在指定数据库中执行管理函数,如索引建立、删除,查看统计或访问system.profile
userAdmin:容许用户向system.users集合写入,能够找指定数据库里建立、删除和管理用户
clusterAdmin:只在admin数据库中可用,赋予用户全部分片和复制集相关函数的管理权限。
readAnyDatabase:只在admin数据库中可用,赋予用户全部数据库的读权限
readWriteAnyDatabase:只在admin数据库中可用,赋予用户全部数据库的读写权限
userAdminAnyDatabase:只在admin数据库中可用,赋予用户全部数据库的userAdmin权限
dbAdminAnyDatabase:只在admin数据库中可用,赋予用户全部数据库的dbAdmin权限。
root:只在admin数据库中可用。超级帐号,超级权限
官网参考地址:
http://docs.mongoing.com/manual-zh/tutorial/enable-authentication.html