Linux MongoDB安装
1、下载MongoDB安装包
MongoDB安装文件mongdb-linux-4.2.10.tar.gz下载地址(MongoDB版本4.2.10)
或者前往mirrors.aliyun.com/mongodb自己选择下载
下载链接http://mirrors.aliyun.com/mongodb/yum/redhat/8Server/mongodb-org/4.2/x86_64/RPMS/
2、解压
tar -zxvf mongdb-linux-4.2.10.tar.gz
3、安装
# cd mongdb
# rpm -ivh mongodb-org-mongos-4.2.10-1.el8.x86_64.rpm
# rpm -ivh mongodb-org-server-4.2.10-1.el8.x86_64.rpm
# rpm -ivh mongodb-org-server-4.2.10-1.el8.x86_64.rpm
# rpm -ivh mongodb-org-shell-4.2.10-1.el8.x86_64.rpm
# rpm -ivh mongodb-org-tools-4.2.10-1.el8.x86_64.rpm
这里安装时,可能出现错误提示:warning: mongodb-org-server-4.2.10-1.el8.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID 058f8b6b: NOKEY
解决办法:后面加上 --force --nodeps
# rpm -ivh mongodb-org-server-4.2.10-1.el8.x86_64.rpm --force --nodeps
4、修改配置文件
// 编辑mongod.conf
# vim /etc/mongod.conf
// 以下是mongod.conf文件内容
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
修改bindIp,将127.0.0.1
修改为0.0.0.0
,保存退出;
这样修改后,其他的IP就可以远程连接MongoDB。
MongoDB启动命令
启动:service mongod start
查看状态:service mongod status
停止:service mongod stop
重启:service mongod restart
创建用户
> mongo
> use admin;
> db.createUser({user:"admin",pwd:"admin",roles:[{role:"root",db:"admin"}]});
创建索引
> db.collectionName.createIndex(keys, options)
其中,collectionName表示要为其创建索引的集合名称;keys指定了要创建索引的字段及顺序;options是可选参数,用于设置索引的属性,1表示升序,-1表示降序。
举个例子,我们有一个名为"users"的集合,想要对该集合的"username"字段进行升序索引,可以这样写:
> db.users.createIndex({ username: 1 })