1. MongoDB安装与启动

1.1 MongoDB安装

在MacOS X系统中,采用Homebrew安装MongoDB。打开终端(Terminal),输入如下:

brew install mongodb

安装完成后,MongoDB所在目录为/usr/local/Cellar/mongodb, 该目录结构如下:

[xinhuan@MacBook-Pro] ~$ tree /usr/local/Cellar/mongodb/
/usr/local/Cellar/mongodb/
└── 3.4.7
    ├── INSTALL_RECEIPT.json
    ├── README
    ├── bin
    │   ├── bsondump
    │   ├── mongo
    │   ├── mongod
    │   ├── mongodump
    │   ├── mongoexport
    │   ├── mongofiles
    │   ├── mongoimport
    │   ├── mongooplog
    │   ├── mongoperf
    │   ├── mongoreplay
    │   ├── mongorestore
    │   ├── mongos
    │   ├── mongostat
    │   └── mongotop
    └── homebrew.mxcl.mongodb.plist

2 directories, 17 files
[xinhuan@MacBook-Pro] ~$

默认的配置文件为/usr/local/etc/mongod.conf,配置文件内容如下:

systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /usr/local/var/mongodb
net:
  bindIp: 127.0.0.1

1.2 启动MongoDB服务进程

打开终端,输入如下:

mongod -f /usr/local/etc/mongod.conf

然后,按回车键,就启动了MongoDB服务进程。

1.3 客户端连接MongoDB

重新打开终端,输入:mongo,然后回车,显示如下:

[xinhuan@MacBook-Pro] ~$ mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Server has startup warnings: 
2017-09-05T00:59:42.419+0800 I CONTROL  [initandlisten] 
2017-09-05T00:59:42.419+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-09-05T00:59:42.419+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-09-05T00:59:42.419+0800 I CONTROL  [initandlisten] 
2017-09-05T00:59:42.419+0800 I CONTROL  [initandlisten] 
2017-09-05T00:59:42.419+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>

则表明连接成功。

1.4 关闭MongoDB服务进程

(1) 服务端直接关闭1.2中的服务进程
(2) 客户端使用管理员命令关闭
  1. 选择admin数据库

    use admin;
    
  2. 关闭MongoDB服务

    db.shutdownServer();
    

运行示例如下:

> use admin;
switched to db admin
> db.shutdownServer();
server should be down...
2017-09-05T01:19:29.094+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2017-09-05T01:19:29.095+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-09-05T01:19:29.095+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed 
>
    原文作者:V1ncent_zZ
    原文地址: https://www.jianshu.com/p/6ded87dbab17
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