1.使用homebrew安装:

brew install mongodb

查看安装好的版本:

mongo --version
MongoDB shell version v3.6.4
git version: d0181a711f7e7f39e60b5aeb1dc7097bf6ae5856
OpenSSL version: OpenSSL 1.0.2o Mar
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    root  wheel          data

然后进行权限的更改(-R表示对目录进行递归操作,就是data目录下的子文件也设置该权限):

sudo chmod -R  /data

变为:

drwxrwxrwx    root  wheel          data

3.实现开机自启动

1)设置plist文件

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

然后找到上面的mongod执行文件,右键-显示简介,可以得到该执行文件的原始位置,用来得到安装的mongodb的目录:

/usr/local/Cellar/mongodb/3.6./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></integer>
</dict>
<key>SoftResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer></integer>
</dict>
</dict>
</plist>

修改部分:

  <key>Label</key>
<string>mongodb</string> //改
<key>ProgramArguments</key>
<array>
<string>/usr/local/Cellar/mongodb/3.6./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.
Server has startup warnings:
--30T11::36.449+ I CONTROL [initandlisten]
--30T11::36.449+ I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
--30T11::36.449+ I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
--30T11::36.450+ I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
--30T11::36.450+ I CONTROL [initandlisten]
--30T11::36.450+ I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
--30T11::36.450+ I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
--30T11::36.450+ I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
--30T11::36.450+ I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
--30T11::36.450+ I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
--30T11::36.450+ I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
--30T11::36.450+ 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:
--30T14::34.231+ I CONTROL [initandlisten]
--30T14::34.231+ I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
--30T14::34.231+ I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
--30T14::34.231+ I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
--30T14::34.231+ I CONTROL [initandlisten]

解决办法就是在mongod.conf上添加:

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

更多详细的内容可以看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.
>

关闭服务

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
--30T11::41.753+ W NETWORK [thread1] Failed to connect to 127.0.0.1:, in(checking socket for error after poll), reason: Connection refused
--30T11::41.760+ E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js::
@(connect)::
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)

--30T15::24.360+ I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
--30T15::24.360+ I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
--30T15::24.360+ I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
--30T15::24.360+ I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
--30T15::24.360+ I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
--30T15::24.360+ 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)
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)
pipe size ( bytes, -p)
stack size (kbytes, -s)
cpu time (seconds, -t) unlimited
max user processes (-u)
virtual memory (kbytes, -v) unlimited

更改:

userdeMBP:test-sequelize user$ ulimit -n
userdeMBP:test-sequelize user$ ulimit -a
core file size (blocks, -c)
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)
pipe size ( bytes, -p)
stack size (kbytes, -s)
cpu time (seconds, -t) unlimited
max user processes (-u)
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.
> show dbs
--30T16::13.704+ E QUERY [thread1] Error: listDatabases failed:{
"ok" : ,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, $db: \"admin\" }",
"code" : ,
"codeName" : "Unauthorized"
} :

可见不能直接操作了

测试之前添加的用户:

> use admin
switched to db admin
> show dbs
--30T16::00.963+ E QUERY [thread1] Error: listDatabases failed:{
"ok" : ,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, $db: \"admin\" }",
"code" : ,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js::
Mongo.prototype.getDBs@src/mongo/shell/mongo.js::
shellHelper.show@src/mongo/shell/utils.js::
shellHelper@src/mongo/shell/utils.js::
@(shellhelp2)::
> db.auth('user','user')
Error: Authentication failed. > use test
switched to db test
> show dbs
--30T16::59.639+ E QUERY [thread1] Error: listDatabases failed:{
"ok" : ,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, $db: \"admin\" }",
"code" : ,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js::
Mongo.prototype.getDBs@src/mongo/shell/mongo.js::
shellHelper.show@src/mongo/shell/utils.js::
shellHelper@src/mongo/shell/utils.js::
@(shellhelp2)::
> db.auth('user','user') > show dbs
admin .000GB
config .000GB
local .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



mongodb的学习-3-在Mac上的安装配置的更多相关文章

  1. Mac上Hive安装配置

    Mac上Hive安装配置 1.安装 下载hive,地址:http://mirror.bit.edu.cn/apache/hive/ 之前我配置了集群,tjt01.tjt02.tjt03,这里hive安 ...

  2. MySql在Mac上的安装配置

    一.下载安装 官网下载社区版dmg安装文件: https://dev.mysql.com/downloads/mysql/ 1.执行安装文件,按步骤完成安装. 2.安装完成后终端输入: mysql - ...

  3. 一点MongoDB的基础及mongodb在mac上的安装

    最近发现维持写博客的习惯还是挺困难的,尤其对我来说,计划好的事过了好长时间才想到要去做. 这段时间一直在熟悉MongoDB,首先我是参考的这一篇:8天学通MongoDB   原博主写得非常好,我这里就 ...

  4. 《从0到1学习Flink》—— Mac 上搭建 Flink 1.6.0 环境并构建运行简单程序入门

    准备工作 1.安装查看 Java 的版本号,推荐使用 Java 8. 安装 Flink 2.在 Mac OS X 上安装 Flink 是非常方便的.推荐通过 homebrew 来安装. brew in ...

  5. Android Studio中mac上面的安装

    Android Studio中mac上面的安装 学习了:https://blog.csdn.net/xianrenli38/article/details/79347170 http://www.an ...

  6. Infer 在 Mac 上的安装和环境配置

    Infer 在 Mac 上的安装和环境配置 Infer 介绍 Infer 是一个静态分析工具.Infer 可以分析 Objective-C, Java 或者 C 代码,报告潜在的问题. 任何人都可以使 ...

  7. Mac上Node环境配置

    公司配备Mac笔记本,以前没用过mac开发项目,一开始依然是从node官网下载安装包,后来领导说最好是用brew安装软件,这样比较方便,安装和卸载,只要在命令行输入相应的 install 和 unin ...

  8. 【JMeter4.0学习(二)】之搭建openLDAP在windows8.1上的安装配置以及JMeter对LDAP服务器的性能测试脚本开发

    目录: 概述 安装测试环境 安装过程 配置启动 配置搭建OpenLDAP 给数据库添加数据 测试查询刚刚插入的数据 客户端介绍 JMeter建立一个扩展LDAP服务器的性能测试脚本开发 附:LDAP学 ...

  9. Mac上刚安装的WebStorm或PHPStorm遇到SVN版本太旧的问题

    Mac上刚安装的WebStorm或PHPStorm遇到SVN版本太旧的问题: URL: svn: E155021: This client is too old to work with the wo ...

  10. Mac上利用VScode配置c/c++开发环境

    Mac上利用VScode配置c/c++开发环境 哭辽,Typora里面最好不要插入表情,不然保存会闪退 首先你要有一个vscode 在扩展里面下载c/c++ 第一步 ⬆+com+p 打开命令模式:选择 ...

随机推荐

  1. 【代码笔记】iOS-GCD用法

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...

  2. 关于nginx下drupal7无法$_GET获取参数的问题

    参考:nginx 官方drupal 配置 - Drupal | NGINX 之前配置好了LNMP下的drupal7(7.59版本),简洁路径也配好了.但是在使用过程出现问题. 配置的nginx.con ...

  3. 在生产环境中安全执行更新删除SQL脚本的技巧

    今天在生产环境上解决问题,由于广发银行的管理制度是开发公司是不允许确生产环境的,所以我们只能把要更新的语句发给运营中心,由运营中心的投产人员执行,我们则在旁边看着:在他执行的时候发现了一个很有趣的技巧 ...

  4. Spring Boot—13事务支持

    pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  5. 玩转Android拍摄功能

    简单拍照与摄像 在富媒体开始流行之前,整个世界是一个灰暗且平淡无奇的地方.还记得Gopher吗?我或许不记得了.自从APP成为用户生活的一部分之后,这便给他们提供了一种方式可以来存放他们生活的细节.使 ...

  6. 润乾填报页面导入excel后增加js动作

     当页面从excel中导入数据之后,自动加入js的检查功能,下面是如何在导入excel后直接引入js的功能实例: var _orgImportExcel = report1_importExcel ...

  7. MyBatis -01- 初识 MyBatis + MyBatis 环境搭建

    MyBatis -01- 初识 MyBatis + MyBatis 环境搭建 MyBatis 本是 apache 的一个开源项目 iBatis(iBATIS = "internet" ...

  8. Android使用xml文件中的array资源

    Android中有种使用数组的非常简单的用法,在xml文件中获取. 创建数组资源 在value目录下创建arrays.xml文件 然后在arrays.xml文件中使用<string-array& ...

  9. 请求包含(Include)和请求转发(Forward)

    定义 请求包含是指将另一个Servlet的请求处理流程包含进来. 请求转发是指将请求转发给别的Servlet处理. 实现 实现请求包含和请求转发,可以使用HttpServletRequest的getR ...

  10. 实验二:klee处理未建模函数和处理error的方式

    首先,能够分析klee源码固然重要.但是目前尚未到那个地步.我按照我的过程,记录和分析我所做的实验. 结论性内容是: 1.klee处理printf传入符号值的情形时,报为error,不会将符号值具体化 ...