Ubuntu MongoDB 使用壓縮包配置安裝 && 使用包管理工具安裝

準備目錄結構

使用普通用戶wuyujin執行以下操作(若無則新建useradd wuyujin):

  • 下載 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.2.3.tgz
    更多版本見:mongodb 官網下載頁,選好Version, OS, Package之後就會顯示對應的下載鏈接。
  • 解壓到指定目錄下 sudo tar -zxvf mongodb-linux-x86_64-ubuntu1804-4.2.3.tgz -C /opt
  • 重命名文件夾 sudo mv /opt/mongodb-linux-x86_64-ubuntu1804-4.2.3/ /opt/mongodb423
  • 查看文件夾內容並修改文件屬主 sudo chown wuyujin -R /opt/mongodb423/
wuyujin@ubuntu18:/opt$ ll /opt/mongodb423/
total 324
drwxr-xr-x  3 root    root      4096 2月   9 16:20 ./
drwxr-xr-x 16 root    root      4096 2月   9 16:21 ../
drwxr-xr-x  2 root    root      4096 2月   9 16:20 bin/
-rw-r--r--  1 wuyujin wuyujin  30608 1月  24 13:18 LICENSE-Community.txt
-rw-r--r--  1 wuyujin wuyujin  16726 1月  24 13:18 MPL-2
-rw-r--r--  1 wuyujin wuyujin   2617 1月  24 13:18 README
-rw-r--r--  1 wuyujin wuyujin  75405 1月  24 13:18 THIRD-PARTY-NOTICES
-rw-r--r--  1 wuyujin wuyujin 183512 1月  24 13:20 THIRD-PARTY-NOTICES.gotools
wuyujin@ubuntu18:/opt$ # 修改文件夾屬主
wuyujin@ubuntu18:/opt$ sudo chown wuyujin -R /opt/mongodb423/
wuyujin@ubuntu18:/opt$ ll /opt/mongodb423/
total 324
drwxr-xr-x  3 wuyujin root      4096 2月   9 16:20 ./
drwxr-xr-x 16 root    root      4096 2月   9 16:21 ../
drwxr-xr-x  2 wuyujin root      4096 2月   9 16:20 bin/
-rw-r--r--  1 wuyujin wuyujin  30608 1月  24 13:18 LICENSE-Community.txt
-rw-r--r--  1 wuyujin wuyujin  16726 1月  24 13:18 MPL-2
-rw-r--r--  1 wuyujin wuyujin   2617 1月  24 13:18 README
-rw-r--r--  1 wuyujin wuyujin  75405 1月  24 13:18 THIRD-PARTY-NOTICES
-rw-r--r--  1 wuyujin wuyujin 183512 1月  24 13:20 THIRD-PARTY-NOTICES.gotools
wuyujin@ubuntu18:/opt$ 
  • 配置環境變量
    編輯/etc/profile,添加以下配置:
# MongoDB
export MONGO_HOME=/opt/mongodb423
export PATH=${MONGO_HOME}/bin:$PATH

重啓使最新配置對所有用戶生效。

  • 測試
wuyujin@ubuntu18:~$ echo $MONGO_HOME
/opt/mongodb423
wuyujin@ubuntu18:~$ mongo --version
MongoDB shell version v4.2.3
git version: 6874650b362138df74be53d366bbefc321ea32d4
OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1804
    distarch: x86_64
    target_arch: x86_64
wuyujin@ubuntu18:~$ mongod --version
db version v4.2.3
git version: 6874650b362138df74be53d366bbefc321ea32d4
OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1804
    distarch: x86_64
    target_arch: x86_64
wuyujin@ubuntu18:~$

直接運行

  • 新建數據文件目錄
wuyujin@ubuntu18:/opt/mongodb423$ # 新建數據文件目錄
wuyujin@ubuntu18:/opt/mongodb423$ sudo mkdir /data/db/ -p
wuyujin@ubuntu18:/opt/mongodb423$ 
wuyujin@ubuntu18:/opt/mongodb423$ ll /data
total 12
drwxr-xr-x  3 root root 4096 2月   9 17:12 ./
drwxr-xr-x 25 root root 4096 2月   9 17:12 ../
drwxr-xr-x  2 root root 4096 2月   9 17:12 db/
wuyujin@ubuntu18:/opt/mongodb423$ # 改變數據文件目錄的屬主爲本用戶(mongodb的實際操作者)
wuyujin@ubuntu18:/opt/mongodb423$ sudo chown wuyujin -R /data/db/
  • 運行服務器mongod(mongo daaemon,mongo的後臺守護進程/服務器程序)

配置

  • 查看幫助信息
    • mongod --help
      常用的參數:
      配置文件全路徑:-f [ --config ] arg Configuration file specifying additional options
      數據文件目錄:--dbpath arg Directory for datafiles - defaults to /data/db
      日誌文件全路徑:--logpath arg Log file to send write to instead of
      綁定的IP --bind_ip arg Comma separated list of ip addresses to scripts)
      端口號 --port arg Specify port number - 27017 by default cluster; default port 27019; default cluster; default port 27018
    • mongo --help
  • 新建所需目錄和文件(自己靈活運用)
