linux下安装MongoDB

介绍

官网:https://www.mongodb.com/

安装步骤

一、创建目录

 [[email protected] ~]# cd /usr/local/
 [[email protected] local]# mkdir momgodb
 [[email protected] local]# cd momgodb/

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

选择版本 复制链接进行下载
linux下安装MongoDB
下载

[[email protected] momgodb]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.5.tgz

下载完成
linux下安装MongoDB

[[email protected] momgodb]# ll
总用量 86000
-rw-r--r--. 1 root root 88063053 12月 20 03:15 mongodb-linux-x86_64-rhel70-4.0.5.tgz

二、安装

解压

[[email protected] momgodb]# tar zxvf mongodb-linux-x86_64-rhel70-4.0.5.tgz
mongodb-linux-x86_64-rhel70-4.0.5/README
mongodb-linux-x86_64-rhel70-4.0.5/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-4.0.5/MPL-2
mongodb-linux-x86_64-rhel70-4.0.5/LICENSE-Community.txt
mongodb-linux-x86_64-rhel70-4.0.5/bin/mongodump
mongodb-linux-x86_64-rhel70-4.0.5/bin/mongorestore
mongodb-linux-x86_64-rhel70-4.0.5/bin/mongoexport
mongodb-linux-x86_64-rhel70-4.0.5/bin/mongoimport
mongodb-linux-x86_64-rhel70-4.0.5/bin/mongostat
mongodb-linux-x86_64-rhel70-4.0.5/bin/mongotop
mongodb-linux-x86_64-rhel70-4.0.5/bin/bsondump
mongodb-linux-x86_64-rhel70-4.0.5/bin/mongofiles
mongodb-linux-x86_64-rhel70-4.0.5/bin/mongoreplay
mongodb-linux-x86_64-rhel70-4.0.5/bin/mongod
mongodb-linux-x86_64-rhel70-4.0.5/bin/mongos
mongodb-linux-x86_64-rhel70-4.0.5/bin/mongo
mongodb-linux-x86_64-rhel70-4.0.5/bin/install_compass

重命名

[[email protected] momgodb]# mv mongodb-linux-x86_64-rhel70-4.0.5 mongodb-4.0.5
[[email protected] momgodb]# ls
mongodb-4.0.5  mongodb-linux-x86_64-rhel70-4.0.5.tgz
[[email protected] momgodb]# cd mongodb-4.0.5/
[[email protected] mongodb-4.0.5]# ll
总用量 112
drwxr-xr-x. 2 root root   231 1月  30 11:55 bin
-rw-r--r--. 1 root root 30608 12月 20 02:48 LICENSE-Community.txt
-rw-r--r--. 1 root root 16726 12月 20 02:48 MPL-2
-rw-r--r--. 1 root root  2601 12月 20 02:48 README
-rw-r--r--. 1 root root 57190 12月 20 02:48 THIRD-PARTY-NOTICES

在mongodb目录下面,创建data目录,在data目录下创建db目录和logs。

MongoDB的数据存储在data目录的db目录下,但是这个目录在安装过程不会自动创建,所以你需要手动创建data目录,并在data目录中创建db目录。默认是在 /data/db 目录下。我们可以使用配置文件修改数据库路径,通过 --dbpath 来指定。

[[email protected] momgodb]# mkdir data
[[email protected] momgodb]# cd data/
[[email protected] data]# ls
[[email protected] data]# mkdir db
[[email protected] data]# touch logs
[[email protected] data]# ls
db  logs

在data目录下 创建 my.conf 文件,加入以下文字

#端口号
port = 27017
#数据目录
dbpath = /usr/local/mongodb/data
#日志目录
logpath = /usr/local/mongodb/data/logs
#设置后台运行
fork = true
#日志输出方式
logappend = true
#开启认证
#auth = true

如下

[email protected] data]# vi my.conf 
[[email protected] data]# vi my.conf 
[[email protected] data]# cat my.conf 
# 端口号
port = 27017
#数据目录
dbpath = /usr/local/mongodb/data
#日志目录
logpath = /usr/local/mongodb/data/logs
#设置后台运行
fork = true
#日志输出方式
logappend = true
#开启认证
#auth = true
[[email protected] data]# 

三、启动与关闭 mongodb服务

启动 mongodb服务

在命令行中执行mongo安装目录中的bin目录执行mongod命令来启动mongdb服务。

Mongodb支持从文件读取配置文件。指定配置文件可以使用**-f–config**选项

[[email protected] bin]# ./mongod --config ../../data/my.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1548
child process started successfully, parent exiting
[[email protected] bin]# ps -ef |grep mongo
root       1548      1  3 14:01 ?        00:00:00 ./mongod --config ../../data/my.conf
root       1575   1278  0 14:01 pts/0    00:00:00 grep --color=auto mongo
[[email protected] bin]# 

ps:配置文件的路径一定要写正确,否则找不到文件路径,无法启动

如果你需要进入MongoDB后台管理,你需要先打开mongodb装目录的下的bin目录,然后执行mongo命令文件。

MongoDB Shell是MongoDB自带的交互式Javascript shell,用来对MongoDB进行操作和管理的交互式环境。当你进入mongoDB后台后,它默认会链接到 test 文档(数据库):

关闭mongodb服务

方法一

查看mongo进程

[[email protected] bin]# ps -ef|grep mongod
root       1548      1  0 14:01 ?        00:00:09 ./mongod --config ../../data/my.conf
root       1647   1278  0 14:26 pts/0    00:00:00 grep --color=auto mongod

关闭mongo进程

不能使用kill -9,否则会造成数据丢失

[[email protected] bin]# kill 1548
[[email protected] bin]# ps -ef|grep mongod
root       1649   1278  0 14:27 pts/0    00:00:00 grep --color=auto mongod

如果再次启动报错的情况 先查看mongo进程 如果有占用就kill掉,如果还不行就去{…}/data/db下把 mongod.lock删掉再启动

方法二

进入mongo shell :运行 db.shuidownServer()

四、测试

MongoDB后台管理 Shell

linux下安装MongoDB
成功

访问网页

linux下安装MongoDB

远程访问

一、需要防火墙开启27017端口的访问,这里我直接关闭了防火墙。

二、如果是阿里云服务器,需要进行安全组配置,配置结果如下图

linux下安装MongoDB
三、只监听本地接口。注释掉听在所有接口。

bind_ip = 127.0.0.1

如果只监听本地,远程连接则会报错。如下图。

linux下安装MongoDB
设置配置文件

linux下安装MongoDB

ps: 这里设置了监听所有地址,实际情况,可以自己配置监听哪些地址。
配置文件修改完成后,需要进行重启

重启

[[email protected] bin]# ps -ef|grep mongod
root       1548      1  0 14:01 ?        00:00:09 ./mongod --config ../../data/my.conf
root       1647   1278  0 14:26 pts/0    00:00:00 grep --color=auto mongod
[[email protected] bin]# kill 1548
[[email protected] bin]# ps -ef|grep mongod
root       1649   1278  0 14:27 pts/0    00:00:00 grep --color=auto mongod
[[email protected] bin]# ./mongod --config ../../data/my.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 1652
child process started successfully, parent exiting
[[email protected] bin]# 

linux下安装MongoDB
再次连接,成功。

接下来就可以愉快地的进行开发学习了。

小记

以上记录了本次安装mongodb的过程。

关于具体的mongodb操作可以另找资料参考。

如果错误,欢迎指正。