最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Mac中搭建本地MongoDB数据库

Mac crifan 17471浏览 0评论

折腾:

【已解决】把本地的音频字幕等数据存储到MongoDB中

期间,先去折腾本地的MongoDB,搭建起本地的环境,用于后续保存数据进去。

mac 安装 mongodb

mac install mongodb

官网:

Install MongoDB Community Edition on macOS — MongoDB Manual 3.6

Mac OSX 平台安装 MongoDB | 菜鸟教程

MAC OSX 安装MongoDB – 简书

Mac下安装MongoDB 及使用教程 – trigkit4 – SegmentFault 思否

Mac上安装MongoDB – 姜家志 – SegmentFault 思否

mac 下用 brew 安装mongodb – CSDN博客

mac 下用 brew 安装mongodb 一介布衣

Mac OS安装MongoDB – CSDN博客

mac 下用 brew 安装mongodb – CSDN博客

通过homebrew去安装

结果:

【已解决】brew update出错:error: Unable to get pack file mirrors.ustc.edu.cn pack

<code>➜  Homebrew git:(stable) brew install mongodb
==&gt; Installing dependencies for mongodb: openssl
==&gt; Installing mongodb dependency: openssl
==&gt; Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2o.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==&gt; Pouring openssl-1.0.2o.high_sierra.bottle.tar.gz
==&gt; Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' &gt;&gt; ~/.zshrc

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig

==&gt; Summary
🍺  /usr/local/Cellar/openssl/1.0.2o: 1,792 files, 12.3MB
==&gt; Installing mongodb
==&gt; Downloading https://homebrew.bintray.com/bottles/mongodb-3.6.3.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==&gt; Pouring mongodb-3.6.3.high_sierra.bottle.tar.gz
==&gt; 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
==&gt; Summary
🍺  /usr/local/Cellar/mongodb/3.6.3: 19 files, 297.6MB
</code>

安装好后,去看看安装的位置,以及命令:

<code>➜  Homebrew git:(stable) which mongod
/usr/local/bin/mongod
➜  Homebrew git:(stable) mongod --help
Options:

General options:
  -h [ --help ]                         show this usage information
  --version                             show version information
  -f [ --config ] arg                   configuration file specifying
                                        additional options
  -v [ --verbose ] [=arg(=v)]           be more verbose (include multiple times
                                        for more verbosity e.g. -vvvvv)
  --quiet                               quieter output
  --port arg                            specify port number - 27017 by default
  --bind_ip arg                         comma separated list of ip addresses to
                                        listen on - localhost by default
  --bind_ip_all                         bind to all ip addresses
  --ipv6                                enable IPv6 support (disabled by
                                        default)
  --listenBacklog arg (=128)            set socket listen backlog size
  --maxConns arg                        max number of simultaneous connections
                                        - 1000000 by default
  --logpath arg                         log file to send write to instead of
                                        stdout - has to be a file, not
                                        directory
  --syslog                              log to system's syslog facility instead
                                        of file or stdout
  --syslogFacility arg                  syslog facility used for mongodb syslog
                                        message
  --logappend                           append to logpath instead of
                                        over-writing
  --logRotate arg                       set the log rotation behavior
                                        (rename|reopen)
  --timeStampFormat arg                 Desired format for timestamps in log
                                        messages. One of ctime, iso8601-utc or
                                        iso8601-local
  --pidfilepath arg                     full path to pidfile (if not set, no
                                        pidfile is created)
  --timeZoneInfo arg                    full path to time zone info directory,
                                        e.g. /usr/share/zoneinfo
  --keyFile arg                         private key for cluster authentication
  --noauth                              run without security
  --setParameter arg                    Set a configurable parameter
  --transitionToAuth                    For rolling access control upgrade.
                                        Attempt to authenticate over outgoing
                                        connections and proceed regardless of
                                        success. Accept incoming connections
                                        with or without authentication.
  --clusterAuthMode arg                 Authentication mode used for cluster
                                        authentication. Alternatives are
                                        (keyFile|sendKeyFile|sendX509|x509)
  --nounixsocket                        disable listening on unix sockets
  --unixSocketPrefix arg                alternative directory for UNIX domain
                                        sockets (defaults to /tmp)
  --filePermissions arg                 permissions to set on UNIX domain
                                        socket file - 0700 by default
  --fork                                fork server process
  --networkMessageCompressors [=arg(=disabled)] (=snappy)
                                        Comma-separated list of compressors to
                                        use for network messages
  --auth                                run with security
  --clusterIpSourceWhitelist arg        Network CIDR specification of permitted
                                        origin for `__system` access.
  --slowms arg (=100)                   value of slow for profile and console
                                        log
  --slowOpSampleRate arg (=1)           fraction of slow ops to include in the
                                        profile and console log
  --profile arg                         0=off 1=slow, 2=all
  --cpu                                 periodically show cpu and iowait
                                        utilization
  --sysinfo                             print some diagnostic system
                                        information
  --noIndexBuildRetry                   don't retry any index builds that were
                                        interrupted by shutdown
  --noscripting                         disable scripting engine
  --notablescan                         do not allow table scans

