部署mongDB数据库服务

部署mongDB数据库服务

192.168.4.51主机

配置步骤:

装包

    ls mongodb-linux-x86_64-rhel70-3.6.3.tgz

    tar -zxf mongodb-linux-x86_64-rhel70-3.6.3.tgz 

    cp -r mongodb-linux-x86_64-rhel70-3.6.3/bin  /usr/local/mongodb/

    cd /usr/local/mongodb/

    mkdir -p etc log data/db

修改配置服务

vim /usr/local/mongodb/etc/mongodb.conf

dbpath=/usr/local/mongodb/data/db

fork=true

logpath=/usr/local/mongodb/log/mongodb.log

logappend=true

bind_ip=192.168.4.51

port=27051

vim /etc/profile

export PATH=/usr/local/mongodb/bin:$PATH

source /etc/profile

Vim /etc/bashrc

alias mstop=’mongod --shutdown -f /usr/local/mongodb/etc/mongodb.conf’

alias mstar=’mongod -f /usr/local/mongodb/etc/mongodb.conf’

source  /etc/bashrc

启动服务

Mstart

查看服务信息

ss -unltp | grep :27051

tcp    LISTEN     0      128    192.168.4.51:27051                 *:*                   users:(("mongod",pid=1465,fd=11))

连接服务信息

mongo --host 192.168.4.51 --port 27051

杀掉进程

Killall -9 mongod

区分大小写

//延时创建

> show dbs   //查看所有库

> use studb  //创建集合  有的时候使用,没用时创建

switched to db studb

> db

studb

> show tables 

> db.c1.save({name:"bob",age:19,sex:"girl"}) //创建文档    //相当于mysql行记录

WriteResult({ "nInserted" : 1 })

> db.c1.save({name:"bob",class:"nsd1803"})

WriteResult({ "nInserted" : 1 })

> db.c1.find()

{ "_id" : ObjectId("5b403a22d701a74e97d8457f"), "name" : "bob", "age" : 19, "sex" : "girl" }

{ "_id" : ObjectId("5b403a4ad701a74e97d84580"), "name" : "bob", "class" : "nsd1803" }

>

代码类型

> db.c1.save({lname:"php",codeformat:function(){/*<?php echo "hello world" ?> */}})

> db.c1.find()

{ "_id" : ObjectId("5b4061ac020926a1487b189f"), "lname" : "php", "codeformat" : { "code" : "function (){/*<?php echo \"hello world\" ?> */}" } }

日期类型

> db.c1.save({

... name:"lilei",birthday:new Date() })

WriteResult({ "nInserted" : 1 })

> db.c1.find({name:"lilei"})

{ "_id" : ObjectId("5b406351020926a1487b18a0"), "name" : "lilei", "birthday" : ISODate("2018-07-07T06:53:05.555Z") }

对象类型

> db.c1.save({name:"alice",stuid:ObjectId()})

WriteResult({ "nInserted" : 1 })

> db.c1.find({name:"alice"})

{ "_id" : ObjectId("5b406498020926a1487b18a1"), "name" : "alice", "stuid" : {  } }

{ "_id" : ObjectId("5b40650a020926a1487b18a2"), "name" : "alice", "stuid" : {  } }

{ "_id" : ObjectId("5b406536020926a1487b18a4"), "name" : "alice", "stuid" : ObjectId("5b406536020926a1487b18a3") }

>

内嵌类型

> db.c1.save({ ywzd:{p:"dmy",jg:69,v:2}, "ngsfc":{ p:"birdg",jg:89,v:3} })

WriteResult({ "nInserted" : 1 })

正则类型

> db.c1.save({name:"hanmm",match:/^a/})

WriteResult({ "nInserted" : 1 })

数据导入导出

> show dbs

admin   0.000GB

config  0.000GB

local   0.000GB

studb   0.000GB

test    0.000GB

> use studb

switched to db studb

> show tables

c1

Json格式:

数据导出:

Mongoimport --help

[root@host50 ~]# mongoexport --host 192.168.4.50 --port 27050 -d studb -c c1 --type=json

2018-07-07T15:53:12.275+0800 connected to: 192.168.4.50:27050

{"_id":{"$oid":"5b403a22d701a74e97d8457f"},"name":"bob","age":19.0,"sex":"girl"}

{"_id":{"$oid":"5b403a4ad701a74e97d84580"},"name":"bob","class":"nsd1803"}

2018-07-07T15:53:12.277+0800 exported 2 records

[root@host50 ~]# mkdir /mongodbdir

[root@host50 ~]# mongoexport --host 192.168.4.50 --port 27050 -d studb -c c1 --type=json > /mongodbdir/c1.json

2018-07-07T15:53:56.622+0800 connected to: 192.168.4.50:27050

2018-07-07T15:53:56.623+0800 exported 2 records

[root@host50 ~]# ls /mongodbdir/c1.json -l

-rw-r--r--. 1 root root 156 7月   7 15:53 /mongodbdir/c1.json

[root@host50 ~]# cat /mongodbdir/c1.json 

{"_id":{"$oid":"5b403a22d701a74e97d8457f"},"name":"bob","age":19.0,"sex":"girl"}

{"_id":{"$oid":"5b403a4ad701a74e97d84580"},"name":"bob","class":"nsd1803"}

数据导入:

