CentOS系统安装MongoDB

8/7/2020 数据库

本文主要讲解在Linux系统下如何部署MongoDB数据库

# 前言

MongoDB是一种面向文档的数据库管理系统,属于NoSQL数据库,是可以应用于各种规模的企业、各个行业以及各类应用程序的开源数据库。

下面介绍下如何在CentOS系统下安装部署MongoDB数据库。

# 下载

官网下载地址:https://www.mongodb.com/download-center/community

如:

$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.22.tgz
1

# 安装

* 下载后解压

$ tar zxf mongodb-linux-x86_64-rhel62-3.2.22.tgz
$ cd  mongodb-linux-x86_64-rhel62-3.2.22/bin
1
2

* 新建配置文件mongodb.conf

$ cat mongodb.conf 
dbpath=/opt/testerzhang/3rd/mongodb-3.2.7-freeproxy/data/db
logpath=/opt/testerzhang/3rd/mongodb-3.2.7-freeproxy/logs/mongodb.log
logappend=true
bind_ip=xxx.xxx.xxx.xxx
port=47017
fork=true
#nohttpinterface=true
#auth=true
1
2
3
4
5
6
7
8
9

默认端口27017,这里配置为47017。

* 启动命令

$./mongod --config ./mongodb.conf
1

## 创建数据库

$ ./mongo 10.10.10.10:47017

切换到admin数据库
> use admin
switched to db admin

创建用户名admin,密码为mysecret
> db.createUser( 
  { 
    user:"admin", 
    pwd:"mysecret", 
    roles: [ { role:"userAdminAnyDatabase", db:"admin" } ]   
  } 
)


切换到mytestdb数据库, 如果没有插入数据,这个数据库 `show dbs;`是不会显示出来的。
> use mytestdb
switched to db mytestdb

创建普通用户mytest,密码:mytest,数据库名mytestdb
> db.createUser({
    user:"mytest",
    pwd:"mytest",
    roles:[{
        role:"dbAdmin",
        db:"mytestdb"
    },{
        role:"readWrite",
        db:"mytestdb"
    }]
})


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

# 验证

$ ./mongo 10.10.10.10:47017/mytestdb -u mytest -p mytest
1

如果能连接成功,就说明成功了,是不是很简单,相信你学会了。


欢迎关注我的公众号testerzhang,原创技术文章第一时间推送。

公众号二维码

Last Updated: 1/1/2022, 11:13:04 PM