Replication options:
  --oplogSize arg                       size to use (in MB) for replication op
                                        log. default is 5% of disk space (i.e.
                                        large is good)

Master/slave options (old; use replica sets instead):
  --master                              master mode
  --slave                               slave mode
  --source arg                          when slave: specify master as
                                        &lt;server:port&gt;
  --only arg                            when slave: specify a single database
                                        to replicate
  --slavedelay arg                      specify delay (in seconds) to be used
                                        when applying master ops to slave
  --autoresync                          automatically resync if slave data is
                                        stale

Replica set options:
  --replSet arg                         arg is &lt;setname&gt;[/&lt;optionalseedhostlist
                                        &gt;]
  --replIndexPrefetch arg               specify index prefetching behavior (if
                                        secondary) [none|_id_only|all]
  --enableMajorityReadConcern [=arg(=1)] (=1)
                                        enables majority readConcern

Sharding options:
  --configsvr                           declare this is a config db of a
                                        cluster; default port 27019; default
                                        dir /data/configdb
  --shardsvr                            declare this is a shard db of a
                                        cluster; default port 27018

SSL options:
  --sslOnNormalPorts                    use ssl on configured ports
  --sslMode arg                         set the SSL operation mode
                                        (disabled|allowSSL|preferSSL|requireSSL
                                        )
  --sslPEMKeyFile arg                   PEM file for ssl
  --sslPEMKeyPassword arg               PEM file password
  --sslClusterFile arg                  Key file for internal SSL
                                        authentication
  --sslClusterPassword arg              Internal authentication key file
                                        password
  --sslCAFile arg                       Certificate Authority file for SSL
  --sslCRLFile arg                      Certificate Revocation List file for
                                        SSL
  --sslDisabledProtocols arg            Comma separated list of TLS protocols
                                        to disable [TLS1_0,TLS1_1,TLS1_2]
  --sslWeakCertificateValidation        allow client to connect without
                                        presenting a certificate
  --sslAllowConnectionsWithoutCertificates
                                        allow client to connect without
                                        presenting a certificate
  --sslAllowInvalidHostnames            Allow server certificates to provide
                                        non-matching hostnames
  --sslAllowInvalidCertificates         allow connections to servers with
                                        invalid certificates
  --sslFIPSMode                         activate FIPS 140-2 mode at startup

Storage options:
  --storageEngine arg                   what storage engine to use - defaults
                                        to wiredTiger if no data files present
  --dbpath arg                          directory for datafiles - defaults to
                                        /data/db
  --directoryperdb                      each database will be stored in a
                                        separate directory
  --noprealloc                          disable data file preallocation - will
                                        often hurt performance
  --nssize arg (=16)                    .ns file size (in MB) for new databases
  --quota                               limits each database to a certain
                                        number of files (8 default)
  --quotaFiles arg                      number of files allowed per db, implies
                                        --quota
  --smallfiles                          use a smaller default file size
  --syncdelay arg (=60)                 seconds between disk syncs (0=never,
                                        but not recommended)
  --upgrade                             upgrade db if needed
  --repair                              run repair on all dbs
  --repairpath arg                      root directory for repair files -
                                        defaults to dbpath
  --journal                             enable journaling
  --nojournal                           disable journaling (journaling is on by
                                        default for 64 bit)
  --journalOptions arg                  journal diagnostic options
  --journalCommitInterval arg           how often to group/batch commit (ms)

