1、切換的src目錄,下載包文件解壓后,改名並遷移到 /usr/local/mongodb ,
創建運行mongodb的用戶、組,創建存放數據、日、運行pid的目錄,並改成所屬用戶、組為mongodb
cd /usr/local/src
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.0.tgz
tar xvzf mongodb-linux-x86_64-rhel70-4.2.0.tgz
mv mongodb-linux-x86_64-rhel70-4.2.0 /usr/local/mongodb
cd /usr/local/mongodb
groupadd mongodb
useradd -s /sbin/nologin -g mongodb -M mongodb
mkdir data log run
chown -R mongodb:mongodb data log run
2、在/usr/local/mongodb 里面創建一個配置文件 mongodb.conf
vi mongodb.conf 並寫入下面的信息:
bind_ip=0.0.0.0
port=27017
dbpath=/usr/local/mongodb/data/
logpath=/usr/local/mongodb/log/mongodb.log
pidfilepath =/usr/local/mongodb/run/mongodb.pid
logappend=true
fork=true
maxConns=5000
noauth = true
3、注冊服務 vi /usr/lib/systemd/system/mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/mongodb.conf
PrivateTmp=
true
[Install]
WantedBy=multi-user.target
4、這是開機啟動,並啟動。
systemctl daemon-reload
systemctl enable mongodb
systemctl start mongodb
如果需要停止,就執行:
systemctl stop mongodb
查看狀態:
systemctl status mongodb
5、配置mongodb為環境變量,方便直接在shell中操作
vi /etc/profile
在/etc/profile文件末尾添加一行:
export PATH=/usr/local/mongodb/bin:$PATH
讓其生效:
source /etc/profile
6、查看當前mongodb的版本:
mongod --version