2019-07-08 NoSQL之 MongoDB

本文档参考 https://www.jianshu.com/p/44499e245b38

Debain 和 CentOS 区别

1.软件包管理工具不一样
2.一些配置文件路径不一样
3.一些默认设置不一样

MongoDB应用场景

1.基于地理位置计算

2.日志存储

3.不同用户不同选项

4.评论信息

5.问卷调查,《易企秀》

MongoDB 应用场景? - 阿里云云栖社区的回答 - 知乎
https://www.zhihu.com/question/32071167/answer/147896283

MongoDB 和 mysql 区别

mongo
json 格式          http://www.bejson.com/
{"FirstName":"ke","LastName":"me","email":"hikeme@aa"}
格式化之后:
{
    "FirstName": "zhang",
    "LastName": "ya",
    "email": "[email protected]",
    "job": "linux"
}

mysql
user表
FirstName  LastName  email               job 
zhang      ya        [email protected]  linux

MongoDB 特点

1.高性能,尤其是支持嵌入式数据模型减少数据库系统上的I/O操作

2.高可用性,MongoDB的复制工具,副本集,提供自动故障转移的数据冗余

3.水平可扩展性

4.支持多种存储引擎:WiredTiger存储引擎

MongoDB 包含的存储程序

Drivers

Atlas 云扩展

看不惯云计算公司流氓行为,MongoDB 更改开源协议
https://www.oschina.net/news/100948/mongodb-switches-up-its-open-source-license

安装部署

安装服务套路:

1.规划目录
2.下载软件包
3.解压到指定目录
4.创建数据目录
5.创建用户
6.更改目录、授权
7.修改配置文件
8.启动
9.测试
10.编写启动关停脚本
yum install libcurl openssl -y

mkdir /opt/mongo_cluster/ -p

#mkdir /data/soft -p

cd /data/soft/

#wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.13.tgz

tar zxvf mongodb-linux-x86_64-3.6.13.tgz -C /opt/mongo_cluster/

cd /opt/mongo_cluster/ && ln -s mongodb-linux-x86_64-3.6.13 mongodb

mkdir /opt/mongo_cluster/mongo_27017/{conf,logs,pid} -p

mkdir /data/mongo_cluster/mongo_27017 -p

创建完后目录规划:

2019-07-08 NoSQL之 MongoDB_第1张图片
image.png
配置启动

1. 修改配置文件:

cat > /opt/mongo_cluster/mongo_27017/conf/monogdb.conf  << EOF
systemLog:
  destination: file   
  logAppend: true  
  path: /opt/mongo_cluster/mongo_27017/logs/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/mongo_cluster/mongo_27017
  directoryPerDB: true
  wiredTiger:
     engineConfig:
        cacheSizeGB: 1
        directoryForIndexes: true
     collectionConfig:
        blockCompressor: zlib
     indexConfig:
        prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/mongo_cluster/mongo_27017/pid/mongod.pid

net:
  port: 27017
  bindIp: 127.0.0.1,10.0.0.51
EOF

2. 启动命令

cd /opt/mongo_cluster/
mongodb/bin/mongod -f mongo_27017/conf/mongodb.conf 

3.检查

ps -ef|grep mongo
netstat -lntup|grep 27017

4.登录

mongodb/bin/mongo db01:27017

5.关闭

mongodb/bin/mongod -f mongo_27017/conf/monogdb.conf --shutdown

6.写入环境变量

echo 'PATH=$PATH:/opt/mongodb/bin' >> /etc/profile

tail -1 /etc/profile
PATH=$PATH:/opt/mongodb/bin

source /etc/profile
优化警告
 ============= 解决大内存页警告 =============

echo never > /sys/kernel/mm/transparent_hugepage/enabled 
echo never > /sys/kernel/mm/transparent_hugepage/defrag

