mac下mongodbのインストールと構成

7758 ワード

本明細書の関連ソース

mongodbのインストール


インストールする前にiTerm 2でbrewを使用してインストール済みソフトウェアを表示し、mongodbを検索します.
brew list
brew search mongodb

mongodbのインストール:
brew install mongodb

ここではしばらく待たなければなりません.成功すると、インストールに成功したことを示します.
$ 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

mongodbの起動


新しいiTerm 2ウィンドウを作成し、mongodを実行してmongodbを起動しようとしますが、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

mongodbを起動する前に、mongodbのデフォルトのデータ書き込みディレクトリを新規作成します.
$ mkdir -p /data/db
mkdir: /data/db: Permission denied (        )

// sudo      ,      
$ sudo mkdir -p /data/db
Password:

新しく作成したデータベース・ディレクトリに権限を付与するには、次の手順に従います.
$ sudo chown -R guojc /data

このとき、mongodを実行してmongodbサービスを開始します.
$ 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の起動に成功し、接続を待っています.
iTerm 2ウィンドウを新規作成し、mongoを実行し、mongodbコマンドラインモードに入ります.
$ 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]
>

上の端末にshow dbsを入力し続けると、システムに付属する2つのデータベースがリストされます.
> show dbs
admin  0.000GB
local  0.000GB
help

まとめて、mongodbサービスを再起動し、mongodbコマンドラインにアクセスする操作:iTerm 2ウィンドウで実行:mongod //MongoDB starting........waiting for connections別iTerm 2ウィンドウで実行:mongo //MongoDB shell

データの挿入


まず、nodeのデータドライバライブラリをコンピュータのルートディレクトリにインストールします.
# guojc @ MacBook-Pro-2 in ~ [16:29:33] C:127
$ cnpm install mongodb

ディレクトリを新規作成しtextを編集します.jsファイル:s 3_mongodb/test.js:
var MongoClient = require('mongodb').MongoClient;
//      
var url_test = 'mongodb://localhost:27017/test'; //   test    ,        

var insertData = function(db){
  //  test        site  ,       
  db.collection('site').insertOne({name: 'guojc', age: 99, hobby: 'movie'}, function(err, result){
    console.log('inserted successly');
    console.log(result);
    db.close();
    console.log('close');
  });
}

MongoClient.connect(url_test, function(err, db) {
  console.log('Connected successly to server.');
  insertData(db);
});

iTerm 2ウィンドウを新規作成し、testを実行します.js:
node test.js

成功後、mongoが実行されていたMongoDB shellウィンドウに来て、実行します.
> show dbs
admin  0.000GB
local  0.000GB
test  0.000GB
help

続行:データベースtestを選択し、testのテーブルを表示し、テーブルのすべてのデータをクエリーします.
> use test
switched to db test

> show tables
site

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

これでmongodbのインストール構成と基本起動について説明します.次はmongoseを使ってmongodbを操作することを学び続けます.