[root@host50 ~]# mongoimport --host 192.168.4.50 --port 27050 -d bbsdb -c t1 --type=json /mongodbdir/c1.json

2018-07-07T16:22:43.799+0800 connected to: 192.168.4.50:27050

2018-07-07T16:22:43.996+0800 imported 2 documents

[root@host50 ~]# mongo --host 192.168.4.50 --port 27050

> show dbs

admin   0.000GB

bbsdb   0.000GB

config  0.000GB

local   0.000GB

studb   0.000GB

test    0.000GB

> use bbsdb

switched to db bbsdb

> db.t1.find()

{ "_id" : ObjectId("5b403a22d701a74e97d8457f"), "name" : "bob", "age" : 19, "sex" : "girl" }

{ "_id" : ObjectId("5b403a4ad701a74e97d84580"), "name" : "bob", "class" : "nsd1803" }

注意:数据id不能重复

Csv格式 不指定字段时报错

导出:

[root@host50 ~]# mongoexport --host 192.168.4.50 --port 27050 -d studb -c c1 -f _id,name  --type=csv > /mongodbdir/c1.csv

2018-07-07T16:34:12.634+0800 connected to: 192.168.4.50:27050

2018-07-07T16:34:12.636+0800 exported 2 records

导入:

[root@host50 ~]# mongoimport --host 192.168.4.50 --port 27050 -d bbsdb -c t2 -f _id,name --type=csv /mongodbdir/c1.csv   //t2集合没有时自动创建 

2018-07-07T16:48:01.689+0800 connected to: 192.168.4.50:27050

2018-07-07T16:48:01.906+0800 imported 3 documents

> db

bbsdb

> show tables

t1

t2

> db.t2.find()

{ "_id" : "_id", "name" : "name" }//需要去掉的字段

{ "_id" : "ObjectId(5b403a22d701a74e97d8457f)", "name" : "bob" }

{ "_id" : "ObjectId(5b403a4ad701a74e97d84580)", "name" : "bob" }

[root@host50 ~]# mongoimport --host 192.168.4.50 --port 27050 -d studb -c c1 --headerline --drop  --type=csv /mongodbdir/c1.csv 

2018-07-07T17:00:56.750+0800 connected to: 192.168.4.50:27050

2018-07-07T17:00:56.751+0800 dropping: studb.c1

2018-07-07T17:00:56.963+0800 imported 2 documents

> db.c1.find()

{ "_id" : "ObjectId(5b403a22d701a74e97d8457f)", "name" : "bob" }

{ "_id" : "ObjectId(5b403a4ad701a74e97d84580)", "name" : "bob" }

> db.c3.save({

... name:"yaya",

... password:"x",

... uid:8888,

... gid:999,

... comment:"teacher",

... homedir:"/home/yaya",

... shell:"/bin/bash"

... })

WriteResult({ "nInserted" : 1 })

[root@host50 ~]# mongoexport --host 192.168.4.50 --port 27050 -d studb -c c3 -f  name,password,uid,gid,comment,homedir,shell  --type=csv 

2018-07-07T17:28:52.032+0800 connected to: 192.168.4.50:27050

name,password,uid,gid,comment,homedir,shell

yaya,x,8888,999,teacher,/home/yaya,/bin/bash

2018-07-07T17:28:52.033+0800 exported 1 record

[root@host50 ~]# mongoexport --host 192.168.4.50 --port 27050 -d studb -c c3 -f  name,password,uid,gid,comment,homedir,shell  --type=csv > /mongodbdir/c3.csv

2018-07-07T17:29:50.617+0800 connected to: 192.168.4.50:27050

2018-07-07T17:29:50.618+0800 exported 1 record

[root@host50 ~]# 

[root@host50 mongodbdir]# cp /etc/passwd ./

[root@host50 mongodbdir]# ls

c1.csv  c1.json  c3.csv  passwd

[root@host50 mongodbdir]# sed -i '$r passwd' c3.csv 

[root@host50 mongodbdir]# sed -i 's/:/,/g' c3.csv

[root@host50 mongodbdir]# mongoimport --host 192.168.4.50 --port 27050 -d studb -c c3 --headerline --drop  --type=csv /mongodbdir/c3.csv 

2018-07-07T17:45:21.625+0800 connected to: 192.168.4.50:27050

2018-07-07T17:45:21.625+0800 dropping: studb.c3

2018-07-07T17:45:21.836+0800 imported 44 documents

> db.c3.count() //默认20

44

Type "it" for more

> it

数据备份恢复

> use studb

switched to db studb

> db.c3.remove({})

WriteResult({ "nRemoved" : 44 })

[root@host50 mongodbdir]# mongodump --host 192.168.4.50 --port 27050 -d studb -c c3 -o /bakmongo

2018-07-07T18:01:00.640+0800 writing studb.c3 to 

2018-07-07T18:01:00.642+0800 done dumping studb.c3 (0 documents)

[root@host50 mongodbdir]# mongorestore  --host 192.168.4.50 --port 27050 -d studb -c c3  /bakmongo/studb/c3.bson 

> db.c3.remove({})

WriteResult({ "nRemoved" : 44 })

> db.c3.count()

0

> db.c3.count()

44


猜你喜欢

转载自blog.csdn.net/weixin_40018205/article/details/80951394