MongoDB

从真机传mongodb这个目录给50,里面有mongodb这个软件的tar包

# cd /root/mongodb/
# ls
mongodb-linux-x86_64-rhel70-3.6.3.tgz 这是mongodb软件的tar包

# tar -zxvf mongodb-linux-x86_64-rhel70-3.6.3.tgz 解压就行
# ls
mongodb-linux-x86_64-rhel70-3.6.3 mongodb-linux-x86_64-rhel70-3.6.3.tgz 解压后会生成同名目录

# cd mongodb-linux-x86_64-rhel70-3.6.3/ 进入同名目录下
# ls
bin GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES 里面有一个bin目录,还有其他一些文件

# mkdir /etc/mongodb 我们自己创建目录mongodb,用来放程序的相关文档
# cp -r bin/ /etc/mongodb/ 把解压后的同名目录下的bin目录,拷贝到我们新建的目录mongodb下
# cd /etc/mongodb/ 进入我们新建的目录mongodb

# mkdir -p log etc data/db 创建
# ls
bin data etc log

# cd /etc/mongodb/etc

# vim mongodb.conf
logpath=/etc/mongodb/log/mongodb.log
logappend=true
dbpath=/etc/mongodb/data/db
fork=true

# cd bin/
# ls
bsondump mongo mongodump mongofiles mongoperf mongorestore mongostat
install_compass mongod mongoexport mongoimport mongoreplay mongos mongotop


启动服务!mongod,有d的这个!是用来控制启动或关闭服务的!
# ./mongod -f /etc/mongodb/etc/mongodb.conf 在当前目录下,用./来启动服务,-f来指定读的配置文件
显示如下:
about to fork child process, waiting until server is ready for connections.
forked process: 2887
child process started successfully, parent exiting


如果不指定,服务默认的IP是127.0.0.1,端口号是27017。服务名是mongod
# ss -tunlp | grep mongod
tcp LISTEN 0 128 127.0.0.1:27017 *:* users:(("mongod",pid=2887,fd=11))


连上服务!mongo,没有d的这个是用来连接上数据库服务的!
# ./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-12-04T14:43:21.163+0800 I CONTROL [initandlisten]
2018-12-04T14:43:21.163+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-12-04T14:43:21.163+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-12-04T14:43:21.163+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-12-04T14:43:21.163+0800 I CONTROL [initandlisten]
2018-12-04T14:43:21.163+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-12-04T14:43:21.163+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-12-04T14:43:21.163+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2018-12-04T14:43:21.163+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-12-04T14:43:21.163+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-12-04T14:43:21.163+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2018-12-04T14:43:21.163+0800 I CONTROL [initandlisten]
2018-12-04T14:43:21.164+0800 I CONTROL [initandlisten]
2018-12-04T14:43:21.164+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-12-04T14:43:21.164+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-04T14:43:21.164+0800 I CONTROL [initandlisten]
2018-12-04T14:43:21.164+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-12-04T14:43:21.164+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-04T14:43:21.164+0800 I CONTROL [initandlisten]
>


关闭服务!mongod,有d的这个是用来控制启动或关闭服务的!
# ./mongod -f /etc/mongodb/etc/mongodb.conf --shutdown
killing process with pid: 2887


查看命令的有关选项!
# ./mongod --help
# ./mongod --help | grep -i shutdown 过滤出有关shutdown这个选项
interrupted by shutdown
--shutdown kill a running server (for init

服务启动后会自己生成日志,就是我们之前在配置文件里指定的日志路径/etc/mongodb/log/mongodb.log,还能用cat查看到日志里也有内容
# ls /etc/mongodb/log/
mongodb.log

 

# ss -tunlp | grep :27017 确认当前关闭服务
# ./mongod --help | grep -i port 查看有关端口的选项
--port arg specify port number - 27017 by default
--ipv6 enable IPv6 support (disabled by
<server:port>
cluster; default port 27019; default
cluster; default port 27018
--sslOnNormalPorts use ssl on configured ports


# ./mongod --help | grep -i bind 查看有关监听地址的选项
--bind_ip arg comma separated list of ip addresses to
--bind_ip_all bind to all ip addresses

 

# ss -tunlp | grep mongod

# vim etc/mongodb.conf
bind_ip=192.168.4.50 增加这行,指定IP
port=27050 增加这行,指定端口,可以用50来标示,和主机同名
logpath=/etc/mongodb/log/mongodb.log
logappend=true
dbpath=/etc/mongodb/data/db
fork=true

# cd bin/
# ./mongod -f /etc/mongodb/etc/mongodb.conf 重新启动服务
about to fork child process, waiting until server is ready for connections.
forked process: 5651
child process started successfully, parent exiting


# ss -tunlp | grep mongod 发现IP已变成192.168.4.50,端口已变成27050
tcp LISTEN 0 128 192.168.4.50:27050 *:* users:(("mongod",pid=5651,fd=11))
#######################################################################################
启动服务时,要用--host连接指定的IP,要用--port连接指定的端口。退出用exit。清屏也用ctrl+l。

# ./mongo --host 192.168.4.50 --port 27050
MongoDB shell version v3.6.3
connecting to: mongodb://192.168.4.50:27050/
MongoDB server version: 3.6.3
Server has startup warnings:
2018-12-04T15:11:09.139+0800 I CONTROL [initandlisten]
2018-12-04T15:11:09.139+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-12-04T15:11:09.139+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-12-04T15:11:09.139+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-12-04T15:11:09.139+0800 I CONTROL [initandlisten]
2018-12-04T15:11:09.139+0800 I CONTROL [initandlisten]
2018-12-04T15:11:09.139+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-12-04T15:11:09.139+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-04T15:11:09.139+0800 I CONTROL [initandlisten]
2018-12-04T15:11:09.139+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-12-04T15:11:09.139+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-04T15:11:09.139+0800 I CONTROL [initandlisten]
>

#########################################################################################
有关./mongo的所有帮助信息,记住这个命令是没有d的!它是来连接上服务的。

# ./mongo --help
显示如下:
MongoDB shell version v3.6.3
usage: ./mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.168.0.5/foo foo database on 192.168.0.5 machine
192.168.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
Options:
--shell run the shell after executing files
--nodb don't connect to mongod on startup - no
'db address' arg expected
--norc will not run the ".mongorc.js" file on
start up
--quiet be less chatty
--port arg port to connect to
--host arg server to connect to
--eval arg evaluate javascript
-h [ --help ] show this usage information
--version show version information
--verbose increase verbosity
--ipv6 enable IPv6 support (disabled by default)
--disableJavaScriptJIT disable the Javascript Just In Time
compiler
--disableJavaScriptProtection allow automatic JavaScript function
marshalling
--ssl use SSL for all connections
--sslCAFile arg Certificate Authority file for SSL
--sslPEMKeyFile arg PEM certificate/key file for SSL
--sslPEMKeyPassword arg password for key in PEM file for SSL
--sslCRLFile arg Certificate Revocation List file for SSL
--sslAllowInvalidHostnames allow connections to servers with
non-matching hostnames
--sslAllowInvalidCertificates allow connections to servers with invalid
certificates
--sslFIPSMode activate FIPS 140-2 mode at startup
--retryWrites automatically retry write operations upon
transient network errors
--jsHeapLimitMB arg set the js scope's heap size limit

Authentication Options:
-u [ --username ] arg username for authentication
-p [ --password ] arg password for authentication
--authenticationDatabase arg user source (defaults to dbname)
--authenticationMechanism arg authentication mechanism
--gssapiServiceName arg (=mongodb) Service name to use when authenticating
using GSSAPI/Kerberos
--gssapiHostName arg Remote host name to use for purpose of
GSSAPI/Kerberos authentication

file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
##############################################################################################
给命令的启动和关闭做别名

# vim /root/.bashrc
# User specific aliases and functions
alias linkm="/etc/mongodb/bin/mongo --host 192.168.4.50 --port 27050" 添加linkm来连接服务
alias mstart="/etc/mongodb/bin/mongod -f /etc/mongodb/etc/mongodb.conf" 添加mstart来作为启动mongodb服务
alias mstop="/etc/mongodb/bin/mongod -f /etc/mongodb/etc/mongodb.conf --shutdown" 添加mstop来作为关闭mongodb服务
... ...

# source /root/.bashrc
# linkm
##################################################################################
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db
test
> use gamedb
switched to db gamedb
> db
gamedb
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> show tables
> db
gamedb
> db.dropDatabase()
{ "ok" : 1 }
>

 

命令格式:
show dbs 查看已有的库
db 显示当前所在的库
use 库名 切换库,若库不存在延时创建库
show collections 或 show tables 查看库下已有集合
db.dropDatabase() 删除当前所在的库


2)集合管理
命令格式:
show collections 或 show tables 查看集合
db.集合名.drop() 删除集合
db.集合名.save({'',''}) 创建集合,集合不存在时,创建并添加文档


3)文档管理
命令格式:
db.集合名.find()
db.集合名.count()
db.集合名.insert({“name”:”jim”})
db.集合名.find(条件)
db.集合名.findOne() 返回查询一条文档
db.集合名.remove({}) 删除所有文档
db.集合名.remove({条件}) 删除与条件匹配的所有文档

 