WiredTiger options:
  --wiredTigerCacheSizeGB arg           maximum amount of memory to allocate
                                        for cache; defaults to 1/2 of physical
                                        RAM
  --wiredTigerJournalCompressor arg (=snappy)
                                        use a compressor for log records
                                        [none|snappy|zlib]
  --wiredTigerDirectoryForIndexes       Put indexes and data in different
                                        directories
  --wiredTigerCollectionBlockCompressor arg (=snappy)
                                        block compression algorithm for
                                        collection data [none|snappy|zlib]
  --wiredTigerIndexPrefixCompression arg (=1)
                                        use prefix compression on row-store
                                        leaf pages
</code>

看看版本:

<code>➜  Homebrew git:(stable) mongod --version
db version v3.6.3
git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
allocator: system
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64
</code>

是3.6.3的版本

继续看看官网:

The mongo Shell — MongoDB Manual 3.6

The mongo shell is an interactive JavaScript interface to MongoDB

Install MongoDB — MongoDB Manual 3.6

好像还分两个版本:

  • MongoDB Enterprise

  • MongoDB Community Edition

不知道啥关系

Introduction to MongoDB — MongoDB Manual 3.6

Aggregation Pipeline — MongoDB Manual 3.6

Text Search — MongoDB Manual 3.6

Getting Started — MongoDB Manual 3.6

运行mongod

