linux系统安装MongoDB

Posted 懒惰虫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux系统安装MongoDB相关的知识,希望对你有一定的参考价值。

1.在官网https://www.mongodb.org/dl/linux/x86_64下载对应的安装压缩包,我用的是3.2.5,建议使用此版本,版本越高,可能会出现其它未知问题,比如需要glibc高版本,通过

ll /lib64/libc.so.6能查看本机版本为/lib64/libc.so.6 -> libc-2.17.so,安装一次过。

2.把压缩包放在/usr/download目录下,进入download目录,解压,tar -zxvf mongodb-linux-x86_64-3.2.5.tgz。

3.把文件夹复制到/usr/local/mongodb目录下,cp -r mongodb-linux-x86_64-3.2.5 /usr/local/mongodb。

4.编辑环境变量信息,sudo vi /etc/profile,在里面加上下面的内容,然后source /etc/profile。

export MONGODB_HOME=/usr/local/mongodb/mongodb-linux-x86_64-3.2.5
export PATH=$PATH:$MONGODB_HOME/bin

5.进入/usr/local/mongodb/mongodb-linux-x86_64-3.2.5,按顺序执行如下命令。

sudo mkdir -p data/db

sudo chmod -r 777 data/db

sudo mkdir logs

cd logs

touch mongodb.log

6.进入/usr/local/mongodb/mongodb-linux-x86_64-3.2.5/bin,执行sudo vi mongodb.conf,里面添加如下内容。

dbpath = /usr/local/mongodb/mongodb-linux-x86_64-3.2.5/data/db #数据文件存放目录
logpath = /usr/local/mongodb/mongodb-linux-x86_64-3.2.5/logs/mongodb.log #日志文件存放目录
port = 27017  #端口
fork = true  #以守护程序的方式启用,即在后台运行
nohttpinterface = true

7.在bin目录下执行./mongod -f mongodb.conf以配置文件的方式启动MongoDB数据库服务。

8.执行./mongo,可以连接MongoDB数据库。

MongoDB shell version: 3.2.5
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] ** WARNING: Insecure configuration, access control is not enabled and no --bind_ip has been specified.
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted, 
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] **          and the server listens on all available network interfaces.
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is \'always\'.
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] **        We suggest setting it to \'never\'
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is \'always\'.
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] **        We suggest setting it to \'never\'
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] 
>

9.设置mongodb.service启动服务,设置开机启动,按顺序执行如下命令。

cd /lib/systemd/system

sudo vi mongodb.service

里面的内容添加如下内容,然后保存。

[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/mongodb/mongodb-linux-x86_64-3.2.5/bin/mongod --config /usr/local/mongodb/mongodb-linux-x86_64-3.2.5/bin/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/mongodb-linux-x86_64-3.2.5/bin/mongod --shutdown --config /usr/local/mongodb/mongodb-linux-x86_64-3.2.5/bin/mongodb.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

10.授权chmod 754 mongodb.service。

11.系统mongodb.service的操作命令如下。

#启动服务  
systemctl start mongodb.service  
#关闭服务  
systemctl stop mongodb.service  
#开机启动  
systemctl enable mongodb.service

12.你会发现执行systemctl start mongodb.service时会有如下内容,没关系,不影响,没有更好。

Job for mongodb.service failed because the control process exited with error code. See "systemctl status mongodb.service" and "journalctl -xe" for details.

13.执行cd /usr/local/mongodb/mongodb-linux-x86_64-3.2.5/bin/到bin目录下,执行./mongo,出现如下画面,表示MongoDB数据库启动成功,可以去连接做任何操作了。

MongoDB shell version: 3.2.5
connecting to: test
Server has startup warnings: 
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] ** WARNING: Insecure configuration, access control is not enabled and no --bind_ip has been specified.
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted, 
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] **          and the server listens on all available network interfaces.
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is \'always\'.
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] **        We suggest setting it to \'never\'
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is \'always\'.
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] **        We suggest setting it to \'never\'
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] 
>

14.下载NoSQL Manager for MongoDB,输入IP和端口(默认是27017),就可以连接进去,做任何你之前在oracle或者mysql的任何操作了,但是,语法不一样。

 

以上是关于linux系统安装MongoDB的主要内容,如果未能解决你的问题,请参考以下文章

Linux安装MongoDB5.0.7的流程Linux系统下安装mongoDB5.0.7并且使用MongoDBCompass进行远程连接测试

Linux系统安装NoSQL(MongoDB和Redis)步骤及问题解决办法

Linux安装MongoDB5.0.7的流程Linux系统下安装mongoDB5.0.7并且使用MongoDBCompass进行远程连接测试MongoDB设置账号密码验证登陆

如何在arch linux系统中安装MongoDB

在Linux系统下安装MongoDB数据库

Mongodb安装必读之系统优化