Detailed steps for installing and configuring mongodb under mac

  • 2020-06-23 02:10:50
  • OfStack

What is MongoDB

MongoDB is a database based on distributed file storage. Written by C++ language. Designed to provide scalable, high-performance data storage solutions for WEB applications.

Its characteristics: high performance, easy to deploy, easy to use, very convenient to store data.

This article mainly introduces the steps of installing and configuring mongodb under mac. It is Shared for your reference and learning. Let's start with a detailed introduction.

Note: this article related source can be downloaded at the end of the text!

Install mongodb

Before install, iTerm2 USES brew to view the installed software and search mongodb:


brew list
brew search mongodb

Install mongodb:


brew install mongodb

Here, it needs to wait for a period of time. After success, the following output will indicate the success of installation:


$ brew install mongodb
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.4.0.sierra.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring mongodb-3.4.0.sierra.bottle.1.tar.gz
==> Caveats
To have launchd start mongodb now and restart at login:
 brew services start mongodb
Or, if you don't want/need a background service you can just run:
 mongod --config /usr/local/etc/mongod.conf
==> Summary
🍺 /usr/local/Cellar/mongodb/3.4.0: 17 files, 261.4M

Start the mongodb

Create a new iTerm2 window and execute mongod Attempt to start mongodb fails exiting:


$ mongod
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] MongoDB starting : pid=1765 port=27017 dbpath=/data/db 64-bit host=MacBook-Pro-2.local
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] db version v3.4.0
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] git version: f4240c60f005be757399042dc12f6addbc3170c1
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2j 26 Sep 2016
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] allocator: system
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] modules: none
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] build environment:
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] distarch: x86_64
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] target_arch: x86_64
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] options: {}
2017-06-12T15:51:49.811+0800 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2017-06-12T15:51:49.811+0800 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2017-06-12T15:51:49.811+0800 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2017-06-12T15:51:49.811+0800 I CONTROL [initandlisten] now exiting
2017-06-12T15:51:49.811+0800 I CONTROL [initandlisten] shutting down with code:100

Before starting mongodb, create a new mongodb default data write directory:


$ mkdir -p /data/db
mkdir: /data/db: Permission denied ( Access is denied without permission )

// sudo  Enter the password and create a new directory 
$ sudo mkdir -p /data/db
Password:

Grant permissions to the database directory you just created:


$ sudo chown -R guojc /data

At this point, execute mongod Start mongodb Service:


$ mongod
2017-06-12T16:00:48.036+0800 I CONTROL [initandlisten] MongoDB starting : pid=1837 port=27017 dbpath=/data/db 64-bit host=MacBook-Pro-2.local
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] db version v3.4.0
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] git version: f4240c60f005be757399042dc12f6addbc3170c1
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2j 26 Sep 2016
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] allocator: system
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] modules: none
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] build environment:
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] distarch: x86_64
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] target_arch: x86_64
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] options: {}
2017-06-12T16:00:48.037+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=3584M,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten]
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten] **  Read and write access to data and configuration is unrestricted.
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten]
2017-06-12T16:00:48.665+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2017-06-12T16:00:48.741+0800 I INDEX [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2017-06-12T16:00:48.741+0800 I INDEX [initandlisten] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2017-06-12T16:00:48.755+0800 I INDEX [initandlisten] build index done. scanned 0 total records. 0 secs
2017-06-12T16:00:48.756+0800 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.4
2017-06-12T16:00:48.756+0800 I NETWORK [thread1] waiting for connections on port 27017

mongodb started successfully and is waiting to be connected.

Create a new WINDOW for iTerm2 and execute mongo , enter mongodb command line mode:


$ mongo
MongoDB shell version v3.4.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.0
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
 http://docs.mongodb.org/
Questions? Try the support group
 http://groups.google.com/group/mongodb-user
Server has startup warnings:
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten]
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten] **  Read and write access to data and configuration is unrestricted.
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten]
>

Continue input at the terminal above show dbs , will list the two databases that come with the system:


> show dbs
admin 0.000GB
local 0.000GB
help

Summary 1. To restart the mongodb service and enter the mongodb command line:

In one iTerm2 window: mongod //MongoDB starting........waiting for connections

Another iTerm2 window: mongo //MongoDB shell

Insert data

First, install node's data driver library in your computer's root directory:


# guojc @ MacBook-Pro-2 in ~ [16:29:33] C:127
$ cnpm install mongodb

Create a new directory and edit text.js file: s3_mongodb/ test.js:


brew install mongodb
0

Create a new iTerm2 window and run ES104en.js:


brew install mongodb
1

After success comes before execution mongo MongoDB shell window, execute:


brew install mongodb
2

Continue: Select database test, display the table in test, query all the data of the table:


> use test
switched to db test

> show tables
site

> db.site.find()
{ "_id" : ObjectId("593e594ab4727b09349c33a6"), "name" : "guojc", "age" : 99, "hobby" : "movie" }

This completes the installation configuration and basic startup of mongodb.

Related source code download:

github address: https: / / github com gjincai/vue - node - proj/blob master/s3_Mongodb/test js

Local download address: http: / / xiazai ofstack. com / 201707 / yuanma/mac - mongodb (ofstack. com). rar

conclusion


Related articles: