mongodb mac安装_在Mac OS X上安装MongoDB

本文介绍了如何在Mac OS X上安装MongoDB 3.4.7,包括下载、解压、设置数据目录以及启动服务。同时,讲解了如何将MongoDB作为Mac OS X服务运行,涉及PList文件的创建和配置。此外,文章还涵盖了MongoDB的一些基本功能和常用操作,如创建、切换数据库,保存和查询JSON文档。
摘要由CSDN通过智能技术生成

mongodb mac安装

Today we will install MongoDB on Mac OS X. MongoDB is one of the leading NoSQL database. MySql and Oracle are termed as “Relation Databases”, similarly we can say that MongoDB is a “Document Database”. We can store any valid JSON in MongoDB database. MongoDB is open source and written in C++ language.

今天,我们将在Mac OS X 的MongoDB安装MongoDB是领先的NoSQL数据库之一。 MySql和Oracle被称为“关系数据库”,类似地,我们可以说MongoDB是“文档数据库”。 我们可以在MongoDB数据库中存储任何有效的JSON。 MongoDB是开源的,并使用C ++语言编写。

MongoDB stores JSON documents in Binary format termed as BSON (Binary JSON). Some of the features of MongoDB are:

MongoDB以二进制格式(称为BSON(二进制JSON))存储JSON文档。 MongoDB的一些功能包括:

  1. Full indexing support for faster query processing.

    全面的索引支持,可加快查询处理速度。
  2. Master-Slave replication for making our system fault tolerant.

    主从复制使我们的系统具有容错能力。
  3. Rich Document based query to get the stored data.

    基于丰富文档的查询来获取存储的数据。
  4. Auto Sharding to achieve horizontal scaling.

    自动分片以实现水平缩放。

MongoDB is also very popular NoSQL database because we have drivers for all the major programming languages such as Java, PHP, Perl, Python, Ruby, Scala, C, C++ etc.

MongoDB也是非常流行的NoSQL数据库,因为我们拥有适用于所有主要编程语言的驱动程序,例如Java,PHP,Perl,Python,Ruby,Scala,C,C ++等。

在Mac上安装MongoDB (Install MongoDB on Mac)

This is the first tutorial in MongoDB series and we will learn how to install MongoDB on Mac OS X. For this tutorial, I am using following softwares:

这是MongoDB系列中的第一篇教程,我们将学习如何在Mac OS X上安装MongoDB。对于本教程,我正在使用以下软件:

  1. Max OS X 10.12.6

    Max OS X 10.12.6
  2. MongoDB Server 3.4.7 that you can download from MongoDB Downloads Page

    您可以从MongoDB下载页面下载的MongoDB Server 3.4.7

Follow below commands in order to install MongoDB and start it.

请遵循以下命令以安装MongoDB并启动它。