cd $MONGO_HOME  # 進入mongodb主目錄
mkdir data      # 用於存放數據文件(也可以使用默認的目錄 /data/db/)
mkdir logs      # 用於存放日誌文件
mkdir conf      # 用於存放配置文件

cd conf             # 進入配置文件夾
touch mongod.conf   # 啓動mongod要讀取的配置文件
touch mongos.conf   # 啓動mongos要讀取的配置文件
  • 編輯mongod.conf,內容參照以下:
# 存儲
storage:
    dbPath: /opt/mongodb423/data/

# 網絡
net:
    bindIp: 127.0.0.1
    # 默認爲27017
    port: 11111
   
# 日誌
systemLog:
    destination: file
    path: /opt/mongodb423/logs/mongodb423.log
    logAppend: true

讀取該配置文件,啓動:mongod --config /opt/mongodb423/conf/mongod.conf

或者不讀取配置文件,啓動的時候手動添加參數項(如果不嫌麻煩的話):
mongod --dbpath /opt/mongodb423/data/ --bind_ip 127.0.0.1 --port 11111 --logpath /opt/mongodb423/logs/mongodb423.log

推薦準備自己的配置文件,啓動的時候讀取。

客戶端運行

  • 先啓動服務端,指定端口號 mongod --port 1234
wuyujin@ubuntu18:~$ mongod --port 1234
2020-02-09T18:14:35.177+0800 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-02-09T18:14:35.179+0800 I  CONTROL  [initandlisten] MongoDB starting : pid=9373 port=1234 dbpath=/data/db 64-bit host=ubuntu18
2020-02-09T18:14:35.179+0800 I  CONTROL  [initandlisten] db version v4.2.3
2020-02-09T18:14:35.179+0800 I  CONTROL  [initandlisten] git version: 6874650b362138df74be53d366bbefc321ea32d4
2020-02-09T18:14:35.179+0800 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
2020-02-09T18:14:35.179+0800 I  CONTROL  [initandlisten] allocator: tcmalloc
2020-02-09T18:14:35.179+0800 I  CONTROL  [initandlisten] modules: none
2020-02-09T18:14:35.179+0800 I  CONTROL  [initandlisten] build environment:
2020-02-09T18:14:35.179+0800 I  CONTROL  [initandlisten]     distmod: ubuntu1804
2020-02-09T18:14:35.179+0800 I  CONTROL  [initandlisten]     distarch: x86_64
2020-02-09T18:14:35.179+0800 I  CONTROL  [initandlisten]     target_arch: x86_64
2020-02-09T18:14:35.179+0800 I  CONTROL  [initandlisten] options: { net: { port: 1234 } }
2020-02-09T18:14:35.179+0800 I  STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2020-02-09T18:14:35.179+0800 I  STORAGE  [initandlisten] 
2020-02-09T18:14:35.180+0800 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-02-09T18:14:35.180+0800 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-02-09T18:14:35.180+0800 I  STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1408M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
2020-02-09T18:14:35.901+0800 I  STORAGE  [initandlisten] WiredTiger message [1581243275:901971][9373:0x7f858bfc1b00], txn-recover: Recovering log 10 through 11
2020-02-09T18:14:35.959+0800 I  STORAGE  [initandlisten] WiredTiger message [1581243275:959298][9373:0x7f858bfc1b00], txn-recover: Recovering log 11 through 11
2020-02-09T18:14:36.057+0800 I  STORAGE  [initandlisten] WiredTiger message [1581243276:57878][9373:0x7f858bfc1b00], txn-recover: Main recovery loop: starting at 10/4736 to 11/256
2020-02-09T18:14:36.172+0800 I  STORAGE  [initandlisten] WiredTiger message [1581243276:172388][9373:0x7f858bfc1b00], txn-recover: Recovering log 10 through 11
2020-02-09T18:14:36.239+0800 I  STORAGE  [initandlisten] WiredTiger message [1581243276:239370][9373:0x7f858bfc1b00], txn-recover: Recovering log 11 through 11
2020-02-09T18:14:36.294+0800 I  STORAGE  [initandlisten] WiredTiger message [1581243276:294157][9373:0x7f858bfc1b00], txn-recover: Set global recovery timestamp: (0, 0)
2020-02-09T18:14:36.316+0800 I  RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2020-02-09T18:14:36.323+0800 I  STORAGE  [initandlisten] Timestamp monitor starting
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] 
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] 
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] 
2020-02-09T18:14:36.330+0800 I  SHARDING [initandlisten] Marking collection local.system.replset as collection version: <unsharded>
2020-02-09T18:14:36.333+0800 I  STORAGE  [initandlisten] Flow Control is enabled on this deployment.
2020-02-09T18:14:36.334+0800 I  SHARDING [initandlisten] Marking collection admin.system.roles as collection version: <unsharded>
2020-02-09T18:14:36.334+0800 I  SHARDING [initandlisten] Marking collection admin.system.version as collection version: <unsharded>
2020-02-09T18:14:36.336+0800 I  SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>
2020-02-09T18:14:36.337+0800 I  FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2020-02-09T18:14:36.338+0800 I  SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version: <unsharded>
2020-02-09T18:14:36.338+0800 I  NETWORK  [listener] Listening on /tmp/mongodb-1234.sock
2020-02-09T18:14:36.338+0800 I  NETWORK  [listener] Listening on 127.0.0.1
2020-02-09T18:14:36.338+0800 I  NETWORK  [listener] waiting for connections on port 1234
2020-02-09T18:14:36.339+0800 I  SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version: <unsharded>
2020-02-09T18:14:37.001+0800 I  SHARDING [ftdc] Marking collection local.oplog.rs as collection version: <unsharded>

