1. 程式人生 > >mongodb的學習-3-在Mac上的安裝配置

mongodb的學習-3-在Mac上的安裝配置

1.使用homebrew安裝:

brew install mongodb

檢視安裝好的版本:

mongo --version
MongoDB shell version v3.6.4
git version: d0181a711f7e7f39e60b5aeb1dc7097bf6ae5856
OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
allocator: system
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64

 

2.然後建立資料檔案:

1)進入根目錄

cd /

2)建立目錄(-p是建立多個檔案目錄使用的引數)

mkdir -p /data/db

3)設定許可權,並輸入使用者密碼

首先使用ls -l先檢視許可權:

drwxr-xr-x   3 root  wheel    96  4 27  2018 data

然後進行許可權的更改(-R表示對目錄進行遞迴操作,就是data目錄下的子檔案也設定該許可權):

sudo chmod -R 777 /data

變為:

drwxrwxrwx   3 root  wheel    96  4 27  2018
data

 

3.實現開機自啟動

1)設定plist檔案

userdeMacBook-Pro:~ user$ which mongod
/usr/local/bin/mongod

然後找到上面的mongod執行檔案,右鍵-顯示簡介,可以得到該執行檔案的原始位置,用來得到安裝的mongodb的目錄:

/usr/local/Cellar/mongodb/3.6.4/bin/mongod

然後來到/usr/local/Cellar/mongodb/3.6.4/目錄下可以看見homebrew.mxcl.mongodb.plist檔案:

<?xml version="1.0"
encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>homebrew.mxcl.mongodb</string> <key>ProgramArguments</key> <array> <string>/usr/local/opt/mongodb/bin/mongod</string> <string>--config</string> <string>/usr/local/etc/mongod.conf</string> </array> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <false/> <key>WorkingDirectory</key> <string>/usr/local</string> <key>StandardErrorPath</key> <string>/usr/local/var/log/mongodb/output.log</string> <key>StandardOutPath</key> <string>/usr/local/var/log/mongodb/output.log</string> <key>HardResourceLimits</key> <dict> <key>NumberOfFiles</key> <integer>4096</integer> </dict> <key>SoftResourceLimits</key> <dict> <key>NumberOfFiles</key> <integer>4096</integer> </dict> </dict> </plist>

修改部分:

  <key>Label</key>
  <string>mongodb</string>   //
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/Cellar/mongodb/3.6.4/bin/mongod</string> //
  </array>

並修改檔名為mongodb.plist,然後將其複製到:

userdeMacBook-Pro:~ user$ cp mongodb.plist /Library/LaunchDaemons/
cp: /Library/LaunchDaemons/mongodb.plist: Permission denied
userdeMacBook-Pro:~ user$ sudo cp mongodb.plist /Library/LaunchDaemons/
Password:

該檔案所在位置為:

 /Library/LaunchDaemons/mongodb.plist

⚠️該目錄與~/Library/LaunchDaemons//System/Library/LaunchDaemons/是不同的

Launch

檔名 啟動型別
LaunchDaemons 使用者未登陸前就啟動的服務(守護程序)
LaunchAgents 使用者登陸後啟動的服務(守護程序)

檔案路徑

/System/Library/?目錄是存放Apple自己開發的軟體
/Library/?目錄是系統管理員存放的第三方軟體
~/Library/?目錄是使用者自己存放的第三方軟體

 

提示:由於mongod可執行檔案是第三方軟體,所以放到~/Library目錄或者/Library/?目錄,當然在這裡我存放到了系統管理員存放的第三方軟體。?可以是LaunchDaemons或者LaunchAgents。

 

2)啟動服務

啟動服務(這樣以後你一開機,mongodb的伺服器就打開了)

userdeMacBook-Pro:~ user$ sudo launchctl load -w /Library/LaunchDaemons/mongodb.plist 
Password:

然後啟動,但是會有一些警告:

userdeMacBook-Pro:~ user$ mongo
MongoDB shell version v3.6.4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.4
Server has startup warnings: 
2018-11-30T11:28:36.449+0800 I CONTROL  [initandlisten] 
2018-11-30T11:28:36.449+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-11-30T11:28:36.449+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-11-30T11:28:36.450+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-11-30T11:28:36.450+0800 I CONTROL  [initandlisten] 
2018-11-30T11:28:36.450+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-11-30T11:28:36.450+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2018-11-30T11:28:36.450+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2018-11-30T11:28:36.450+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-11-30T11:28:36.450+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-11-30T11:28:36.450+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-11-30T11:28:36.450+0800 I CONTROL  [initandlisten] 
> 

 

之前在homebrew.mxcl.mongodb.plist檔案的ProgramArguments部署中刪掉了內容:

    <string>--config</string>
    <string>/usr/local/etc/mongod.conf</string>

/usr/local/etc/mongod.conf檔案內容為,一部分內容與警告是符合的:

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

將這個部署放回mongodb.plist檔案,然後關閉服務,開啟服務

然後再執行mongo,可見錯誤少了一些

Server has startup warnings: 
2018-11-30T14:23:34.231+0800 I CONTROL  [initandlisten] 
2018-11-30T14:23:34.231+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-11-30T14:23:34.231+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-11-30T14:23:34.231+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-11-30T14:23:34.231+0800 I CONTROL  [initandlisten]

解決辦法就是在mongod.conf上新增:

security:  
    authorization: enabled   
    javascriptEnabled: true  
setParameter:   
    enableLocalhostAuthBypass: true  
    authenticationMechanisms: SCRAM-SHA-1

更多詳細的內容可以看https://www.jianshu.com/p/f9f1454f251f

然後關閉再開啟服務,執行,就不再有錯誤了:

userdeMacBook-Pro:~ user$ mongo
MongoDB shell version v3.6.4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.4
> 

 

關閉服務

sudo launchctl unload -w /Library/LaunchDaemons/mongodb.plist   

如果你關閉了服務,再訪問mongo時,返回:

userdeMacBook-Pro:~ user$ mongo
MongoDB shell version v3.6.4 connecting to: mongodb://127.0.0.1:27017 2018-11-30T11:38:41.753+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused 2018-11-30T11:38:41.760+0800 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed : [email protected]/mongo/shell/mongo.js:251:13 @(connect):1:6 exception: connect failed

 

4.

當然你也可以不用使用上面的開機自啟動,可以自己使用mongodb來配置伺服器並開啟

但是這個時候我們執行mongod去開啟伺服器時,出現了問題:

2018-11-30T15:03:31.081+0800 E STORAGE  [initandlisten] WiredTiger error (13) [1543561411:81460][1401:0x1188695c0], file:WiredTiger.wt, connection: /data/db/WiredTiger.turtle: handle-open: open: Permission denied 2018-11-30T15:05:52.154+0800 E STORAGE [initandlisten] WiredTiger error (13) [1543561552:154095][1402:0x1196205c0], file:WiredTiger.wt, connection: /data/db/WiredTiger.turtle: handle-open: open: Permission denied

可以看出來是這兩個檔案的許可權問題,然後去檢視:

userdeMacBook-Pro:db user$ ls -l
total 696
-rwxrwxrwx  1 root     wheel     48  4 27 2018 WiredTiger -rwxrwxrwx 1 root wheel 21 4 27 2018 WiredTiger.lock -rw------- 1 root wheel 1069 11 30 14:55 WiredTiger.turtle -rwxrwxrwx 1 root wheel 45056 11 30 14:55 WiredTiger.wt

進行更改,當然,在執行一遍sudo chmod -R 777 /data也行:

userdeMacBook-Pro:db user$ sudo chmod -R 777 ./WiredTiger.turtle 
Password:
userdeMacBook-Pro:db user$ sudo chmod -R 777 ./WiredTigerLAS.wt 

再執行就成功了:

 

這時候跟之前一樣有警告資訊,解決

 1)

2018-11-30T15:26:24.360+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-11-30T15:26:24.360+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2018-11-30T15:26:24.360+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2018-11-30T15:26:24.360+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-11-30T15:26:24.360+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-11-30T15:26:24.360+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.

解決-新增--bind_ip 127.0.0.1

userdeMBP:~ user$ mongod --bind_ip 127.0.0.1

 

 2)WARNING: soft rlimits too low. Number of files is 256, should be at least 10

userdeMBP:test-sequelize user$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 1
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1418
virtual memory          (kbytes, -v) unlimited

更改:

userdeMBP:test-sequelize user$ ulimit -n 1024
userdeMBP:test-sequelize user$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 1
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1418
virtual memory          (kbytes, -v) unlimited

 

 

3)WARNING: Access control is not enabled for the database.

首先開啟訪問控制(--auth)

當然,你要現在沒有開啟訪問控制時進行使用者的新增:

這樣就建立好一個超級管理員使用者,建立全域性使用者或者超級使用者,需要在MongoDB的admin資料庫中建立(在其他庫也可以建立,但是沒有該角色功能)

重啟mongod程序後:

mongod --auth --bind_ip 127.0.0.1

接下來做一下許可權的驗證:

userdeMBP:~ user$ mongo
MongoDB shell version v3.6.4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.4
> show dbs
2018-11-30T16:11:13.704+0800 E QUERY    [thread1] Error: listDatabases failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, $db: \"admin\" }",
    "code" : 13,
    "codeName" : "Unauthorized"
} :

可見不能直接操作了

測試之前新增的使用者:

> use admin
switched to db admin
> show dbs
2018-11-30T16:12:00.963+0800 E QUERY    [thread1] Error: listDatabases failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, $db: \"admin\" }",
    "code" : 13,
    "codeName" : "Unauthorized"
} :
[email protected]/mongo/shell/utils.js:25:13
[email protected]/mongo/shell/mongo.js:65:1
[email protected]/mongo/shell/utils.js:820:19
[email protected]/mongo/shell/utils.js:710:15
@(shellhelp2):1:1
> db.auth('user','user')
Error: Authentication failed.
0
> use test
switched to db test
> show dbs
2018-11-30T16:13:59.639+0800 E QUERY    [thread1] Error: listDatabases failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, $db: \"admin\" }",
    "code" : 13,
    "codeName" : "Unauthorized"
} :
[email protected]/mongo/shell/utils.js:25:13
[email protected]/mongo/shell/mongo.js:65:1
[email protected]/mongo/shell/utils.js:820:19
[email protected]/mongo/shell/utils.js:710:15
@(shellhelp2):1:1
> db.auth('user','user')
1
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> 

⚠️:這裡admin資料庫沒能成功認證使用者的原因是我上面生成使用者的時候忘記進入admin資料庫了,所以其實生成出來的使用者是test資料庫的,但是大家大概知道這個意思即可

 MongoDB資料庫的使用者許可權控制權限還是比較多的,有系統自帶的,已經定義好的角色,也可以自己定義角色許可權,需要根據業務需要進行許可權分配:

自帶角色的說明(一般內建的角色基本上就可以滿足生產環境需求了):

https://docs.mongodb.org/manual/core/security-built-in-roles/

使用者自行定義角色的說明:

https://docs.mongodb.org/manual/core/security-user-defined-roles/

使用者管理配置的說明

https://docs.mongodb.org/manual/reference/method/#user-management-methods