<code>➜  Homebrew git:(stable) mongod
2018-03-28T11:24:25.284+0800 I CONTROL  [initandlisten] MongoDB starting : pid=57950 port=27017 dbpath=/data/db 64-bit host=promote.cache-dns.local
2018-03-28T11:24:25.284+0800 I CONTROL  [initandlisten] db version v3.6.3
2018-03-28T11:24:25.284+0800 I CONTROL  [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
2018-03-28T11:24:25.284+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
2018-03-28T11:24:25.284+0800 I CONTROL  [initandlisten] allocator: system
2018-03-28T11:24:25.284+0800 I CONTROL  [initandlisten] modules: none
2018-03-28T11:24:25.284+0800 I CONTROL  [initandlisten] build environment:
2018-03-28T11:24:25.284+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-03-28T11:24:25.284+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-03-28T11:24:25.284+0800 I CONTROL  [initandlisten] options: {}
2018-03-28T11:24:25.285+0800 I STORAGE  [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2018-03-28T11:24:25.285+0800 I CONTROL  [initandlisten] now exiting
2018-03-28T11:24:25.285+0800 I CONTROL  [initandlisten] shutting down with code:100
</code>

结果无法打开:

http://127.0.0.1:28017/

看到了:

Data directory /data/db not found

所以去创建:

<code>➜  Homebrew git:(stable) mkdir -p /data/db
mkdir: /data/db: Permission denied
</code>

用sudo:

<code>➜  Homebrew git:(stable) sudo mkdir -p /data/db
Password:
</code>

但是接着运行mongod又提示,文件夹只读:

<code>➜  Homebrew git:(stable) mongod
2018-03-28T11:25:56.228+0800 I CONTROL  [initandlisten] MongoDB starting : pid=58037 port=27017 dbpath=/data/db 64-bit host=promote.cache-dns.local
2018-03-28T11:25:56.228+0800 I CONTROL  [initandlisten] db version v3.6.3
2018-03-28T11:25:56.228+0800 I CONTROL  [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
2018-03-28T11:25:56.228+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
2018-03-28T11:25:56.228+0800 I CONTROL  [initandlisten] allocator: system
2018-03-28T11:25:56.228+0800 I CONTROL  [initandlisten] modules: none
2018-03-28T11:25:56.228+0800 I CONTROL  [initandlisten] build environment:
2018-03-28T11:25:56.228+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-03-28T11:25:56.228+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-03-28T11:25:56.228+0800 I CONTROL  [initandlisten] options: {}
2018-03-28T11:25:56.229+0800 I STORAGE  [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating
2018-03-28T11:25:56.229+0800 I CONTROL  [initandlisten] now exiting
2018-03-28T11:25:56.229+0800 I CONTROL  [initandlisten] shutting down with code:100
</code>

所以再去看看权限:

<code>➜  Homebrew git:(stable) ll /data
total 0
drwxr-xr-x  2 root  wheel    64B  3 28 11:25 db
➜  Homebrew git:(stable) whoami
crifan
</code>

所以当前用户crifan无法操作新建的/data/db

所以去更换所有者:

<code>➜  Homebrew git:(stable) chown -R crifan /data/db
chown: /data/db: Operation not permitted
➜  Homebrew git:(stable) sudo chown -R crifan /data/db
➜  Homebrew git:(stable) ll /data
total 0
drwxr-xr-x  2 crifan  wheel    64B  3 28 11:25 db
</code>

然后再试试,终于可以了:

<code>➜  Homebrew git:(stable) mongod
2018-03-28T11:29:30.373+0800 I CONTROL  [initandlisten] MongoDB starting : pid=58272 port=27017 dbpath=/data/db 64-bit host=promote.cache-dns.local
2018-03-28T11:29:30.373+0800 I CONTROL  [initandlisten] db version v3.6.3
2018-03-28T11:29:30.373+0800 I CONTROL  [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
2018-03-28T11:29:30.373+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
2018-03-28T11:29:30.373+0800 I CONTROL  [initandlisten] allocator: system
2018-03-28T11:29:30.373+0800 I CONTROL  [initandlisten] modules: none
2018-03-28T11:29:30.373+0800 I CONTROL  [initandlisten] build environment:
2018-03-28T11:29:30.373+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-03-28T11:29:30.373+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-03-28T11:29:30.373+0800 I CONTROL  [initandlisten] options: {}
2018-03-28T11:29:30.374+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3584M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten]
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten]
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip &lt;address&gt; to specify which IP
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten]
2018-03-28T11:29:31.249+0800 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: 48a9bf5e-6323-420a-9b86-b28327f909f0
2018-03-28T11:29:31.310+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.6
2018-03-28T11:29:31.316+0800 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: 8303ba05-ef0d-43ac-bfa2-477548e1c583
2018-03-28T11:29:31.362+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2018-03-28T11:29:31.363+0800 I NETWORK  [initandlisten] waiting for connections on port 27017

</code>

然后再去打开:

http://127.0.0.1:27017/

还是不行。

http://localhost:27017/

也不行。

然后有新log:

<code>2018-03-28T11:30:02.858+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:62864 #1 (1 connection now open)
</code>

2018-03-28T11:30:02.858+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:62865 #2 (2 connections now open)

2018-03-28T11:30:02.858+0800 I NETWORK  [conn1] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:62864 (connection id: 1)

2018-03-28T11:30:02.859+0800 I NETWORK  [conn1] end connection 127.0.0.1:62864 (1 connection now open)

2018-03-28T11:30:02.859+0800 I NETWORK  [conn2] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:62865 (connection id: 2)

2018-03-28T11:30:02.859+0800 I NETWORK  [conn2] end connection 127.0.0.1:62865 (0 connections now open)

2018-03-28T11:30:02.860+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:62866 #3 (1 connection now open)

2018-03-28T11:30:02.860+0800 I NETWORK  [conn3] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:62866 (connection id: 3)

2018-03-28T11:30:02.860+0800 I NETWORK  [conn3] end connection 127.0.0.1:62866 (0 connections now open)

2018-03-28T11:30:17.235+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:62955 #4 (1 connection now open)

2018-03-28T11:30:17.235+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:62957 #5 (2 connections now open)

2018-03-28T11:30:17.237+0800 I NETWORK  [conn4] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:62955 (connection id: 4)

2018-03-28T11:30:17.237+0800 I NETWORK  [conn4] end connection 127.0.0.1:62955 (1 connection now open)

2018-03-28T11:30:17.239+0800 I NETWORK  [conn5] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:62957 (connection id: 5)

2018-03-28T11:30:17.239+0800 I NETWORK  [conn5] end connection 127.0.0.1:62957 (0 connections now open)

2018-03-28T11:30:17.240+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:62959 #6 (1 connection now open)

2018-03-28T11:30:17.240+0800 I NETWORK  [conn6] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:62959 (connection id: 6)

2018-03-28T11:30:17.240+0800 I NETWORK  [conn6] end connection 127.0.0.1:62959 (0 connections now open)

2018-03-28T11:30:17.535+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:62967 #7 (1 connection now open)

2018-03-28T11:30:17.536+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:62969 #8 (2 connections now open)

2018-03-28T11:30:17.540+0800 I NETWORK  [conn7] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:62967 (connection id: 7)

2018-03-28T11:30:17.540+0800 I NETWORK  [conn7] end connection 127.0.0.1:62967 (1 connection now open)

2018-03-28T11:30:17.541+0800 I NETWORK  [conn8] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:62969 (connection id: 8)

2018-03-28T11:30:17.541+0800 I NETWORK  [conn8] end connection 127.0.0.1:62969 (0 connections now open)

2018-03-28T11:30:17.542+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:62971 #9 (1 connection now open)

2018-03-28T11:30:17.543+0800 I NETWORK  [conn9] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:62971 (connection id: 9)

2018-03-28T11:30:17.543+0800 I NETWORK  [conn9] end connection 127.0.0.1:62971 (0 connections now open)

2018-03-28T11:30:22.564+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:63004 #10 (1 connection now open)

2018-03-28T11:30:22.564+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:63006 #11 (2 connections now open)

2018-03-28T11:30:22.566+0800 I NETWORK  [conn10] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:63004 (connection id: 10)

2018-03-28T11:30:22.566+0800 I NETWORK  [conn10] end connection 127.0.0.1:63004 (1 connection now open)

2018-03-28T11:30:22.572+0800 I NETWORK  [conn11] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:63006 (connection id: 11)

2018-03-28T11:30:22.572+0800 I NETWORK  [conn11] end connection 127.0.0.1:63006 (0 connections now open)

2018-03-28T11:30:22.576+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:63008 #12 (1 connection now open)

2018-03-28T11:30:22.578+0800 I NETWORK  [conn12] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:63008 (connection id: 12)

2018-03-28T11:30:22.579+0800 I NETWORK  [conn12] end connection 127.0.0.1:63008 (0 connections now open)

2018-03-28T11:30:52.595+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:63198 #13 (1 connection now open)

2018-03-28T11:30:52.595+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:63199 #14 (2 connections now open)

2018-03-28T11:30:52.596+0800 I NETWORK  [conn13] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:63198 (connection id: 13)

2018-03-28T11:30:52.597+0800 I NETWORK  [conn13] end connection 127.0.0.1:63198 (1 connection now open)

2018-03-28T11:30:52.597+0800 I NETWORK  [conn14] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:63199 (connection id: 14)

2018-03-28T11:30:52.597+0800 I NETWORK  [conn14] end connection 127.0.0.1:63199 (0 connections now open)

2018-03-28T11:30:52.598+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:63201 #15 (1 connection now open)

2018-03-28T11:30:52.598+0800 I NETWORK  [conn15] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:63201 (connection id: 15)

2018-03-28T11:30:52.598+0800 I NETWORK  [conn15] end connection 127.0.0.1:63201 (0 connections now open)

2018-03-28T11:31:52.632+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:63574 #16 (1 connection now open)

2018-03-28T11:31:52.633+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:63575 #17 (2 connections now open)

2018-03-28T11:31:52.635+0800 I NETWORK  [conn16] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:63574 (connection id: 16)

2018-03-28T11:31:52.635+0800 I NETWORK  [conn16] end connection 127.0.0.1:63574 (1 connection now open)

2018-03-28T11:31:52.636+0800 I NETWORK  [conn17] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:63575 (connection id: 17)

2018-03-28T11:31:52.636+0800 I NETWORK  [conn17] end connection 127.0.0.1:63575 (0 connections now open)

2018-03-28T11:31:52.637+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:63577 #18 (1 connection now open)

2018-03-28T11:31:52.637+0800 I NETWORK  [conn18] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:63577 (connection id: 18)

2018-03-28T11:31:52.637+0800 I NETWORK  [conn18] end connection 127.0.0.1:63577 (0 connections now open)

参考:

mac 下用 brew 安装mongodb – CSDN博客

原来:

mongod是服务端

再去新开一个终端,运行客户端:

mongo

<code>➜  ~ which mongo
/usr/local/bin/mongo
➜  ~ mongo --version
MongoDB shell version v3.6.3
git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
allocator: system
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64
</code>

还是参考:

The mongo Shell — MongoDB Manual 3.6

好好看看,再操作

看来官网的教程,还是不够清晰:

Once you have installed and have started MongoDB

点击进去后,结果只有install没有介绍如何start的

mongo — MongoDB Manual 3.6

mongo — MongoDB Manual 3.6

去试试:

<code>➜  ~ mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
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:
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten]
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten]
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip &lt;address&gt; to specify which IP
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-03-28T11:29:31.241+0800 I CONTROL  [initandlisten]
&gt; db
test
&gt; help
    db.help()                    help on db methods
    db.mycoll.help()             help on collection methods
    sh.help()                    sharding helpers
    rs.help()                    replica set helpers
    help admin                   administrative help
    help connect                 connecting to a db help
    help keys                    key shortcuts
    help misc                    misc things to know
    help mr                      mapreduce

    show dbs                     show database names
    show collections             show collections in current database
    show users                   show users in current database
    show profile                 show most recent system.profile entries with time &gt;= 1ms
    show logs                    show the accessible logger names
    show log [name]              prints out the last segment of log in memory, 'global' is default
    use &lt;db_name&gt;                set current database
    db.foo.find()                list objects in collection foo
    db.foo.find( { a : 1 } )     list objects in foo where a == 1
    it                           result of the last line evaluated; use to further iterate
    DBQuery.shellBatchSize = x   set default number of items to display on shell
    exit                         quit the mongo shell
