Installing the configuration MongoDB database
Lab Environment:
System Environment : Rhel6 x64 SELinux and iptables disabled
First, install MongoDB
:
Https://www.mongodb.com/download-center#community
[[email protected] ~]# ls
mongodb-linux-x86_64-rhel62-4.0.0
[email protected] ~]# mv mongodb-linux-x86_64-rhel62-4.0.0//usr/local/mongodb #移动解压文件夹到MongoDB安装目录
[email protected] ~]# mkdir-p/home/data/mongodb/mongodb_data #创建MongoDB数据库存放路径
[email protected] ~]# mkdir-p/home/data/mongodb/mongodb_log #创建MongoDB数据库日志存放路径
#启动MongoDB
[Email protected] ~]#/usr/local/mongodb/bin/mongod--port 27017--fork--dbpath=/home/data/mongodb/mongodb_data/-- Logpath=/home/data/mongodb/mongodb_log/mongodb.log--logappend
View ports
[Email protected] ~]# NETSTAT-ANTLP #查看MongoDB是否启动
[Email protected] ~]# cd/usr/local/mongodb/bin/
[Email protected] bin]#/mongo #进入MongoDB数据库控制台
> 2+3
5
> 2*3
6
>
> Use admin #进入admin数据库
Switched to DB admin
> Db.shutdownserver () #关闭MongoDB数据库
Server should is down ...
2018-07-22t21:25:17.131+0800 I NETWORK [JS] trying reconnect to 127.0.0.1:27017 failed
2018-07-22t21:25:17.131+0800 I NETWORK [JS] Reconnect 127.0.0.1:27017 failed failed
> Exit #退出
Bye
[Email protected] bin]# NETSTAT-ANTLP | GREP:27017 is closed.
[Email protected] bin]#
Once again enter the MongoDB database console when the following error:
[Email protected] bin]#./mongo
MongoDB Shell version v4.0.0
Connecting to:mongodb://127.0.0.1:27017
2018-07-22t22:07:54.310+0800 E QUERY [js] error:couldn ' t connect to server 127.0.0.1:27017, connection attempt failed: Socketexception:error Connecting to 127.0.0.1:27017:: Caused by:: Connection refused:
[Email protected]/mongo/shell/mongo.js:251:13
@ (Connect): 1:6
Exception:connect failed
Solution path:
(1) Cd/usr/local/mongodb/bin
(2)./mongod--dbpath=/home/data/mongodb/mongodb_data/
(3) The previous path setting is successful, but do not close the interface, you need to reopen a new window to start MONGO
(4) At another terminal: cd/usr/local/mongodb/
./bin/mongo
When doing the following, close the MongoDB database first
Ii. setting up a MongoDB database
[Email protected] bin]# cd/usr/local/mongodb/ #进入MongoDB安装目录
[Email protected] mongodb]# vim mongodb.conf #编写配置文件
port=27017 #端口号
dbpath=/home/data/mongodb/mongodb_data/ #数据库路径
Logpath=/home/data/mongodb/mongodb_log/mongodb.log #日志输出文件路径
Logappend=true #日志输出方式
[Email protected] ~]# cd/usr/local/mongodb/
[Email protected] bin]# pwd
/usr/local/mongodb/bin
[Email protected] bin]#/mongod-f mongodb.conf
To view ports:
Third, the Linux registered MongoDB service
Enter the/etc/init.d/directory
[Email protected] bin]# cd/etc/init.d/
Create a MongoDB file
[[email protected] init.d]# Vim MongoDB
#!/bin/sh
#
# # # # BEGIN INIT INFO
# Short-description:mongodb
# Description:mongo DB Server
# # # END INIT INFO
Program=/usr/local/mongodb/bin/mongod
Mongopid= ' Ps-ef | grep ' Mongod ' | Grep-v grep | awk ' {print $} '
Test-x $PROGRAM | | Exit 0
Case "$" in
Start
Ulimit-n 3000
echo "Starting MongoDB server"
$PROGRAM--fork--quiet-f/usr/local/mongodb/mongodb.conf
echo "Started MongoDB Server"
;;
Stop
echo "Stopping MongoDB server"
if [!-Z "$MONGOPID"]; Then
Kill-15 $MONGOPID
Fi
echo "Stopped MongoDB Server"
;;
Status
;;
*)
echo "Usage:mongodb {start|stop|status}"
Exit 1
Esac
Exit 0
After you have saved it, you can perform the following commands:
[Email protected] init.d]# chmod +x MongoDB
Test:
[[email protected] init.d]#/etc/init.d/mongodb start
[Email protected] init.d]# NETSTAT-ANTLP | grep:27017
Installing the Configuration MongoDB database