1. 程式人生 > >初學MonggoDb—Linux平臺安裝MongoDB

初學MonggoDb—Linux平臺安裝MongoDB

 參考資料:http://www.runoob.com/mongodb

也是第一次接觸mongodb資料庫,那麼什麼是mongodb資料庫?

下面引用相關文件的介紹:

什麼是MongoDB ?

MongoDB 是由C++語言編寫的,是一個基於分散式檔案儲存的開源資料庫系統。

在高負載的情況下,新增更多的節點,可以保證伺服器效能。

MongoDB 旨在為WEB應用提供可擴充套件的高效能資料儲存解決方案。

MongoDB 將資料儲存為一個文件,資料結構由鍵值(key=>value)對組成。MongoDB 文件類似於 JSON 物件。欄位值可以包含其他文件,陣列及文件陣列。

主要特點:

  • MongoDB 是一個面向文件儲存的資料庫,操作起來比較簡單和容易。
  • 你可以在MongoDB記錄中設定任何屬性的索引 (如:FirstName="Sameer",Address="8 Gandhi Road")來實現更快的排序。
  • 你可以通過本地或者網路建立資料映象,這使得MongoDB有更強的擴充套件性。
  • 如果負載的增加(需要更多的儲存空間和更強的處理能力) ,它可以分佈在計算機網路中的其他節點上這就是所謂的分片。
  • Mongo支援豐富的查詢表示式。查詢指令使用JSON形式的標記,可輕易查詢文件中內嵌的物件及陣列。
  • MongoDb 使用update()命令可以實現替換完成的文件(資料)或者一些指定的資料欄位 。
  • Mongodb中的Map/reduce主要是用來對資料進行批量處理和聚合操作。
  • Map和Reduce。Map函式呼叫emit(key,value)遍歷集合中所有的記錄,將key與value傳給Reduce函式進行處理。
  • Map函式和Reduce函式是使用Javascript編寫的,並可以通過db.runCommand或mapreduce命令來執行MapReduce操作。
  • GridFS是MongoDB中的一個內建功能,可以用於存放大量小檔案。
  • MongoDB允許在服務端執行指令碼,可以用Javascript編寫某個函式,直接在服務端執行,也可以把函式的定義儲存在服務端,下次直接呼叫即可。
  • MongoDB支援各種程式語言:RUBY,PYTHON,JAVA,C++,PHP,C#等多種語言。
  • MongoDB安裝簡單

學習任何資料庫都是從安裝開始,下面就一起來看看mongodb資料庫是如何安裝的

1.MongoDB 提供了 linux 各發行版本 64 位的安裝包,你可以在官網下載安裝包。

下載地址:https://www.mongodb.com/download-center#community

下載完安裝包,並解壓 tgz(以下演示的是 64 位 Linux上的安裝) 。

#下載
[[email protected] opt]# curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.4.tgz 
#解壓
[[email protected] opt]# tar -zxvf mongodb-linux-x86_64-4.0.4.tgz
#將解壓包拷貝到指定目錄
[[email protected] opt]# mv mongodb-linux-x86_64-4.0.4 /usr/local/mongodb

MongoDB 的可執行檔案位於 bin 目錄下,所以可以將其新增到 PATH 路徑中:


[[email protected] ~]# cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

MONGONDB=/usr/local/mongodb
PATH=$MONGONDB/bin:$PATH:$HOME/bin
export PATH

[[email protected] ~]# source .bash_profile

MONGODB 為你 MongoDB 的安裝路徑。如本文的 /usr/local/mongodb

2.建立資料庫目錄和日誌目錄:

MongoDB的資料儲存在data目錄的db目錄下,但是這個目錄在安裝過程不會自動建立,所以你需要手動建立data目錄,並在data目錄中建立db目錄。


[[email protected] ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda2        36G   18G   17G  52% /
tmpfs           1.9G     0  1.9G   0% /dev/shm
/dev/vdb1        30G   12G   17G  42% /data
[[email protected] ~]# cd /data/mongodb/
[[email protected] mongodb]# pwd
/data/mongodb
[[email protected] mongodb]# mkdir data logs

 

注意:/data/db 是 MongoDB 預設的啟動的資料庫路徑(--dbpath)。這裡我們不使用預設的路徑,自己建立個/data/mongodb/data目錄

3.建立配置檔案


[[email protected] mongodb]# cat mongodb.conf
#資料庫檔案位置
dbpath=/data/mongodb/data
#日誌檔案位置
logpath=/data/mongodb/logs/mongodb.log
#以追加方式寫入日誌
logappend=true
#預設27017
port=27017
#設定後臺執行
fork=true

3.命令列中執行MongoDb服務

你可以再命令列中執行mongo安裝目錄中的bin目錄執行mongod命令來啟動mongdb服務。

[[email protected] bin]# ./mongod --config /data/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 22315
child process started successfully, parent exiting

到此mongdb就已經安裝完成了,是不是so easy!!

4.嘗試登陸mongodb

臥槽,上面出現這麼多的Waring,不過不影響使用,但是看起來有點彆扭,下面將一一介紹這些告警資訊啥意思

#首先檢視mongdb服務是否真的起來
[[email protected] ~]# ps -ef |grep mongod
root     22315     1  0 10:49 ?        00:00:02 ./mongod --config /data/mongodb/mongodb.conf
root     22453 22275  0 11:03 pts/1    00:00:00 grep mongod
#登陸
[[email protected] ~]# mongo
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("3a2fe154-6b5d-4f11-bb28-b69e40887105") }
MongoDB server version: 4.0.4
Server has startup warnings:
2018-11-20T10:49:39.628+0800 I STORAGE  [initandlisten]
2018-11-20T10:49:39.628+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-11-20T10:49:39.628+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten]
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten]
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten]
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten]
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten]
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-11-20T10:49:40.346+0800 I CONTROL  [initandlisten]
>

 

