1.mongodb下载
通过在网页上选择对应的版本下载:
https://www.mongodb.org/downloads#production
目前mongodb支持的Linux版本如图所示,对于Centos笔者没有尝试红帽的版本(应该可以使用)。
本次下载使用版本为legacy版本,下载下来后,对于window导入虚拟机中可使用xftp,对于mac导入虚拟机,笔者使用的是filezilla,
导入过程略,下面开始讲解正式安装。
2.mongodb安装
首先使用tar命令进行解压,命令如下:
tar -zxvf mongodb-linux-x86_64-4.0.2.tgz
解压后,查看解压好的文件夹,可以发现bin文件夹
此时,我们在root用户下,可以使用chmod对bin进行递归权限修改,实验环境下,我们可以给满权限使用,如图所示:
修改完成后,我们需要在本地创建一个存放操作日志和数据文件的文件夹,然后给文件夹赋予最大权限。
[root@hadoop1 data]# mkdir -p /data/mongo_data
[root@hadoop1 data]# chmod -R 777 /data/
3.编写配置文件
直接在刚才的bin目录下使用vim编辑器编写mongodb.conf配置文件。
port = 27017
dbpath = /data/mongo_data
logpath = /data/logs
logappend = true
fork = true
fork代表开启守护进程,守护进程需要依赖日志文件,这里的日志文件是系统自动生成的,所以在配置文件中指定日志地址。
4.配置mongodb环境变量
为了方便以后启动mongodb,我们可以配置环境变量。使用命令:
vim ~/.bash_profile
环境变量配置命令如下:
Mongo_HOME=/hadoop/soft/mongodb-linux-x86_64-4.0.2/
PATH=$Mongo_HOME/bin
export PATH
然后我们在bin目录下调用我们的配置文件,命令如下:
[root@hadoop1 bin]# mongod -f ./mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3040
child process started successfully, parent exiting
系统提示,启动成功。我们通过mongo命令可以开始使用mongodb
[root@hadoop1 bin]# mongo
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2
Welcome to the MongoDB shell.
5.开机自启动mongodb
接下来,我们可以创建mongodb.service启动文件使系统开机自启动mongodb。
[root@hadoop1 system]# cd /lib/systemd/system
[root@hadoop1 system]# vim mongodb.service
具体配置文件如下:
[unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/hadoop/soft/mongodb-linux-x86_64-4.0.2/bin/mongod -f /hadoop/soft/mongodb-linux-x86_64-4.0.2/bin/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/hadoop/soft/mongodb-linux-x86_64-4.0.2/bin/mongod --shutdown -f /hadoop/soft/mongodb-linux-x86_64-4.0.2/bin/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
注意,有很多朋友在这个地方报错哦!
根本原因现在的27017端口被占用了,需要先把服务关了!!!
使用mongo登陆,然后代码如下,可关闭mongodb
2018-09-09T15:07:45.223+0800 I CONTROL [initandlisten]
> use admin
switched to db admin
> db.shutdownServer()
server should be down...
之后再关就可以了,操作如下:
[root@hadoop1 system]# systemctl start mongodb.service
Warning: mongodb.service changed on disk. Run 'systemctl daemon-reload' to reload units.
之后将mongodb注册到开机启动目录下,命令如下:
systemctl enable mongodb.service
我们可以使用:shutdown -r now进行测试看看啦!!!:)