> db.t1.count()
3
> db.t1.remove({name:"zhangsan"})
WriteResult({ "nRemoved" : 1 })
> db.t1.count()
2


> show tables
t1
> db.t1.find()
{ "_id" : ObjectId("5c0632b7d7b946618a950705"), "name" : "harry", "age" : 25, "classroom" : "nsd1808" }
{ "_id" : ObjectId("5c063579d7b946618a950707"), "name" : "tom" }
> db.t1.remove()
2018-12-04T16:08:41.060+0800 E QUERY [thread1] Error: remove needs a query :
DBCollection.prototype._parseRemove@src/mongo/shell/collection.js:357:1
DBCollection.prototype.remove@src/mongo/shell/collection.js:382:18
@(shell):1:1
> db.t1.remove({})
WriteResult({ "nRemoved" : 2 })
> db.t1.find()
> show tables
t1

 

 


> use db1
switched to db db1
> db
db1
> db.t1.save({name:"bob",age:21,likes:"book"})
WriteResult({ "nInserted" : 1 })


#############################################################################
常见错误:

> db.t1.found() 命令打错了,它会提示这不是一个函数
2018-12-04T15:52:24.594+0800 E QUERY [thread1] TypeError: db.t1.found is not a function :
@(shell):1:1

> show table 应该是tables,要有s。它现在提示不知道该怎么显示[table]这个东西
2018-12-04T15:52:40.592+0800 E QUERY [thread1] Error: don't know how to show [table] :
shellHelper.show@src/mongo/shell/utils.js:953:11
shellHelper@src/mongo/shell/utils.js:706:15
@(shellhelp2):1:1

> db.t1.save(name:"harry",age:25,classroom:"nsd1808") 少了一对花括号,所以提示(Syntax)语法报错
2018-12-04T15:53:57.829+0800 E QUERY [thread1] SyntaxError: missing ) after argument list @(shell):1:15

> db.t1.find({tom}) 查找的字符要用引号引起来,否认则提示 (Reference) 索引,参考报错
2018-12-04T15:56:15.706+0800 E QUERY [thread1] ReferenceError: tom is not defined :
@(shell):1:13

> db.t1.find({"tom"}) 提示缺少属性(property)
2018-12-04T15:56:21.070+0800 E QUERY [thread1] SyntaxError: missing : after property id @(shell):1:17
#######################################################################################

> show tables
t1
> db.t1.find()
> db.t1.save({姓名:"张三",age:19,住址:"深圳"})
WriteResult({ "nInserted" : 1 })
> db.t1.find()
{ "_id" : ObjectId("5c0640abd7b946618a950708"), "姓名" : "张三", "age" : 19, "住址" : "深圳" }
> db.t1.save({姓名:"李四",pay:8000,婚姻:false})
WriteResult({ "nInserted" : 1 })
> db.t1.find()
{ "_id" : ObjectId("5c0640abd7b946618a950708"), "姓名" : "张三", "age" : 19, "住址" : "深圳" }
{ "_id" : ObjectId("5c06410cd7b946618a950709"), "姓名" : "李四", "pay" : 8000, "婚姻" : false }
> db.t1.save({姓名:"王五",工资:null})
WriteResult({ "nInserted" : 1 })
> db.t1.find()
{ "_id" : ObjectId("5c0640abd7b946618a950708"), "姓名" : "张三", "age" : 19, "住址" : "深圳" }
{ "_id" : ObjectId("5c06410cd7b946618a950709"), "姓名" : "李四", "pay" : 8000, "婚姻" : false }
{ "_id" : ObjectId("5c064156d7b946618a95070a"), "姓名" : "王五", "工资" : null }


