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:~$ 
发布了269 篇原创文章 · 获赞 156 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/wuyujin1997/article/details/104238719