0%

mac下mongodb安装

自古逢秋悲寂寥,我言秋日胜春朝。晴空一鹤排云上,便引诗情到碧霄。 – 唐·刘禹锡 《秋词》

一、源码安装

  1. 下载 https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.11.tgz/usr/local/src
  2. 解压 sudo tar -zxvf mongodb-osx-ssl-x86_64-4.0.11.tgz
  3. 移动 sudo mv mongodb-osx-x86_64-4.0.11 /usr/local/mongodb-4.0.11
  4. 切换目录 cd /usr/local/mongodb-4.0.11
  5. 初始化数据目录和日志目录和日志文件 mkdir data && sudo mkdir logs && sudo touch ./logs/mongo.log
  6. 创建配置文件 sudo touch mongodb.conf,加入以下内容:
  7. 修改权限 sudo chmod -R 777 /usr/local/mongodb-4.0.11
1
2
3
4
5
6
7
8
dbpath = /usr/local/mongodb-4.0.11/data
logpath = /usr/local/mongodb-4.0.11/logs/mongo.log
port = 27017 ## 指定端口
fork = true ## 一后台进程方式启动
logappend=true ## 以追加方式记录日志
#maxConns=5000 ## 最大连接数
#storageEngine=mmapv1 ## 指定存储引擎,3.2+版本默认wireTiger
#nohttpinterface = true ## 2.6+版本已废弃
  • MAC下新版本(4.2.1)指定storageEngine=mmapv1选项时会报错,4.2已废弃此引擎,默认wiredTiger
  • MAC下提示Error parsing INI config file: unrecognised option 'nohttpinterface'

配置参数

  1. 启动服务 ./bin/mongod -f mongodb.conf,成功则有如下信息:
1
2
3
about to fork child process, waiting until server is ready for connections.
forked process: 80348
child process started successfully, parent exiting
  1. 进入mongo./bin/mongo --port 27017
    • 查看db支持的系统方法db.help()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
db.auth(username, password)
db.cloneDatabase(fromhost) - will only function with MongoDB 4.0 and below
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) - will only function with MongoDB 4.0 and below
db.createCollection(name, {size: ..., capped: ..., max: ...})
db.createUser(userDocument)
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.currentOp() displays currently executing operations in the db
db.dropDatabase(writeConcern)
db.dropUser(username)
db.eval() - deprecated
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getLogComponents()
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.getName()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
db.hostInfo() get details about the server's host
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
db.printSlaveReplicationInfo()
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj: 1}
db.serverStatus()
db.setLogLevel(level,<component>)
db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
db.setVerboseShell(flag) display extra information in shell output
db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
db.shutdownServer()
db.stats()
db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
db.version() current version of the server
db.watch() - opens a change stream cursor for a database to report on all changes to its non-system collections.
  1. 二、命令安装

TODO

三、参考

  1. 官网