> db.t1.save({num:10})
WriteResult({ "nInserted" : 1 })
> db.t1.find({num:10})
{ "_id" : ObjectId("5c0641b8d7b946618a95070b"), "num" : 10 }
> db.t1.save({num2:10.11})
WriteResult({ "nInserted" : 1 })
> db.t1.find({num2:10.11})
{ "_id" : ObjectId("5c0641ead7b946618a95070c"), "num2" : 10.11 }
> db.t1.find()
{ "_id" : ObjectId("5c0640abd7b946618a950708"), "姓名" : "张三", "age" : 19, "住址" : "深圳" }
{ "_id" : ObjectId("5c06410cd7b946618a950709"), "姓名" : "李四", "pay" : 8000, "婚姻" : false }
{ "_id" : ObjectId("5c064156d7b946618a95070a"), "姓名" : "王五", "工资" : null }
{ "_id" : ObjectId("5c0641b8d7b946618a95070b"), "num" : 10 }
{ "_id" : ObjectId("5c0641ead7b946618a95070c"), "num2" : 10.11 }


> db.t1.save({name:"zhangsan",num:10})
WriteResult({ "nInserted" : 1 })
> db.t1.find({name:"zhangsan"})
{ "_id" : ObjectId("5c064420d7b946618a950711"), "name" : "zhangsan", "num" : 10 }

> db.t1.save({name:"lisi",num:10.11})
WriteResult({ "nInserted" : 1 })
> db.t1.find({name:"lisi"})
{ "_id" : ObjectId("5c064438d7b946618a950712"), "name" : "lisi", "num" : 10.11 }


> db.t1.save({name:"sunny",num:NumberInt(20)})
WriteResult({ "nInserted" : 1 })
> db.t1.find({name:"sunny"})
{ "_id" : ObjectId("5c0643b0d7b946618a950710"), "name" : "sunny", "num" : 20 }

> db.t1.save({name:"tom",num:NumberInt(20.22)})
WriteResult({ "nInserted" : 1 })
> db.t1.find({name:"tom"})
{ "_id" : ObjectId("5c0642d6d7b946618a95070e"), "name" : "tom", "num" : 20 }

> db.t1.save({name:"jerry",num:NumberLong(30)})
WriteResult({ "nInserted" : 1 })
> db.t1.find({name:"jerry"})
{ "_id" : ObjectId("5c064327d7b946618a95070f"), "name" : "jerry", "num" : NumberLong(30) }

> db.t1.save({name:"lucy",num:NumberLong(30.33)})
2018-12-04T17:05:46.672+0800 E QUERY [thread1] Error: number passed to NumberLong must be representable as an int64_t :
@(shell):1:29


> db.t1.save({name:"cherry",likes:["book","film","game"]})
WriteResult({ "nInserted" : 1 })
> db.t1.find({name:"cherry"})
{ "_id" : ObjectId("5c0644d3d7b946618a950713"), "name" : "cherry", "likes" : [ "book", "film", "game" ] }


> db.t1.save({
... lname:"daima",
... php:function(){/*<?php echo "hello world" ?>*/}
... })
WriteResult({ "nInserted" : 1 })
> db.t1.find({lname:"daima"})
{ "_id" : ObjectId("5c0645ded7b946618a950714"), "lname" : "daima", "php" : { "code" : "function (){/*<?php echo \"hello world\" ?>*/}" } }


> db.t1.save({class:"nsd1808",day:new Date()})
WriteResult({ "nInserted" : 1 })
> db.t1.find({class:"nsd1808"})
{ "_id" : ObjectId("5c064657d7b946618a950715"), "class" : "nsd1808", "day" : ISODate("2018-12-04T09:18:15.525Z") }
>


MongoDB中我们经常会接触到一个自动生成的字段:”_id”,类型为ObjectId。

ObjectId构成:
之前我们使用MySQL等关系型数据库时,主键都是设置成自增的。但在分布式环境下,这种方法就不可行了,会产生冲突。为此,MongoDB采用了一个称之为ObjectId的类型来做主键。ObjectId是一个12字节的 BSON 类型字符串。按照字节顺序,一次代表:

4字节:UNIX时间戳
3字节:表示运行MongoDB的机器
2字节:表示生成此_id的进程
3字节:由一个随机数开始的计数器生成的值


内嵌,其实就是redis的haxi表