&gt;
</code>

继续尝试:

<code>&gt; show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
&gt; show collections
&gt; show users
&gt; show profile
db.system.profile is empty
Use db.setProfilingLevel(2) will enable profiling
Use db.system.profile.find() to show raw profile entries
&gt; show logs
global
startupWarnings
</code>

参考:

Mac上安装MongoDB – 姜家志 – SegmentFault 思否

mac 下用 brew 安装mongodb – CSDN博客

继续试试insert

MongoDB 概念解析 | 菜鸟教程

<code>&gt; use test
switched to db test
&gt; show collections
&gt; db.testCollection.insert({curDate: "2018-03-28"});
WriteResult({ "nInserted" : 1 })
&gt; db.testCollection.find()
{ "_id" : ObjectId("5abb109fcbcbd3510ce04cb5"), "curDate" : "2018-03-28" }
&gt; db.testCollection.find().pretty()
{ "_id" : ObjectId("5abb109fcbcbd3510ce04cb5"), "curDate" : "2018-03-28" }
</code>

【总结】

此处再Mac中,先去:

<code>brew update
</code>

确保源是最新的,然后再去安装mongodb:

<code>brew install mongodb
</code>

(另外,通过:

(1)

<code>which mongo
</code>

可以看到此处安装位置是:

/usr/local/bin/mongo

(2)通过:

<code>mongo --version
</code>

可以看到Mongo客户端版本是:

MongoDB shell version v3.6.3

(3)通过

<code>➜  英语资源 mongod --version
db version v3.6.3
git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
allocator: system
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64
</code>

可以查看到MongoDB服务端的版本也是3.6.3)

