前言

实习真的挺忙的, 主要是人多我又不熟, 不够自在, 笔记本小屏幕也不好操作, 只能看看视频学习, 不能方便操作.
redis我猛学1周, 已经基本可以了, 细节之处需要以后再慢慢修炼, 是时候了, 1天解决mongodb!(我在想peach, 叉腰)

下载, 安装

官网选择taz压缩文件for Linux(Centos), 然后下载即可
通过xftp之类的软件把它放到linux虚拟机某个目录中, 然后解压:
tar -zxvf 目录 -C 解压目录

我的是:
tar -zxvf mongodb-linux-x86_64-rhel80-5.0.1.tgz -C /usr/local

然后建议改名, 毕竟它名字太长了:
mv /usr/local/mongodb-linux-x86_64-rhel80-5.0.1/ /usr/local/mongodb

创建几个必要文件(夹):

  1. 创建数据存放文件夹
    mkdir -p /usr/local/mongodb/data/db
  2. 创建日志存放文件夹
    mkdir -p /usr/local/mongodb/logs
  3. 创建日志记录文件
    touch /usr/local/mongodb/logs/mongodb.log

启动

前台启动

前台启动命令:

1
2
3
4
# 切换至mongodb目录
cd /usr/local/mongodb
# 前台启动
bin/mongod --dbpath /usr/local/mongodb/db/ --logpath /usr/local/mongodb/logs/monodb.log --logappend --port 27017 --bind_ip 0.0.0.0
  • --dbpath ...: 指定数据文件存放目录
  • --logpath ...: 指定日志文件, 注意是文件不是目录
  • --logappend: 用追加的方式记录日志
  • --port ...: 端口号
  • --bing_ip ...: 绑定服务ip, 若绑定127.0.01, 则只能本机访问.
  • --fork: 后台启动, 不阻塞控制台.
  • --shutdown: 关闭
后台启动

不是–fork, 而是用配置文件启动

  1. 创建配置文件
    vim /etc/mongodb.conf
1
2
3
4
5
6
dbpath = /usr/local/mongodb/data/db
logpath = /usr/local/mongodb/logs/mongodb.log
logappend = true
port = 27017
bind_ip = 0.0.0.0
fork = true
  1. 启动

真的np, 一开始就遇到问题了, 我尝试多种方法运行, 肯定要先关掉打开的mongodb吧, 我直接kill -9, 结果再启动启动不了了, 百度了一下, mongodb有点娇贵, 要用kill -15或者正常用–shutdown, 不然有可能文件损坏, 人麻了.

我中间有个环节文件夹建错了, 索性直接删了重装.

bin/mongod -f /etc/mongodb.conf
启动成功

看的教程这人用kill -9, 最后了才说不要用这种方式, 人麻了.

配置环境变量

1
2
vim /etc/profile
source /etc/profile

profile:

1
2
export MONGODB_HOME=/usr/local/mongodb
export PATH=$PATH:$MONGODB_HOME/bin

什么你想要精通mongodb?

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
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.commandHelp(name) returns the help for the command
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().setSecondaryOk() allow queries on a replication secondary 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.hello() 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.printSecondaryReplicationInfo()
db.rotateCertificates(message) - rotates certificates, CRLs, and CA files and logs an optional message
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.

多不多? 其实用多了就会了
monogdb是C++写的, 这些都是它的内置函数