从官网下载MongoDB最新版
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.4.tgz
如果是选择其他版本,则在如下页面选择对应版本然后复制连接即可:
https://www.mongodb.com/try/download/community
上面的这个copy link 有时候不会显示出来,多刷新几次就好了
tar zxvf mongodb-linux-x86_64-rhel70-4.4.4.tgz -C /data/software/mongodb/
mv mongodb-linux-x86_64-rhel70-4.4.4 mongodb-4.4.4
# [cdh-test02 bigdata 18:21:38] [Wed Feb 24] /data/software/mongodb/mongodb-4.4.4/bin
$ ll
total 195604
-rwxr-xr-x 1 bigdata bigdata 7684 Feb 12 06:37 install_compass
-rwxr-xr-x 1 bigdata bigdata 53431320 Feb 12 06:27 mongo
-rwxr-xr-x 1 bigdata bigdata 82400432 Feb 12 06:28 mongod
-rwxr-xr-x 1 bigdata bigdata 64440352 Feb 12 06:17 mongos
创建数据及日志文件夹:
mkdir /data/software/mongodb/mongodb-4.4.4/data/
mkdir /data/software/mongodb/mongodb-4.4.4/log/
新建mongodb.conf文件:
vim mongodb.conf
# 数据库存放目录
dbpath=/data/software/mongodb/mongodb-4.4.4/data/
# 日志文件路径:
logpath=/data/software/mongodb/mongodb-4.4.4/log/mongodb.log
# 是否追加日志
logappend=true
# 端口
port=27017
# 是否后台程序启动
fork=true
# 是否启动授权认证
auth=true
添加环境变量
sudo vim /etc/profile
启动MongoDB:
mongod --config /data/software/mongodb/mongodb-4.4.4/bin/mongodb.conf
查看服务是否正常:
ps -ef | grep mongodb
bigdata 13674 1 3 18:27 ? 00:00:00 mongod --config /data/software/mongodb/mongodb-4.4.4/bin/mongodb.conf
启动命令行客户端:
$ mongo
MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f374bb47-a958-43a4-89ec-96a537c77dfc") }
MongoDB server version: 4.4.4
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
> show tables
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> show dbs
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.mycoll.find() list objects in collection mycoll
db.mycoll.find( { a : 1 } ) list objects in mycoll where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
> show collections
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> show dbs
> use test
switched to db test
> db
test
> exit
bye
开启用户名和密码认证
$ mongo
MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("495593ff-f8fb-4d6e-ae86-64d78b914800") }
MongoDB server version: 4.4.4
> use admin
switched to db admin
### 因为没有认证所以show users 会报错
> show users
uncaught exception: Error: not authorized on admin to execute command { usersInfo: 1.0, lsid: { id: UUID("495593ff-f8fb-4d6e-ae86-64d78b914800") }, $db: "admin" } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1659:15
shellHelper.show@src/mongo/shell/utils.js:914:9
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1
## 创建一个用户名密码都为root的用户
> db.createUser({user:"root",pwd:"root",roles:["root"]})
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
> show users
uncaught exception: Error: command usersInfo requires authentication :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1659:15
shellHelper.show@src/mongo/shell/utils.js:914:9
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1
### 用最新创建的用户名密码进行认证
> db.auth('root','root')
1
> show users
{
"_id" : "admin.root",
"userId" : UUID("2fa4d904-544e-4902-9104-91d091a8e736"),
"user" : "root",
"db" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}