接着在使用mongody之前,需要去创建用于保存数据的文件夹:

<code>sudo mkdir -p /data/db
</code>

然后通过

<code>whoami
</code>

确认当前用户,再去把文件夹所有者改为当前用户:

<code>sudo chown -R crifan /data/db
</code>

然后再去:

运行mongo的服务端:

<code>mongod
</code>

能看到类似于:

waiting for connections on port 27017

的表示服务启动正常了

接着再去新开一个终端,然后运行mongo的客户端:

<code>mongo
</code>

即可进去mongo的shell,然后就可以折腾了。

比如:

<code>db
</code>

或:

<code>show dbs
</code>

可以看到当前有哪些数据库。

新建mongo时,貌似会自动创建一个test数据库?

用:

<code>use test
</code>

表示使用test数据库。

用:

<code>help
</code>

可以查看帮助。

相关的尝试是:

<code>&gt; db
test
&gt; show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
&gt; show collections
&gt; show users
&gt; show profile
db.system.profile is empty
Use db.setProfilingLevel(2) will enable profiling
Use db.system.profile.find() to show raw profile entries
&gt; show logs
global
startupWarnings
&gt; use test
switched to db test
&gt; show collections
&gt; db.testCollection.insert({curDate: "2018-03-28"});
WriteResult({ "nInserted" : 1 })
&gt; db.testCollection.find()
{ "_id" : ObjectId("5abb109fcbcbd3510ce04cb5"), "curDate" : "2018-03-28" }
&gt; db.testCollection.find().pretty()
{ "_id" : ObjectId("5abb109fcbcbd3510ce04cb5"), "curDate" : "2018-03-28" }
</code>

转载请注明:在路上 » 【已解决】Mac中搭建本地MongoDB数据库

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
85 queries in 0.238 seconds, using 22.21MB memory