pankaj@Pankajs-MacBook-Pro:~/Downloads/$ curl -O https://fastdl.mongodb.org/osx/mongodb-linux-x86_64-3.4.7.tgz
pankaj:~ pankaj$ tar -xvf mongodb-linux-x86_64-3.4.7.tgz
x mongodb-linux-x86_64-3.4.7/README
x mongodb-linux-x86_64-3.4.7/THIRD-PARTY-NOTICES
x mongodb-linux-x86_64-3.4.7/MPL-2
x mongodb-linux-x86_64-3.4.7/GNU-AGPL-3.0
x mongodb-linux-x86_64-3.4.7/bin/mongodump
x mongodb-linux-x86_64-3.4.7/bin/mongorestore
x mongodb-linux-x86_64-3.4.7/bin/mongoexport
x mongodb-linux-x86_64-3.4.7/bin/mongoimport
x mongodb-linux-x86_64-3.4.7/bin/mongostat
x mongodb-linux-x86_64-3.4.7/bin/mongotop
x mongodb-linux-x86_64-3.4.7/bin/bsondump
x mongodb-linux-x86_64-3.4.7/bin/mongofiles
x mongodb-linux-x86_64-3.4.7/bin/mongooplog
x mongodb-linux-x86_64-3.4.7/bin/mongoreplay
x mongodb-linux-x86_64-3.4.7/bin/mongoperf
x mongodb-linux-x86_64-3.4.7/bin/mongod
x mongodb-linux-x86_64-3.4.7/bin/mongos
x mongodb-linux-x86_64-3.4.7/bin/mongo
pankaj@Pankajs-MacBook-Pro:~/Downloads/$ mv mongodb-linux-x86_64-3.4.7 mongodb
pankaj@Pankajs-MacBook-Pro:~/Downloads/$ cd mongodb
pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb$ mkdir data
pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb$ cd bin
pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$ ./mongod --dbpath /Users/pankaj/Downloads/mongodb/data &
Mon Oct 14 15:55:52.131 [initandlisten] MongoDB starting : pid=950 port=27017 dbpath=/Users/pankaj/Downloads/mongodb/data 64-bit host=Pankajs-MacBook-Pro.local
Mon Oct 14 15:55:52.131 [initandlisten] 
Mon Oct 14 15:55:52.131 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Mon Oct 14 15:55:52.131 [initandlisten] db version v3.4.7
Mon Oct 14 15:55:52.131 [initandlisten] git version: b9925db5eac369d77a3a5f5d98a145eaaacd9673
Mon Oct 14 15:55:52.131 [initandlisten] build info: Darwin bs-osx-106-x86-64-2.10gen.cc 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:32:41 PDT 2011; root:xnu-1504.15.3~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
Mon Oct 14 15:55:52.131 [initandlisten] allocator: system
Mon Oct 14 15:55:52.131 [initandlisten] options: { dbpath: "/Users/pankaj/Downloads/mongodb/data" }
Mon Oct 14 15:55:52.132 [initandlisten] journal dir=/Users/pankaj/Downloads/mongodb/data/journal
Mon Oct 14 15:55:52.132 [initandlisten] recover : no journal files present, no recovery needed
Mon Oct 14 15:55:52.238 [FileAllocator] allocating new datafile /Users/pankaj/Downloads/mongodb/data/local.ns, filling with zeroes...
Mon Oct 14 15:55:52.238 [FileAllocator] creating directory /Users/pankaj/Downloads/mongodb/data/_tmp
Mon Oct 14 15:55:52.347 [FileAllocator] done allocating datafile /Users/pankaj/Downloads/mongodb/data/local.ns, size: 16MB,  took 0.108 secs
Mon Oct 14 15:55:52.646 [FileAllocator] allocating new datafile /Users/pankaj/Downloads/mongodb/data/local.0, filling with zeroes...
Mon Oct 14 15:55:53.578 [FileAllocator] done allocating datafile /Users/pankaj/Downloads/mongodb/data/local.0, size: 64MB,  took 0.931 secs
Mon Oct 14 15:55:53.871 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0  reslen:37 1633ms
Mon Oct 14 15:55:53.872 [initandlisten] couldn't unlink socket file /tmp/mongodb-27017.sockerrno:13 Permission denied skipping
Mon Oct 14 15:55:53.872 [websvr] admin web console waiting for connections on port 28017
Mon Oct 14 15:55:53.872 [initandlisten] waiting for connections on port 27017
pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$

First two commands are to download the tar ball using curl command and then extract it with tar command. Then I am renaming the extracted directory to remember the path easily.

前两个命令是使用curl命令下载tar球,然后使用tar命令将其解压缩。 然后,我重命名提取的目录以轻松记住路径。

If we don’t provide data directory, MongoDB server tries to access it at “/data/db”, so I am overriding it with --dbpath option with mongod executable file.

如果我们不提供数据目录,MongoDB服务器将尝试在“ / data / db”访问它,因此我用--dbpath选项和mongod可执行文件覆盖了它。

mongod is the server program that we need to execute to start the MongoDB server and I am ending it with & so that it runs in background and doesn’t get terminated if I close the terminal.

mongod是启动MongoDB服务器所需的服务器程序,我以&结束它,以便它在后台运行,并且如果我关闭终端也不会终止。

MongoDB default port is 27017 but we can change it by providing command line option or through configuration file.

MongoDB的默认端口是27017,但是我们可以通过提供命令行选项或通过配置文件来更改它。

That’s it, our MongoDB server is up and running, now we can test it with MongoDB terminal client.

install mongodb on mac

就是这样,我们的MongoDB服务器已启动并正在运行,现在我们可以使用MongoDB终端客户端对其进行测试。

pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$ ./mongo
MongoDB shell version: 3.4.7
connecting to: test
Server has startup warnings: 
Mon Oct 14 16:11:35.330 [initandlisten] 
Mon Oct 14 16:11:35.330 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
> show dbs
local	0.078125GB
> use journaldev
switched to db journaldev
> db.names.save({"id":123,"name":"Pankaj"})
> db.names.find()
{ "_id" : ObjectId("525c7af46e7301702bbfea1e"), "id" : 123, "name" : "Pankaj" }
> db.datas.save({})
> show collections
datas
names
system.indexes
> show dbs
journaldev	0.203125GB
local	0.078125GB
> exit
bye
pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$