> db.t1.save({bds:/^$/})
WriteResult({ "nInserted" : 1 })
> db.t1.find({bds:/^$/})
{ "_id" : ObjectId("5c0648a6d7b946618a950716"), "bds" : /^$/ }
> db.t1.save({bds2:/^a/})
WriteResult({ "nInserted" : 1 })
> db.t1.find({bds2:/^a/})
{ "_id" : ObjectId("5c0648d3d7b946618a950717"), "bds2" : /^a/ }

> db.t1.save({书:{书名:"运维之道"},作者:"丁明一",价钱:"60元",版本:2.0})
WriteResult({ "nInserted" : 1 })
> db.t1.find()
...
{ "_id" : ObjectId("5c0649a8d7b946618a95071b"), "书" : { "书名" : "运维之道" }, "作者" : "丁明一", "价钱" : "60元", "版本" : 2 }

 

 

# mkdir /mdb
# /etc/mongodb/bin/mongoexport --host 192.168.4.50 --port 27050 -d db1 -c t1 --type=json > /mdb/t1.json
2018-12-04T17:41:17.373+0800 connected to: 192.168.4.50:27050
2018-12-04T17:41:17.375+0800 exported 17 records
# ls /mdb/
t1.json

# cat /mdb/t1.json
{"_id":{"$oid":"5c0640abd7b946618a950708"},"姓名":"张三","age":19.0,"住址":"深圳"}
{"_id":{"$oid":"5c06410cd7b946618a950709"},"姓名":"李四","pay":8000.0,"婚姻":false}
{"_id":{"$oid":"5c064156d7b946618a95070a"},"姓名":"王五","工资":null}
{"_id":{"$oid":"5c0641b8d7b946618a95070b"},"num":10.0}
{"_id":{"$oid":"5c0641ead7b946618a95070c"},"num2":10.11}
{"_id":{"$oid":"5c064289d7b946618a95070d"},"number":20}
{"_id":{"$oid":"5c0642d6d7b946618a95070e"},"name":"tom","num":20}
{"_id":{"$oid":"5c064327d7b946618a95070f"},"name":"jerry","num":{"$numberLong":"30"}}
{"_id":{"$oid":"5c0643b0d7b946618a950710"},"name":"sunny","num":20}
{"_id":{"$oid":"5c064420d7b946618a950711"},"name":"zhangsan","num":10.0}
{"_id":{"$oid":"5c064438d7b946618a950712"},"name":"lisi","num":10.11}
{"_id":{"$oid":"5c0644d3d7b946618a950713"},"name":"cherry","likes":["book","film","game"]}
{"_id":{"$oid":"5c0645ded7b946618a950714"},"lname":"daima","php":{"$code":"function (){/*\u003c?php echo \"hello world\" ?\u003e*/}"}}
{"_id":{"$oid":"5c064657d7b946618a950715"},"class":"nsd1808","day":{"$date":"2018-12-04T09:18:15.525Z"}}
{"_id":{"$oid":"5c0648a6d7b946618a950716"},"bds":{"$regex":"^$","$options":""}}
{"_id":{"$oid":"5c0648d3d7b946618a950717"},"bds2":{"$regex":"^a","$options":""}}
{"_id":{"$oid":"5c0649a8d7b946618a95071b"},"书":{"书名":"运维之道"},"作者":"丁明一","价钱":"60元","版本":2.0}
###########################################################
格式是csv,要指定字段。如果某行的该字段比如name不存在,那么该行该列的对应值就是空

# /etc/mongodb/bin/mongoexport --host 192.168.4.50 --port 27050 -d db1 -c t1 -f name,age --type=csv > /mdb/t1.csv
2018-12-04T17:50:16.437+0800 connected to: 192.168.4.50:27050
2018-12-04T17:50:16.438+0800 exported 17 records
[root@client50 mdb]# cat /mdb/t1.csv
name,age
,19
,
,
,
,
,
tom,
jerry,
sunny,
zhangsan,
lisi,
cherry,
,
,
,
,
,
########################################################################
导入必须是对应的格式,如果导入的库和表不存在,它会自动创建。因为它是非关系型数据库,它能自动创建!

导入不用<小于号!
# /etc/mongodb/bin/mongoimport --host 192.168.4.50 --port 27050 -d db2 -c t2 --type=json /mdb/t1.json
2018-12-04T17:57:45.378+0800 connected to: 192.168.4.50:27050
2018-12-04T17:57:45.531+0800 imported 17 documents

 