第一個: WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
意思是:強烈建議使用帶WiredTiger儲存引擎的XFS檔案系統
XFS檔案系統:XFS一種高效能的日誌檔案系統,最早於1993年,由Silicon Graphics為他們的IRIX作業系統而開發,
是IRIX 5.3版的預設檔案系統。2000年5月,Silicon Graphics以GNU通用公共許可證釋出這套系統的原始碼,之後
被移植到Linux 核心上。XFS 特別擅長處理大檔案,同時提供平滑的資料傳輸。
既然是warning說明不會影響mongodb的正常使用,只是提醒你安裝mongodb最好使用ext4或者xfs檔案系統!
本人系統已經是ext4系統了!
第二個: WARNING: Access control is not enabled for the database.
意思是:未對資料庫啟用訪問控制,對資料和配置的讀寫訪問不受限制(危險的warning)

解決辦法:
建立管理員並設定密碼

> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
> use admin
switched to db admin
> db.createUser(
...   {
...     user: "yuhuashi",
...     pwd: "yuhuashi",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
...   }
...   )
Successfully added user: {
        "user" : "yuhuashi",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}


然後重啟資料庫伺服器
[[email protected] ~]# ps -ef |grep mongo
root     22556     1  2 11:20 ?        00:00:01 mongod --config /data/mongodb/mongodb.conf
root     22591 22524  0 11:21 pts/0    00:00:00 grep mongo
[[email protected] ~]# kill -9 22556
[[email protected] ~]# ps -ef |grep mongo
root     22593 22524  0 11:21 pts/0    00:00:00 grep mongo
[[email protected] ~]# mongod --auth --config /data/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 22596
child process started successfully, parent exiting
[[email protected] ~]# mongo
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("b60eca6a-0a2d-4399-9b8f-8a3f442f39b2") }
MongoDB server version: 4.0.4
>

 

第三個:WARNING: You are running this process as the root user, which is not recommended.
意思就是不推薦使用root使用者來啟用mongdb,不好意思哥哥是測試環境,你愛咋咋的

 

第四個: WARNING: This server is bound to localhost.

解決辦法,請看!!
Remote systems will be unable to connect to this server.
2018-11-20T11:23:39.317+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-11-20T11:23:39.317+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-11-20T11:23:39.317+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-11-20T11:23:39.317+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-11-20T11:23:39.317+0800 I CONTROL  [initandlisten]

哥哥聰明吧

 

第五個報錯: WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
需要將/sys/kernel/mm/transparent_hugepage/enabled設定為never
解決辦法就是設定never

[[email protected] ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
[[email protected] ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[[email protected] ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]

 

第六個報錯: WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
需要將/sys/kernel/mm/transparent_hugepage/defrag設定為never
解決辦法就是設定never

[[email protected] ~]# cat /sys/kernel/mm/transparent_hugepage/defrag
[always] madvise never
[[email protected] ~]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
[[email protected] ~]# cat /sys/kernel/mm/transparent_hugepage/defrag
always madvise [never]

 mongodb後臺管理shell

如果你需要進入MongoDB後臺管理,你需要先開啟mongodb裝目錄的下的bin目錄,然後執行mongo命令檔案。

MongoDB Shell是MongoDB自帶的互動式Javascript shell,用來對MongoDB進行操作和管理的互動式環境。

當你進入mongoDB後臺後,它預設會連結到 test 文件(資料庫):

> 2+2
4
> 3+6
9

現在測試插入一些簡單的資料,並對插入的資料進行檢索:


> use yuhuashi
switched to db yuhuashi
> db.yuhuashi.insert({"name":"yuhuashi"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin     0.000GB
config    0.000GB
local     0.000GB
yuhuashi  0.000GB

> db.yuhuashi.find({"name":"yuhuashi"})
{ "_id" : ObjectId("5bf3b67e01e6889d06eab63e"), "name" : "yuhuashi" }