啓動成功。

  • 啓動客戶端
    用法:mongo [options] [db address] [file names (ending in .js)]
    db地址可以爲:
foo                   foo database on local machine
192.168.0.5/foo       foo database on 192.168.0.5 machine
192.168.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999
mongodb://192.168.0.5:9999/foo  connection string URI can also be used

啓動客戶端,連接指定的服務器

wuyujin@ubuntu18:~$ mongo mongodb://127.0.0.1:1234/
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:1234/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f21e93eb-7281-4f81-b70d-919fa1868aff") }
MongoDB server version: 4.2.3
Server has startup warnings: 
2020-02-09T18:14:35.179+0800 I  STORAGE  [initandlisten] 
2020-02-09T18:14:35.180+0800 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-02-09T18:14:35.180+0800 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] 
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] 
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2020-02-09T18:14:36.327+0800 I  CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2020-02-09T18:14:36.327+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()
---

> db.wuyujin.insert({x:10, y:20})
WriteResult({ "nInserted" : 1 })
> db.wuyujin.find()
{ "_id" : ObjectId("5e3fdbc9c4157470d1026594"), "x" : 10, "y" : 20 }
> exit
bye
wuyujin@ubuntu18:~$ 

執行的db.wuyujin.insert({x:10, y:20})用於插入一條JSON數據,
db.wuyujin.find()用於查找wuyujin中的數據。
詳細API見官網

  • 查看服務端輸出
    剛纔在客戶端做了一些操作,看服務端輸出有什麼變化。
    多出的輸出如下:
2020-02-09T18:14:47.649+0800 I  NETWORK  [listener] connection accepted from 127.0.0.1:38576 #1 (1 connection now open)
2020-02-09T18:14:47.649+0800 I  NETWORK  [conn1] received client metadata from 127.0.0.1:38576 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.2.3" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "18.04" } }
2020-02-09T18:15:00.155+0800 I  NETWORK  [listener] connection accepted from 127.0.0.1:38580 #2 (2 connections now open)
2020-02-09T18:15:00.155+0800 I  NETWORK  [listener] connection accepted from 127.0.0.1:38584 #3 (3 connections now open)
2020-02-09T18:15:00.161+0800 I  NETWORK  [conn2] Error receiving request from client: ProtocolError: Client sent an HTTP request over a native MongoDB connection. Ending connection from 127.0.0.1:38580 (connection id: 2)
2020-02-09T18:15:00.161+0800 I  NETWORK  [conn2] end connection 127.0.0.1:38580 (2 connections now open)
2020-02-09T18:15:00.230+0800 I  NETWORK  [conn3] Error receiving request from client: ProtocolError: Client sent an HTTP request over a native MongoDB connection. Ending connection from 127.0.0.1:38584 (connection id: 3)
2020-02-09T18:15:00.230+0800 I  NETWORK  [conn3] end connection 127.0.0.1:38584 (1 connection now open)
2020-02-09T18:15:37.786+0800 I  SHARDING [conn1] Marking collection test.wuyujin as collection version: <unsharded>
2020-02-09T18:15:37.786+0800 I  STORAGE  [conn1] createCollection: test.wuyujin with generated UUID: 3265b4b0-813c-4626-bdd4-cae6f2252058 and options: {}
2020-02-09T18:15:37.807+0800 I  INDEX    [conn1] index build: done building index _id_ on ns test.wuyujin
2020-02-09T18:16:24.505+0800 I  NETWORK  [conn1] end connection 127.0.0.1:38576 (0 connections now open)

使用包管理工具快速安裝

  • 查詢包名
wuyujin@ubuntu18:~$ apt-cache search mongodb | grep mongodb
...省略部分行
mongodb - object/document-oriented database (metapackage)
mongodb-clients - object/document-oriented database (client apps)
mongodb-dev - MongoDB C++ Driver (transitional package)
mongodb-server - object/document-oriented database (managed server package)
mongodb-server-core - object/document-oriented database (server binaries package)
php-mongodb - MongoDB driver for PHP
...省略部分行
wuyujin@ubuntu18:~$ 
發佈了283 篇原創文章 · 獲贊 156 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章