700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > mongodb mac安装_在Mac OS X上安装MongoDB

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

时间:2021-08-26 11:49:54

相关推荐

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

mongodb mac安装

Today we will install MongoDB on Mac OS X.MongoDBis 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的一些功能包括:

Full indexing support for faster query processing.全面的索引支持,可加快查询处理速度。 Master-Slave replication for making our system fault tolerant.主从复制使我们的系统具有容错能力。 Rich Document based query to get the stored data.基于丰富文档的查询来获取存储的数据。 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。对于本教程,我正在使用以下软件:

Max OS X 10.12.6Max OS X 10.12.6 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 /osx/mongodb-linux-x86_64-3.4.7.tgzpankaj:~ pankaj$ tar -xvf mongodb-linux-x86_64-3.4.7.tgzx mongodb-linux-x86_64-3.4.7/READMEx mongodb-linux-x86_64-3.4.7/THIRD-PARTY-NOTICESx mongodb-linux-x86_64-3.4.7/MPL-2x mongodb-linux-x86_64-3.4.7/GNU-AGPL-3.0x mongodb-linux-x86_64-3.4.7/bin/mongodumpx mongodb-linux-x86_64-3.4.7/bin/mongorestorex mongodb-linux-x86_64-3.4.7/bin/mongoexportx mongodb-linux-x86_64-3.4.7/bin/mongoimportx mongodb-linux-x86_64-3.4.7/bin/mongostatx mongodb-linux-x86_64-3.4.7/bin/mongotopx mongodb-linux-x86_64-3.4.7/bin/bsondumpx mongodb-linux-x86_64-3.4.7/bin/mongofilesx mongodb-linux-x86_64-3.4.7/bin/mongooplogx mongodb-linux-x86_64-3.4.7/bin/mongoreplayx mongodb-linux-x86_64-3.4.7/bin/mongoperfx mongodb-linux-x86_64-3.4.7/bin/mongodx mongodb-linux-x86_64-3.4.7/bin/mongosx mongodb-linux-x86_64-3.4.7/bin/mongopankaj@Pankajs-MacBook-Pro:~/Downloads/$ mv mongodb-linux-x86_64-3.4.7 mongodbpankaj@Pankajs-MacBook-Pro:~/Downloads/$ cd mongodbpankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb$ mkdir datapankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb$ cd binpankaj@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.localMon 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 1000Mon Oct 14 15:55:52.131 [initandlisten] db version v3.4.7Mon Oct 14 15:55:52.131 [initandlisten] git version: b9925db5eac369d77a3a5f5d98a145eaaacd9673Mon Oct 14 15:55:52.131 [initandlisten] build info: Darwin bs-osx-106-x86-64- 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:32:41 PDT ; root:xnu-1504.15.3~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49Mon Oct 14 15:55:52.131 [initandlisten] allocator: systemMon 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/journalMon Oct 14 15:55:52.132 [initandlisten] recover : no journal files present, no recovery neededMon 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/_tmpMon Oct 14 15:55:52.347 [FileAllocator] done allocating datafile /Users/pankaj/Downloads/mongodb/data/local.ns, size: 16MB, took 0.108 secsMon 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 secsMon Oct 14 15:55:53.871 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 reslen:37 1633msMon Oct 14 15:55:53.872 [initandlisten] couldn't unlink socket file /tmp/mongodb-27017.sockerrno:13 Permission denied skippingMon Oct 14 15:55:53.872 [websvr] admin web console waiting for connections on port 28017Mon Oct 14 15:55:53.872 [initandlisten] waiting for connections on port 27017pankaj@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--dbpathoption withmongodexecutable 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.

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

pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$ ./mongoMongoDB shell version: 3.4.7connecting to: testServer 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 dbslocal0.078125GB> use journaldevswitched to db journaldev> db.names.save({"id":123,"name":"Pankaj"})> db.names.find(){ "_id" : ObjectId("525c7af46e7301702bbfea1e"), "id" : 123, "name" : "Pankaj" }> db.datas.save({})> show collectionsdatasnamessystem.indexes> show dbsjournaldev0.203125GBlocal0.078125GB> exitbyepankaj@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 itexport PATH=$PATH:/Users/pankaj/Downloads/mongodb/binpankaj@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" "/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/LaunchDaemonspankaj@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 commandpankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ sudo launchctl start org.mongodb.mongodpankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ sudo launchctl stop org.mongodb.mongodpankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ ps -eaf | grep mongod0 611 0 3:04PM ?? 0:06.31 /Users/pankaj/Downloads/mongodb/bin/mongod run --config /Users/pankaj/Downloads/mongodb/mongod.confpankaj@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 portport=27017#we can enable verbose mode, by default its falseverbose=true#limit max connections, cant set more than 20000maxConns=50#setting up log pathlogpath=/Users/pankaj/Downloads/mongodb/logs/mongo.loglogappend=true#create PID filepidfilepath=/Users/pankaj/Downloads/mongodb/mongo.pid#setting dbpath, dont want to use default /data/db pathdbpath=/Users/pankaj/Downloads/mongodb/data

Note that you need to createmongo.logfile 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作为服务运行

翻译自: /2330/install-mongodb-mac

mongodb mac安装

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。