MongoDB是一個通用的、基於文檔的分布式數據庫,它是為現代應用程序開發人員和雲時代而構建的。沒有數據庫能讓你更有效率。
1.下載需要的軟件包
https://www.mongodb.com/download-center/community
這個是社區版本,按照自己的需求,選擇自己需要的版本
進行解壓 tar -xzvf mongodb-linux-x86_64-rhel70-4.2.1.tgz
重命名 mv mongodb-linux-x86_64-rhel70-4.2.1 mongodb
置系統環境變量 vim /etc/profile
export PATH=$PATH:/root/mongodb/bin
使生效 source /etc/profile
創建文件夾:
[root@master2 mongodb]# pwd
/root/mongodb/mongodb
[root@master2 mongodb]# mkdir db logs
創建MongoDB運行時使用的配置文件 vim mongodb.conf
dbpath = /root/mongodb/db #數據文件存放目錄
logpath =/root/mongodb/logs/mongodb.log #日志文件存放目錄
port = 27017 #端口
fork = true #以守護程序的方式啟用,即在后台運行
#auth=true #需要認證。如果放開注釋,就必須創建MongoDB的賬號,使用賬號與密碼才可遠程訪問,第一次安裝建議注釋
bind_ip=0.0.0.0 #允許遠程訪問,或者直接注釋,127.0.0.1是只允許本地訪問
啟動MongoDB
[root@master2 bin]# ./mongod -f mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4858
child process started successfully, parent exiting
關閉MongoDB
[root@master2 bin]# mongod -f mongodb.conf --shutdown
2019-11-29T22:27:10.645+0800 I CONTROL [main] log file "/root/mongodb/logs/mongodb.log" exists; moved to "/root/mongodb/logs/mongodb.log.2019-11-29T14-27-10".
killing process with pid: 4858
創建MongoDB的賬號
[root@master2 bin]# mongo
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3e1cfaee-6dc4-460b-b986-661b11f7cbc3") }
MongoDB server version: 4.2.1
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten]
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten]
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten]
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten]
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten]
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 7812 processes, 204800 files. Number of processes should be at least 102400 : 0.5 times number of files.
2019-11-29T22:31:57.430+0800 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
> use admin
switched to db admin
> show users
> show collections
system.version
> db.createUser({ user: 'root', pwd: 'root',roles: [ { role: "dbOwner", db: "test" }] })
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "dbOwner",
"db" : "test"
}
]
}
> db.auth("root","root")
1
> use test
switched to db test
> db.firstCollection.save({"name":"yzh","age":"25"})
WriteResult({ "nInserted" : 1 })
> db.firstCollection.find({name:"yzh"})
{ "_id" : ObjectId("5de12c9c771bf721cc7a0b07"), "name" : "yzh", "age" : "25" }
>
至此,MongoDB 創建完成!