vim /etc/init.d/disable-transparent-hugepages
#!/bin/bash
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case $1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > ${thp_path}/enabled
    echo 'never' > ${thp_path}/defrag

    re='^[0-1]+$'
    if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
    then
      # RHEL 7
      echo 0  > ${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > ${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac

#授权目录
chmod 755 /etc/init.d/disable-transparent-hugepages 

#设置开机自启    
chkconfig --add disable-transparent-hugepages


#普通用户启动
/opt/mongo_cluster/mongodb/bin/mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf --shutdown

#创建mongo用户、密码、授权
useradd mongo
echo '123456'|passwd --stdin mongo
chown -R mongo:mongo /opt/mongo_cluster
chown -R mongo:mongo /data/mongo_cluster

#切换用户登陆
su - mongo
vim .bashrc 
export PATH=/opt/mongo_cluster/mongodb/bin:$PATH
source .bashrc
mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
mongo

 ============= rlimits 警告 =============

vim /etc/profile
ulimit -u 64000
source /etc/profile
重启mongo服务

mongo基本操作
> show databases   #查库
admin   0.000GB
config  0.000GB
local   0.000GB

> show dbs    
admin   0.000GB
config  0.000GB
local   0.000GB

> db
test

> use local   #切换
switched to db local

> db    #查看在哪个库
local

> show tables
startup_log
> 

帮助命令

 > help   
 > db.help()
 > db.stats()
#插入数据
db.test.insert({"name":"zhangya","age":27,"ad":"北京市朝阳区"})
db.test.insert({"name":"zhangya","age":27,"ad":"北京市朝阳区"})
db.test.insert({"name":"yazhang","age":28,"ad":"北京市朝阳区"})
db.test.insert({"name":"xiaozhang","age":28,"ad":"北京市朝阳区"})
db.test.insert({"name":"xiaozhang","age":28,"ad":"北京市朝阳区","sex":"boy"})

#批量插入多条数据
db.inventory.insertMany( [
    { "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
    { "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "A" },
    { "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
    { "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
    { "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }
 ]);

 
#查询所有
db.test.find()
db.test.findOne()
db.inventory.find()
db.inventory.findOne()

#按条件查询
> db.inventory.find({"status":"D"})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82a"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82b"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }

> db.inventory.find({"size.uom":"cm"})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee828"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82b"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82c"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

> db.inventory.find({"size.uom":"cm","qty":{$lt: 50}})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee828"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82c"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

> db.inventory.find({"size.uom":"cm","qty":{$eq: 75}})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82b"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }

#分解
db.inventory.find(
        {
        "size.uom":"cm",
        "qty":
            {
                $lt:50
            }
        }
)

db.inventory.find( {
    $and : [
        { $or : [ { "status" : "D" }, {"qty":{$lt: 50}} },
        { $or : [ { "status" : "A" }, {"qty":{$lt: 50} } ] }
    ]
})
 
#按条件和正则查询
> myCursor = db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
{ "_id" : ObjectId("5d22c043b2adc5091f6ee828"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee829"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82c"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

> myCursor = db.inventory.find( {status: "A",$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee828"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82c"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

#分解
myCursor = db.inventory.find( {
     status: "A",
     $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )

#更新语句
##更新前数据
> db.inventory.find({"item":"paper"})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82a"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "cm" }, "status" : "P", "lastModified" : ISODate("2019-07-08T07:23:31.570Z") }

##更新语句(更新第一条数据)
> db.inventory.updateOne({"item":"paper"},{$set:{"size.h":"10","size.w":"15","status":"D"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

##更新后数据
> db.inventory.find({"item":"paper"})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82a"), "item" : "paper", "qty" : 100, "size" : { "h" : "10", "w" : "15", "uom" : "cm" }, "status" : "D", "lastModified" : ISODate("2019-07-08T07:23:31.570Z") }

#删除数据
##删除前数据
> db.inventory.find( {"status" : "D"})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82a"), "item" : "paper", "qty" : 100, "size" : { "h" : "10", "w" : "15", "uom" : "cm" }, "status" : "D", "lastModified" : ISODate("2019-07-08T07:23:31.570Z") }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82b"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }

##删除语句(删除第一条数据)
> db.inventory.deleteOne(
... {"status" : "D"}
... )
{ "acknowledged" : true, "deletedCount" : 1 }

##删除后数据
> db.inventory.find( {"status" : "D"})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82b"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }

mongo 分析工具,系统自带
[mongo@db01 ~]$ mongotop
2019-07-08T15:39:09.899+0800    connected to: 127.0.0.1

                    ns    total    read    write    2019-07-08T15:39:10+08:00
    admin.system.roles      0ms     0ms      0ms                             
  admin.system.version      0ms     0ms      0ms                             
config.system.sessions      0ms     0ms      0ms                             
     local.startup_log      0ms     0ms      0ms                             
  local.system.replset      0ms     0ms      0ms                             
        test.inventory      0ms     0ms      0ms                             
             test.test      0ms     0ms      0ms 
  
[mongo@db01 ~]$ mongostat
insert query update delete getmore command dirty used flushes vsize   res qrw arw net_in net_out conn                time
    *0    *0     *0     *0       0     1|0  0.0% 0.0%       0  972M 45.0M 0|0 1|0   156b   59.9k    1 Jul  8 15:38:53.268
    *0    *0     *0     *0       0     1|0  0.0% 0.0%       0  972M 45.0M 0|0 1|0   157b   60.5k    1 Jul  8 15:38:54.270
    *0    *0     *0     *0       0     2|0  0.0% 0.0%       0  972M 45.0M 0|0 1|0   158b   60.7k    1 Jul  8 15:38:55.268

用户授权认证
use admin
db.createUser(
    {
        user: "admin",
        pwd: "123456",
        roles:[ { role: "root", db:"admin"}]}
)

修改配置文件
vim /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
security:
  authorization: enabled

重启服务
mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf --shutdown
mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf

登录
[mongo@db01 ~]$ mongo -uadmin -p
MongoDB shell version v3.6.13
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b97926dc-1614-4da8-96c0-67d8e930b195") }
MongoDB server version: 3.6.13
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB

1.使用admin用户登录 
mongo -uadmin -p 
show dbs 

2.切换到test库,并创建一个用户赋予不同库的不同角色
use test
db.createUser(
  {
    user: "myTester",
    pwd: "xyz123",
    roles: [ { role: "readWrite", db: "test" },
             { role: "read", db: "test2" } ]
  }
)

3.在test库下插入测试数据 
db.test.insert({"name":"zhangya","age":27,"ad":"北京市朝阳区"})
db.test.insert({"name":"zhangya","age":27,"ad":"北京市朝阳区"})
db.test.insert({"name":"yazhang","age":28,"ad":"北京市朝阳区"})
db.test.insert({"name":"xiaozhang","age":28,"ad":"北京市朝阳区"})

4.在test2库下插入测试数据
use test2
db.test2.insert({"name":"zhangya","age":27,"ad":"北京市昌平区"})
db.test2.insert({"name":"zhangya","age":27,"ad":"北京市昌平区"})
db.test2.insert({"name":"yazhang","age":28,"ad":"北京市昌平区"})

5.使用myTester用户登录到test验证库
mongo -umyTester -pxyz123 db01:27017/test
show dbs 
db 

6.切换到test库,测试能否读写
use test
db.test.insert({"name":"58NB","age":27,"ad":"北京市朝阳区"})
db.test.find()

7.切换到test2库,测试能否读写
use test2
db.test2.insert({"name":"58NB","age":27,"ad":"北京市昌平区"})
db.test2.find()

副本集配置
 [mongo@db01 ~]$ 下执行

1.创建配置文件目录
mkdir -p /opt/mongo_cluster/mongo_2801{7,8,9}/{conf,logs,pid}


2.修改副本集配置文件
cat >/opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf < config = {
_id : "dba58", 
members : [
{_id : 0, host : "db01:28017"},
{_id : 1, host : "db01:28018"},
{_id : 2, host : "db01:28019"},
] }
rs.initiate(config) 

8.查看副本集状态
dba58:PRIMARY> rs.status()
{
    "set" : "dba58",
    "date" : ISODate("2019-07-08T09:50:16.711Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1562579412, 3),
            "t" : NumberLong(1)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1562579412, 3),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1562579412, 3),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1562579412, 3),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "db01:28017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 1805,
            "optime" : {
                "ts" : Timestamp(1562579412, 3),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2019-07-08T09:50:12Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1562579282, 1),
            "electionDate" : ISODate("2019-07-08T09:48:02Z"),
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "db01:28018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 145,
            "optime" : {
                "ts" : Timestamp(1562579412, 3),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1562579412, 3),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2019-07-08T09:50:12Z"),
            "optimeDurableDate" : ISODate("2019-07-08T09:50:12Z"),
            "lastHeartbeat" : ISODate("2019-07-08T09:50:16.648Z"),
            "lastHeartbeatRecv" : ISODate("2019-07-08T09:50:15.644Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "db01:28017",
            "syncSourceHost" : "db01:28017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "db01:28019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 145,
            "optime" : {
                "ts" : Timestamp(1562579412, 3),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1562579412, 3),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2019-07-08T09:50:12Z"),
            "optimeDurableDate" : ISODate("2019-07-08T09:50:12Z"),
            "lastHeartbeat" : ISODate("2019-07-08T09:50:16.648Z"),
            "lastHeartbeatRecv" : ISODate("2019-07-08T09:50:15.648Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "db01:28017",
            "syncSourceHost" : "db01:28017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1,
    "operationTime" : Timestamp(1562579412, 3),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1562579412, 3),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

权重调整+主库降级
1.查看配置信息
rs.conf()

2.配置权重
config = rs.conf()
config.members[2].priority=100

3.重新导入配置信息
rs.reconfig(config)

4.主库降级,发起重新选举 
rs.stepDown()

5.恢复成默认权重
config = rs.conf()
config.members[2].priority=1
rs.reconfig(config) 
rs.stepDown()

增加节点
1.安装配置启动新节点
mkdir /opt/mongo_cluster/mongo_28010/{conf,logs,pid} -p

mkdir /data/mongo_cluster/mongo_28010

cp /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf

sed -i 's#28017#28010#g' /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf

mongod -f /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf

mongo db01:28010

2.主库执行添加新节点操作
use admin
rs.add("db01:28010")

删除旧节点
1.主库操作,删除旧节点
mongo db01:28017
rs.remove("db01:28010")

2.节点下线
mongod -f /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf --shutdown

添加仲裁节点
1.清空28010节点的数据
rm -rf /data/mongo_cluster/mongo_28010/*

2.重新启动
mongod -f /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf

3.检查
mongo db01:28010

4.主库操作
mongo db01:28017
rs.addArb("db01:28010")

5.查看节点状态
mongo db01:28010

数据备份导出
创建备份目录并授权(注意在不同用户下操作)
[root@db01 ~]# mkdir /backup
[root@db01 ~]# chown -R mongo:mongo /backup/

导出数据
[mongo@db01 ~]$ mongoexport --port 28017 -d test -c inventory -o /backup/log.json
2019-07-08T19:20:43.728+0800    connected to: localhost:28017
2019-07-08T19:20:43.729+0800    exported 5 records

查看导出数据
[mongo@db01 /backup]$ cat log.json 
{"_id":{"$oid":"5d23149b20a88ce703b89dbf"},"item":"journal","qty":25.0,"size":{"h":14.0,"w":21.0,"uom":"cm"},"status":"A"}
{"_id":{"$oid":"5d23149b20a88ce703b89dc0"},"item":"notebook","qty":50.0,"size":{"h":8.5,"w":11.0,"uom":"in"},"status":"A"}
{"_id":{"$oid":"5d23149b20a88ce703b89dc1"},"item":"paper","qty":100.0,"size":{"h":8.5,"w":11.0,"uom":"in"},"status":"D"}
{"_id":{"$oid":"5d23149b20a88ce703b89dc2"},"item":"planner","qty":75.0,"size":{"h":22.85,"w":30.0,"uom":"cm"},"status":"D"}
{"_id":{"$oid":"5d23149b20a88ce703b89dc3"},"item":"postcard","qty":45.0,"size":{"h":10.0,"w":15.25,"uom":"cm"},"status":"A"}

备份恢复
导出
mongoexport --port 28017 -d dba -c inventory -o /backup/log.json
mongoexport --port 28017 -d dba -c inventory --type=csv -f item,qty,size,status  -o /backup/log.csv

导入
mongoimport --port 28017 -d oldboy -c inventory /backup/log.json

导入CSV
mongoimport  --port 28017 -d dba58 -c log2 --type=csv --headerline -f item,qty,size,status  --file  /backup/log.csv

mysql导出csv
select * from world.city into outfile '/tmp/city1.csv' fields terminated by ',';

导入csv
mongoimport --port 28017  -d world  -c city --type=csv -f ID,Name,CountryCode,District,Population --file  /tmp/city1.csv

全备
mongodump  --port 28017 -o /data/backup

只备一个库
mongodump  --port 28017 -d world -o /data/backup

恢复:
mongorestore --port 28017 -d world1  /data/backup/world/

恢复之前先删除
mongorestore --port 28017 -d world1  /data/backup/world/  --drop

今天回顾:

1.sql和nosql关系

2.mongo介绍

3.mongo的应用场景

4.安装部署

5.修改配置文件,启动关闭

6.优化警告

7.插入
- 单条插入
- 多条插入

8.查询
- 查询所有
- 按条件查询
- 按条件和正则查询

9.更新

10.删除
- 按条件删除
- 删除整个库或者表

11.授权
- 创建admin用户
- 创建普通用户授予不同角色和不同权限

12.副本集操作
- 配置文件添加副本集参数
- 启动所有节点
- 配置副本集命令
- 初始化副本集
- 故障转移
- 增加节点
- 删除节点
- 仲裁节点
- 权重调整,主库降级
- 故障案例,4台竞争,只剩一台,老张的忧伤

13.工具使用
mongostat 
mongotop

14.备份恢复
mongoexport/mongoimport:json csv
- 异构平台迁移  mysql  <---> mongodb
mongodump/mongorestore
-日常备份恢复时使用.

15.故障恢复案例
- 全备
- 误操作
- 查询误操作时间点
- 备份oplog
- 恢复到误操作时间点以前的数据
- 检查数据是否恢复完整

报错记录1

主库插入数据
dba58:PRIMARY> 
db.inventory.insertMany( [
    { "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
    { "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "A" },
    { "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
    { "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
    { "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }
 ]);

从库查看数据
dba58:SECONDARY> show tables
2019-07-08T18:03:06.965+0800 E QUERY    [thread1] Error: listCollections failed: {
    "operationTime" : Timestamp(1562580184, 1),
    "ok" : 0,
    "errmsg" : "not master and slaveOk=false",
    "code" : 13435,
    "codeName" : "NotMasterNoSlaveOk",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1562580184, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:941:1
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:953:19
DB.prototype.getCollectionNames@src/mongo/shell/db.js:964:16
shellHelper.show@src/mongo/shell/utils.js:853:9
shellHelper@src/mongo/shell/utils.js:750:15
@(shellhelp2):1:1


临时解决
dba58:SECONDARY> rs.slaveOk()

永久解决
[mongo@db01 ~]$ echo "rs.slaveOk();" >> .mongorc.js
[mongo@db01 ~]$ cat .mongorc.js 
rs.slaveOk();

报错记录2

从库执行命令
db.inventory.deleteOne( {"item" : "journal"})

2019-07-08T18:03:45.247+0800 E QUERY    [thread1] WriteCommandError: not master :
WriteCommandError({
    "operationTime" : Timestamp(1562580215, 1),
    "ok" : 0,
    "errmsg" : "not master",
    "code" : 10107,
    "codeName" : "NotMaster",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1562580215, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
})

问题原因:
从库不能执行增删改命令

报错记录3

2019-07-08T18:58:31.608+0800 I NETWORK  [thread1] trying reconnect to db01:28010 (10.0.0.51) failed
2019-07-08T18:58:31.609+0800 I NETWORK  [thread1] reconnect db01:28010 (10.0.0.51) ok
dba58:OTHER> 
dba58:OTHER> 

问题原因:
不在集群内或者被集群移除了

报错记录4

> db.test.insert({"name":"yazhangya","age":27,"ad":"北京市朝阳区"})
WriteResult({
    "writeError" : {
        "code" : 13,
        "errmsg" : "not authorized on test2 to execute command { insert: \"test\", ordered: true, lsid: { id: UUID(\"01431e12-5d4a-47a9-8ac4-657ab08fda7c\") }, $db: \"test2\" }"
    }
})

问题原因:
当前登录的用户对这个库没有写入权限

你可能感兴趣的:(2019-07-08 NoSQL之 MongoDB)