> show dbs
admin 0.000GB
config 0.000GB
db1 0.000GB
db2 0.000GB 可以发现有db2库
local 0.000GB
> use db2
switched to db db2
> show tables
t2 可以发现有t2表
> db.t2.find() 表里面也有一样的内容
{ "_id" : ObjectId("5c0640abd7b946618a950708"), "姓名" : "张三", "age" : 19, "住址" : "深圳" }
{ "_id" : ObjectId("5c06410cd7b946618a950709"), "姓名" : "李四", "pay" : 8000, "婚姻" : false }
{ "_id" : ObjectId("5c064156d7b946618a95070a"), "姓名" : "王五", "工资" : null }
{ "_id" : ObjectId("5c0641b8d7b946618a95070b"), "num" : 10 }
{ "_id" : ObjectId("5c0641ead7b946618a95070c"), "num2" : 10.11 }
{ "_id" : ObjectId("5c064289d7b946618a95070d"), "number" : 20 }
{ "_id" : ObjectId("5c0642d6d7b946618a95070e"), "name" : "tom", "num" : 20 }
{ "_id" : ObjectId("5c064327d7b946618a95070f"), "name" : "jerry", "num" : NumberLong(30) }
{ "_id" : ObjectId("5c0643b0d7b946618a950710"), "name" : "sunny", "num" : 20 }
{ "_id" : ObjectId("5c064420d7b946618a950711"), "name" : "zhangsan", "num" : 10 }
{ "_id" : ObjectId("5c064438d7b946618a950712"), "name" : "lisi", "num" : 10.11 }
{ "_id" : ObjectId("5c0644d3d7b946618a950713"), "name" : "cherry", "likes" : [ "book", "film", "game" ] }
{ "_id" : ObjectId("5c0645ded7b946618a950714"), "lname" : "daima", "php" : { "code" : "function (){/*<?php echo \"hello world\" ?>*/}" } }
{ "_id" : ObjectId("5c064657d7b946618a950715"), "class" : "nsd1808", "day" : ISODate("2018-12-04T09:18:15.525Z") }
{ "_id" : ObjectId("5c0648a6d7b946618a950716"), "bds" : /^$/ }
{ "_id" : ObjectId("5c0648d3d7b946618a950717"), "bds2" : /^a/ }
{ "_id" : ObjectId("5c0649a8d7b946618a95071b"), "书" : { "书名" : "运维之道" }, "作者" : "丁明一", "价钱" : "60元", "版本" : 2 }

 

51-53准备mongodb

/etc/passwd 导入到userdb.user
先文件转为对应的文件格式


常见错误:配置文件写错,导致无法启动服务。可以根据报错信息来排查!

# ./mongod -f /etc/mongodb/etc/mongodb.conf 如果写错了,比如true,错写成了ture,它还会提示。
Error parsing INI config file: the argument ('ture') for option 'logappend' is invalid. Valid choices are 'on|off', 'yes|no', '1|0' and 'true|false'
try './mongod --help' for more information

原来是配置文件写错了,true,错写成了ture,改回来就好了。
logappend=true
... ...


# ./mongod -f /etc/mongodb/etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1702
ERROR: child process failed, exited with error number 100
To see additional information in this output, start without the "--fork" option. 它提示查看输出的信息

查看配置时指定的日志文件
# vim /etc/mongodb/log/mongodb.log
... ...
2018-12-04T19:21:57.887+0800 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory /etc/mongodb/etc/data/db not found., terminating 这里提示找不到这个目录/etc/mongodb/etc/data/db
2018-12-04T19:21:57.887+0800 I CONTROL [initandlisten] now exiting
2018-12-04T19:21:57.887+0800 I CONTROL [initandlisten] shutting down with code:100


原来是配置文件写错了/etc/mongodb/etc/data/db,真正路径是/etc/mongodb/data/db,改回来就好了。
vim /etc/mongodb/etc/mongodb.conf
dbpath=/etc/mongodb/data/db
... ...


一开始登陆上去,只有2个集合,少了一个config集合,这个没关系,它再等几分钟就会创建出来的
> show dbs
admin 0.000GB
local 0.000GB

 

posted @ 2019-04-30 22:36  安于夏  阅读(320)  评论(0编辑  收藏  举报