-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.html
167 lines (150 loc) · 6.63 KB
/
auth.html
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<title>数据库-mongodb权限</title>
<!--国产了浏览器的兼容性(webkit:极速模式、ie-comp:兼容模式、ie-stand:ie模式)-->
<meta name="renderer" content="webkit"/>
<!--ie内核浏览器兼容性-->
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<!--字符编码-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<!--移动端禁止缩放-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<!--缓存-->
<!--private、must-revalidate/no-cache/max-age-->
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
<!--seo优化-->
<meta name="keywords" content="mongo,mongodb"/>
<meta name="description" content="mongodb"/>
<meta name="robots" content="all"/>
<meta name="author" content="zhanghongwei"/>
<!--标签-->
<link rel="bookmark" type="image/x-icon" href="images/favicon.ico"/>
<link rel="icon" type="image/x-icon" href="images/favicon.ico"/>
<link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico"/>
<!--css-->
<link rel="stylesheet" type="text/css" href="lib/bootstrap/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="lib/highlight/styles/monokai_sublime.css">
<link rel="stylesheet" type="text/css" href="css/mongodb.css">
<!-- js -->
<script data-main="js/modules/main" src="lib/require/require.js" defer async="true"></script>
<!--[if lte IE 8]>
<script type="text/javascript" src="js/html5.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-navbar-collapse" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#" style="padding:5px;padding-right:20px;"><img alt="Brand" src="images/logo.png" style="width:111px;height:40px;" /></a>
</div>
<div id="bs-navbar-collapse" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="index.html">首页</a></li>
<li><a href="replicaset.html">复制集</a></li>
<li class="dropdown active" >
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">安全<span class="caret"></span><span class="sr-only">(current)</span></a>
<ul class="dropdown-menu">
<li><a href="auth.html">权限</a></li>
<li><a href="#">安全级别</a></li>
<li><a href="dump.html">备份与恢复</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">性能<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="performance.html">慢查询</a></li>
<li><a href="#">监控</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">crud<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">插入</a></li>
<li><a href="remove.html">删除</a></li>
<li><a href="#">查找</a></li>
<li><a href="#">更新</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">常见条件</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">driver<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">java</a></li>
<li><a href="#">python</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div class="container" style="margin-top:60px;">
<div class="content">
<section>
<article>
<h3>创建一个用户</h3>
<p>mongoDB 没有炒鸡无敌用户root(<a href="roles.html">查看所有角色</a>),只有能“管理用户”的用户 <a href="http://docs.mongodb.org/manual/reference/built-in-roles/#userAdminAnyDatabase">userAdminAnyDatabase</a>。
网上有很多资料都使用addUser()方法,但在mongodb3.0之后都使用<a href="http://docs.mongodb.org/manual/reference/method/db.createUser/">creatUser()。</a></p>
<pre><code class="code">$ use admin
$ db.createUser(
{
"user":"root",
"pwd":"root",
"roles":[{"role":"userAdminAnyDatabase","db":"admin"}]
}
)</code></pre>
</article>
<article>
<h3>查看用户</h3>
<pre><code class="code"># 查看用户,两种方法
$ show users
$ db.system.users.find()
# 查看用户结果
{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "SCRA
M-SHA-1" : { "iterationCount" : 10000, "salt" : "7eWmznto1bhYMrr8jjsFwg==", "sto
redKey" : "KbI6gd7JynIM8kUcJqXkw2Z2MMc=", "serverKey" : "fXA5yDP6mhTfZ208RWbrNw6
+duc=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }</code></pre>
</article>
<article>
<h3>登陆“用户管理”账号</h3>
<p>登陆成功后使用$ show collections时依然提示错误,因为登陆后只有“管理用户”权限。还需要继续创建有读、写权限的账号。</p>
<pre><code class="code"># 认证,返回1表示成功
$ use admin
$ db.auth("root","root")
# 继续创建有读、写权限的用户
$ use test
$ db.createUser(
{
"user":"root",
"pwd":"root",
"roles":[{ role: "readWrite", db: "test" },{ role: "read", db: "other" }]
}
)
</code></pre>
</article>
<article>
<h3>开启验证</h3>
<p>mongodb默认是不开启权限认证的。</p>
<pre><code class="code">Windows:
# 1.注册表中找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\MongoDB
# 2.修改ImagePath中的参数,在结束双引号与--service中间添加--auth
# 3.重启mongdb服务
Linux:
# mongod启动时添加--auth参数或在启动配置文件中指定auth=true
</code></pre>
</article>
</section>
</div>
</div>
<a id="backtop" href="javascript:void(0)"></a>
</body>
</html>