show dbs: Command to get the list of databases

show dbs :用于获取数据库列表的命令

use DBNAME: Command to create new DB or switch to an existing DB, note that until unless you do any save or find operation on new DB, it wont be created.

use DBNAME :命令创建新数据库或切换到现有数据库,请注意,除非对新数据库执行任何保存或查找操作,否则不会创建该数据库。

db.names.save({}): Command to save JSON document in current DB. MongoDB store documents in terms of collections. In this command, names is the collection.

db.names.save({}) :用于将JSON文档保存在当前数据库中的命令。 MongoDB按照集合存储文档。 在此命令中,名称是集合。

db.names.find(): Command to list all the documents in the names collection.

db.names.find() :用于列出名称集合中所有文档的命令。

show collections: Command to list all the collections in the current database.

show collections :命令列出当前数据库中的所有集合。

You might notice that I am accessing mongodb executables through relative path starting with ./, you can add mongodb bin location to PATH variable to access mongodb executables directly.

您可能会注意到,我是通过以./开头的相对路径访问mongodb可执行文件的,您可以将mongodb bin位置添加到PATH变量中以直接访问mongodb可执行文件。

pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$ vi ~/.bash_profile 
//add below line and save it
export PATH=$PATH:/Users/pankaj/Downloads/mongodb/bin
pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$ source ~/.bash_profile

Now you can access mongodb executables from anywhere without providing the directory location.

现在,您可以从任何地方访问mongodb可执行文件,而无需提供目录位置。

将MongoDB作为Mac OS X服务运行 (Running MongoDB as Mac OS X Service)

To run MongoDB as service, we need to create PList file in /Library/LaunchDaemons directory.

要将MongoDB作为服务运行,我们需要在/ Library / LaunchDaemons目录中创建PList文件。

org.mongodb.mongod.plist

org.mongodb.mongod.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>org.mongodb.mongod</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Users/pankaj/Downloads/mongodb/bin/mongod</string>
    <string>run</string>
    <string>--config</string>
    <string>/Users/pankaj/Downloads/mongodb/mongod.conf</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
<key>UserName</key>
<string>pankaj</string>
</dict>
</plist>
pankaj@Pankajs-MacBook-Pro:~$ cd /Library/LaunchDaemons
pankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ sudo vi org.mongodb.mongod.plist 
pankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ sudo launchctl load org.mongodb.mongod.plist 

#below commands to start and stop service through launchctl command
pankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ sudo launchctl start org.mongodb.mongod
pankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ sudo launchctl stop org.mongodb.mongod

pankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ ps -eaf | grep mongod
    0    61     1   0  3:04PM ??         0:06.31 /Users/pankaj/Downloads/mongodb/bin/mongod run --config /Users/pankaj/Downloads/mongodb/mongod.conf
pankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$

You might notice that rather than providing options as command line argument, I am using config file to pass arguments to mongod. Some of the commonly used configurations in MongoDB configuration file can be:

您可能会注意到,我没有使用选项作为命令行参数,而是使用配置文件将参数传递给mongod。 MongoDB配置文件中的一些常用配置可以是:

pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb$ cat mongod.conf 
#we can change port, I am setting default port
port=27017

#we can enable verbose mode, by default its false
verbose=true

#limit max connections, cant set more than 20000
maxConns=50

#setting up log path
logpath=/Users/pankaj/Downloads/mongodb/logs/mongo.log
logappend=true

#create PID file
pidfilepath=/Users/pankaj/Downloads/mongodb/mongo.pid

#setting dbpath, dont want to use default /data/db path
dbpath=/Users/pankaj/Downloads/mongodb/data

Note that you need to create mongo.log file manually and it should have the correct permission. For example, in above configuration I am using “pankaj” user to run the process, so the log file should be created by same user. If you don’t provide any user information in the plist file, it will be started with root user.

请注意,您需要手动创建mongo.log文件,并且该文件应具有正确的权限。 例如,在上述配置中,我使用“ pankaj”用户运行该过程,因此日志文件应由同一用户创建。 如果您没有在plist文件中提供任何用户信息,它将以root用户启动。

Now when you will restart the Mac OS X, you will notice that MongoDB has already been started as service and you don’t need to start it manually.

现在,当您重新启动Mac OS X时,您会注意到MongoDB已经作为服务启动,您无需手动启动它。

References:

参考文献:

MongoDB Official Website
Running MongoDB as Service on Mac OS X

MongoDB官方网站
在Mac OS X上将MongoDB作为服务运行

翻译自: https://www.journaldev.com/2330/install-mongodb-mac

mongodb mac安装

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值