Centos7安装mongodb
<p>mongodb官网:https://www.mongodb.com/</p><h3 id="1配置mongodbyum源">1.配置mongodb(yum源)</h3>
<p>创建一个/etc/yum.repos.d/mongodb-org-4.2.repo文件,以便您可以使用yum以下命令直接安装MongoDB</p>
<pre><code>
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
</code></pre>
<h3 id="2安装mongodb">2、安装mongodb</h3>
<pre><code>yum install -y mongodb-org
</code></pre>
<h3 id="3修改配置文件mongodbconf修改绑定ip默认127001只允许本地连接修改为bindip0000">3、修改配置文件mongodb.conf。修改绑定ip默认127.0.0.1只允许本地连接,修改为bindIp:0.0.0.0</h3>
<pre><code>vim /etc/mongod.conf
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0# Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
</code></pre>
<h3 id="4常用命令">4、常用命令</h3>
<pre><code>systemctl status mongod.service # 查看mongod状态
systemctl start mongod.service # 启动
systemctl stop mongod.service # 停止
systemctl enable mongod.service # 自启
</code></pre>
<h3 id="5远程连接">5、远程连接</h3>
<h5 id="默认连接">默认连接</h5>
<pre><code>mongo 10.128.218.14:27017
</code></pre>
<h5 id="连接到自定义的用户">连接到自定义的用户</h5>
<p>创建用户,设置账号,密码,权限</p>
<pre><code>// admin数据库
> use admin
switched to db admin
> db.createUser({ user:"root", pwd:"123456", roles:["root"] })
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
// 其他数据库
> use test
switched to db test
> db.createUser({ user:"admin", pwd:"123456", roles:["readWrite", "dbAdmin"] })
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
</code></pre>
<p>修改mongodb.conf文件,启用身份验证</p>
<pre><code>vi /etc/mongod.conf
security:
authorization: "enabled" # disable or enabled
</code></pre>
<p>重启MongoDB</p>
<pre><code>sudo service mongod restart
</code></pre>
<p>用户认证</p>
<pre><code>// 授权
> use admin
switched to db admin
> db.auth("root", "123456")
// 其他常用命令
db.updateUser(user, writeConcern) # 更新用户
db.dropUser('test') # 删除用户
</code></pre>
<p>远程连接</p>
<pre><code>// 终端连接
mongo 10.128.218.14:27017/database -u username -p password
// mongoose方式连接
mongoose.connect('mongodb://username:password@host:port/database?options...', {useNewUrlParser: true});
// 通过客户端连接,推荐客户端软件 Robo 3T
</code></pre>
<h4 id="用户权限角色说明">用户权限角色说明</h4>
<table>
<thead>
<tr>
<th>规则</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>root</td>
<td>只在admin数据库中可用。超级账号,超级权限</td>
</tr>
<tr>
<td>Read</td>
<td>允许用户读取指定数据库</td>
</tr>
<tr>
<td>readWrite</td>
<td>允许用户读写指定数据库</td>
</tr>
<tr>
<td>dbAdmin</td>
<td>允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile</td>
</tr>
<tr>
<td>userAdmin</td>
<td>允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户</td>
</tr>
<tr>
<td>clusterAdmin</td>
<td>只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限</td>
</tr>
<tr>
<td>readAnyDatabase</td>
<td>只在admin数据库中可用,赋予用户所有数据库的读权限</td>
</tr>
<tr>
<td>readWriteAnyDatabase</td>
<td>只在admin数据库中可用,赋予用户所有数据库的读写权限</td>
</tr>
<tr>
<td>userAdminAnyDatabase</td>
<td>只在admin数据库中可用,赋予用户所有数据库的userAdmin权限</td>
</tr>
<tr>
<td>dbAdminAnyDatabase</td>
<td>只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限</td>
</tr>
</tbody>
</table><br><br>
来源:https://www.cnblogs.com/hexrui/p/14885785.html
頁:
[1]