一、安装
centos版本:CentOS Linux release 7.4.1708 (Core)
mango版本:mongodb-linux-x86_64-3.6.5.tgz
我直接在root用户下安装的,无需创建用户。
1.将tar包放到/usr/src下,解压
tar -xzvf mongodb-linux-x86_64-3.6.5.tgz
2.创建软连接
ln -s mongodb-linux-x86_64-3.6.5/ mongodb
在/usr/src/mongodb/bin目录下可看到有可执行程序。
3.配置环境变量
编辑配置文件
vi /etc/profile
在文件最后添加如下代码:
export PATH=/usr/src/mongodb/bin:$PATH
保存后刷新环境变量
source /etc/profile
可以使用mongo -version检查环境变量是否生效。
到此安装结束。
二、启动方式及配置
在/usr/src/目录下新建一个名为mongoData的文件夹,用来存放数据库。
方式一:无配置文件模式
直接使用
mongod --dbpath /usr/src/mongoData/ --logpath /usr/src/mongoData/logs/mongodb.log --logappend -fork
参数
方式二:配置文件模式
在/etc/目录下增加配置文件mongo.conf
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /usr/src/mongoData
journal:
enabled: true
directoryPerDB: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/share/logs/mongodb/mongodb.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
fork: true
pidFilePath: /run/data0802.pid
timeZoneInfo: /usr/share/zoneinfo
# set auth
setParameter:
enableLocalhostAuthBypass: false
security:
authorization: enabled
# authorization: disabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
security: authorization: 是否开启认证模式,true 则需要认证后才能访问数据库。
启动:
mongod -f /etc/mongodb.conf
停止
除了使用–shutdown还可以使用kill杀掉进程,注意不要使用-9强杀
三、常用命令
登录:
查看数据库版本
> db.version()
3.4.13
切换数据库
> use test;
switched to db test
> db
test
查看所有数据库
> show dbs;
admin 0.000GB
local 0.000GB
> show databases;
admin 0.000GB
local 0.000GB
查看集合
> show collections
apireqreslogschemas
testcollection
userinfoschemas’
查看集合内容
> db.userinfoschemas.find()
备份
常用的是:mongodump -h 主机 -u 用户名 -p 密码 -d 数据库 -c 集合 -o 备份目录
mongodump -h 127.0.0.1 -d stWebsiteDb -o /usr/src/stWebsiteDb
在/usr/src/stWebsiteDb 可以看到备份文件
mongodb恢复时采用mongorestore来恢复
图形化工具
NoSQLBooster
官网下载:https://nosqlbooster.com/
后记
安装过程中参考的资料,比我写的详细啊:
CentOS7安装MongoDB及基础操作
MongoDB备份与恢复(linux命令行实现)