Table of Contents
主要介绍了日常运行维护的管理工具
启动和停止MongoDB
使用Daemon方式启动
常见的mongod的参数说明
关闭数据库
访问数据库
启动认证
使用命令行操作
进程管理
查看活动进程
结束进程
监控系统的状态和性能
数据导出与导入 mongoexportmongoinport
数据备份和恢复
使用 数据备份 mongodump
使用数据恢复 mongorestore
Home Database Mysql Tutorial MongoDB的日常维护管理

MongoDB的日常维护管理

Jun 07, 2016 pm 03:57 PM
mongodb main daily manage maintain run

主要介绍了日常运行维护的管理工具 MongoDB的日常维护包括使用配置文件,设置访问控制,Shell交互,系统监控和管理,数据库日常备份和恢复 启动和停止MongoDB 启动后可以通过数据库的IP加端口号访问Web形式数据库。配置文件 通过使用拂去配置文件的方式启动

主要介绍了日常运行维护的管理工具

MongoDB的日常维护包括使用配置文件,设置访问控制,Shell交互,系统监控和管理,数据库日常备份和恢复

启动和停止MongoDB

启动后可以通过数据库的IP加端口号访问Web形式数据库。配置文件
通过使用拂去配置文件的方式启动数据库实例,在bin文件夹下创建并编辑mongodb.config(名字可以随意)
事例加上 dbpath =/data/db/
启动时加上 --f 参数,并且指向配置文件即可。
使用Daemon方式启动
为什么我们使用Daemon方式?当我们关闭数据库服务的session端口的时候,MongoDB的服务也随之终止,这样是十分不安全的。通过守护进程的方式,启动即可。
添加 --fork 参数,这里必须指定存储日志的文件,即为启动 --logpath 参数。
事例如下
./mongod.exe --dbpath = D:\MongoDB  --logpath = D:\MongoDB\log\mongodb50.log   --fork
Copy after login
常见的mongod的参数说明
dbpath:数据文件存放路径
logpath: 存放的日志文件
bind_ip :对外的服务绑定IP,一般为空,面对所有的IP开放
port: fork 以后台Daemon的形式启动该服务,web管理端在其上加1000
journal: 开启日志功能,通过保存操作日志来降低单机故障的恢复时间,
config :当参数行十分多的时候,使用这个参数来设定参数文件的位置
关闭数据库
直接使用Control+C来中断
在connect连接状态下,可以切换到admin数据,后直接在库中发送db.shutdownServer()指令终止MongoDB实例。
Unix下发送Kill -2 PID 或者 Kill -15 PID来终止进程
 ps aux|grep mongod
    kill -2 (yourPID)
    ps aux|grep mongod
Copy after login

注意:不能使用kill -9 PID 杀死进程,这样可能导致MongoDB数据库损坏。

访问数据库

绑定iP地址 ——bind_ip
//MongoDB 可以限制只允许某一特定IP 来访问,只要在启动时加一个参数bind_ip 即可,如下:
[root@mongodb01 /home/mongo/mongodb-2.0.2/bin]$ ./mongod --bind_ip 192.168.1.61
设置监听端口 ——port
//将服务端监听端口修改为27018:
[root@mongodb01 /home/mongo/mongodb-2.0.2/bin]$ ./mongod --bind_ip 192.168.1.61 --port 27018
//(报错代码)端户访问时不指定端口,会连接到默认端口27017,对于本例会报错,代码如下:
[root@mongodb01 /home/mongo/mongodb-2.0.2/bin]$ ./mongo 192.168.1.50
MongoDB shell version: 2.0.2
connecting to: 192.168.1.61/test
Sun Apr 14 21:45:26 Error: couldn't connect to server 192.168.1.50 shell/mongo.js:81 exception: connect failed
使用用户名和密码登陆 ——启动时使用--auth参数
//先启用系统的登录验证模块, 只需在启动时指定 auth 参数即可,代码如下:
[root@mongodb01 /home/mongo/mongodb-2.0.2/bin]$ ./mongod --auth
启动认证

默认有个admin数据库,在admin.system.users中保存的用户比其他的数据库设置的用户权限更大。在未添加admin.system.users用户的权限的的情况下, 客户端无需任何认证就可以连接到数据库,并且可以对数据库进行任何操作,只有在admin.system.users添加了用户,启动--auth参数才会起作用。

1.建立系统root用户

>db.addUser("root","123456")
>db.auth("root","123456")
Copy after login

2.建立只读权限用户

>db.addUser("user_reader","1234567",true)
Copy after login

添加只读权限的用户只需添加第三个参数,true。

使用命令行操作

MongoDB不仅可以交互,还可以执行指定的JavaScript文件,执行指定的命令片段,使用Linux Shell。

1.通过eval参数执行指定的语句

查询test库的t1集合的记录有多少:

db.t1.find()

{ "_id" : ObjectId("4f8ac746b2764d3c3e2cafbb"), "num" : 1 }

{ "_id" : ObjectId("4f8ac74cb2764d3c3e2cafbc"), "num" : 2 }

{ "_id" : ObjectId("4f8ac74eb2764d3c3e2cafbd"), "num" : 3 }

{ "_id" : ObjectId("4f8ac751b2764d3c3e2cafbe"), "num" : 4 }

{ "_id" : ObjectId("4f8ac755b2764d3c3e2cafbf"), "num" : 5 }

db.t1.count()

5

通过使用--eval参数直接执行ti的集合中的数

$./mongo.exe  --eval  "printjson(db.t1.count())"
MongoDB shell version: 2.0.2
connecting to: test
5
Copy after login

2.使用js文件执行文件内容

$cat t1_count.js
var count = db.t1.count();
printjson('count of t1 is: '+count);
Copy after login

显示为:

 $./mongo t1_count.js
MongoDB shell version: 2.0.2
connecting to: test
"count of t1 is: 5"
Copy after login

Tips:通过--quiet参数屏蔽部分登陆信息,使结果更清晰

$ ./mongo --quiet t1_count.js
"count of t1 is: 5"
Copy after login

进程管理

查看活动进程
> db.currentOp()
>db.$cmd.sys.inprog.findOne()   //$cmd调用外部函数
Copy after login

显示如下:

> db.currentOp()
{
        "inprog" : [
                {
                        "opid" : 630385,  
                        "active" : true,
                        "lockType" : "read",
                        "waitingForLock" : false,
                        "secs_running" : 0,
                        "op" : "query",
                        "ns" : "test",
                        "query" : {
                                "count" : "t1",
                                "query" : {

                                },
                                "fields" : {

                                }
                        },
                        "client" : "127.0.0.1:51324",
                        "desc" : "conn",
                        "threadId" : "0x7f066087f710",
                        "connectionId" : 7,
                        "numYields" : 0
                }
        ]
}
>
Copy after login

代码解释:

opid:操作进程号
op: 操作类型(query ,update ,etc)
ns: 命名空间(namespace),操作对象
query :显示操作的具体内容
lockType: 锁的类型,指明是写锁还是读锁
结束进程
> db.killOp(630385)
{ "info" : "attempting to kill op" }
Copy after login

我们查看下:

> db.currentOp()
{ "inprog" : [ ] }
>
Copy after login

监控系统的状态和性能

使用serverStatus命令可以获取到运行中的MongoDB服务器统计信息,下面我们来执行命令,查看MongoDB服务器的统计信息(不同平台或不同版本的键会有所不同),代码如下:

> db.runCommand({"serverStatus":1})
{
        "host" : "lindenpatservermongodb01",
        "version" : "2.0.2",
        "process" : "mongod",
        "uptime" : 6003,
        "uptimeEstimate" : 5926,
        "localTime" : ISODate("2012-04-15T11:02:21.795Z"),
        "globalLock" : {
                "totalTime" : 6002811172,
                "lockTime" : 24867,
                "ratio" : 0.000004142559092311891,
                "currentQueue" : {
                        "total" : 0,
                        "readers" : 0,
                        "writers" : 0
                },
                "activeClients" : {
                        "total" : 0,
                        "readers" : 0,
                        "writers" : 0
                }
        },
        "mem" : {
                "bits" : 64,
                "resident" : 52,
                "virtual" : 1175,
                "supported" : true,
                "mapped" : 160,
                "mappedWithJournal" : 320
        },
        "connections" : {
                "current" : 1,
                "available" : 818
        },
        "extra_info" : {
                "note" : "fields vary by platform",
                "heap_usage_bytes" : 341808,
                "page_faults" : 14
        },
        "indexCounters" : {
                "btree" : {
                        "accesses" : 1,
                        "hits" : 1,
                        "misses" : 0,
                        "resets" : 0,
                        "missRatio" : 0
                }
        },
        "backgroundFlushing" : {
                "flushes" : 100,
                "total_ms" : 13,
                "average_ms" : 0.13,
                "last_ms" : 1,
                "last_finished" : ISODate("2012-04-15T11:02:19.010Z")
        },
        "cursors" : {
                "totalOpen" : 0,
                "clientCursors_size" : 0,
                "timedOut" : 0
        },
        "network" : {
                "bytesIn" : 1729666458,
                "bytesOut" : 1349989344,
                "numRequests" : 21093517
        },
        "opcounters" : {
                "insert" : 5,
                "query" : 8,
                "update" : 0,
                "delete" : 0,
                "getmore" : 0,
                "command" : 21093463
        },
        "asserts" : {
                "regular" : 0,
                "warning" : 0,
                "msg" : 0,
                "user" : 0,
                "rollovers" : 0
        },
        "writeBacksQueued" : false,
        "dur" : {
                "commits" : 30,
                "journaledMB" : 0,
                "writeToDataFilesMB" : 0,
                "compression" : 0,
                "commitsInWriteLock" : 0,
                "earlyCommits" : 0,
                "timeMs" : {
                        "dt" : 3073,
                        "prepLogBuffer" : 0,
                        "writeToJournal" : 0,
                        "writeToDataFiles" : 0,
                        "remapPrivateView" : 0
                }
        },
        "ok" : 1
}
>
Copy after login
数据导出与导入 mongoexportmongoinport

使用mongoexport导出数据

先看数据:

> db.t1.find()
{ "_id" : ObjectId("4f8ac746b2764d3c3e2cafbb"), "num" : 1 }
{ "_id" : ObjectId("4f8ac74cb2764d3c3e2cafbc"), "num" : 2 }
{ "_id" : ObjectId("4f8ac74eb2764d3c3e2cafbd"), "num" : 3 }
{ "_id" : ObjectId("4f8ac751b2764d3c3e2cafbe"), "num" : 4 }
{ "_id" : ObjectId("4f8ac755b2764d3c3e2cafbf"), "num" : 5 }
>

Copy after login
使用代码:
./mongoexport.exe -d test -c t1 -o t1.dat
connected to: 127.0.0.1
exported 5 records
[root@mongodb01 /home/mongo/mongodb-2.0.2/bin]$ ls
bsondump  mongodump    mongoimport   mongosniff  t1_count.js
mongo     mongoexport  mongorestore  mongostat   t1.dat
mongod    mongofiles   mongos        mongotop    testfiles.txt
[root@mongodb01 /home/mongo/mongodb-2.0.2/bin]$ cat t1.dat
{ "_id" : ObjectId("4f8ac746b2764d3c3e2cafbb"), "num" : 1 }
{ "_id" : ObjectId("4f8ac74cb2764d3c3e2cafbc"), "num" : 2 }
{ "_id" : ObjectId("4f8ac74eb2764d3c3e2cafbd"), "num" : 3 }
{ "_id" : ObjectId("4f8ac751b2764d3c3e2cafbe"), "num" : 4 }
{ "_id" : ObjectId("4f8ac755b2764d3c3e2cafbf"), "num" : 5 }
Copy after login

./mongoexport.exe -d test -c t1 -o t1.dat 使用参数说明

-d: 指明使用的数据库
-c: 指明导出的集合,这里是t1
-o: 导出的数据名,这里的数据名默认使用相对路径,也可以使用绝对路径。

导出的数据格式的是JSON方式,也可导出csv格式;

导出为CSV格式代码文件如下:

./mongoexport -d test -c t1 -csv -f num -o t1.dat
Copy after login
-csv:指明了要导出的是CSV格式
-f: 指明需要导出的是哪些例子

数据导入 mongoimport

现将ti删除:

> db.t1.drop()
true
> show collections
system.indexes
system.users
>
Copy after login

再导入数据,这里导入的是csv数据:

./mongoimport -d test -c t1 --type csv --headerline -file /home/t1.dat
connected to: 127.0.0.1
Copy after login

看看导入的数据是不是一样:

> db.t1.find()
{ "_id" : ObjectId("4f8ac746b2764d3c3e2cafbb"), "num" : 1 }
{ "_id" : ObjectId("4f8ac74cb2764d3c3e2cafbc"), "num" : 2 }
{ "_id" : ObjectId("4f8ac74eb2764d3c3e2cafbd"), "num" : 3 }
{ "_id" : ObjectId("4f8ac751b2764d3c3e2cafbe"), "num" : 4 }
{ "_id" : ObjectId("4f8ac755b2764d3c3e2cafbf"), "num" : 5 }
>
Copy after login
--type csv 导入的数据格式为CSV,为什么导入CSV格式:CSV对各大主流的数据库支持更良好,而JSON作为轻量级的数据格式,还有些弊端。
--file 指明导入的文件路径

数据备份和恢复

使用 数据备份 mongodump
./mongodump -d test -o /home/dump
Copy after login
-o:表示输出的备份路径,如果没有使用这个选项的话,MongoDB会自动创建dump文件夹并将备份文件放于其内。
使用数据恢复 mongorestore

mongorestore获取mongodump的输出结果,并将备份的数据插入到运行的MongoDB中。

./mongorestore -d test dump/*
connected to: 127.0.0.1
Thu Apr 19 18:16:12 dump/test/system.users.bson
Thu Apr 19 18:16:12      going into namespace [test.system.users]
2 objects found
Thu Apr 19 18:16:12 dump/test/t1.bson
Thu Apr 19 18:16:12      going into namespace [test.t1]
5 objects found
Thu Apr 19 18:16:12 dump/test/system.indexes.bson
Thu Apr 19 18:16:12      going into namespace [test.system.indexes]
Thu Apr 19 18:16:12 { key: { _id: 1 }, ns: "test.system.users", name: "_id_" }
Thu Apr 19 18:16:13 { key: { _id: 1 }, ns: "test.t1", name: "_id_" }
2 objects found
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add and manage users in Google Manager How to add and manage users in Google Manager Sep 02, 2024 pm 02:41 PM

How to add and manage users in Google Manager? Google Chrome supports multiple users to log in, so we don’t have to worry about logging in across devices. If we have many users, we need to add management. Some friends may not know how to operate. Don't worry, the editor has compiled a detailed step-by-step tutorial for everyone today. If you are interested, come and take a look with the editor. Detailed step-by-step tutorial instructions 1. After turning on the computer, find the installed Google Chrome icon on the desktop and double-click to open it, as shown in the picture below. 2. Click the three dots icon in the upper right corner of Google Chrome, as shown in the picture below. 3. Click the [Settings] option in the drop-down menu of Google Chrome, as shown in the figure below. 4. In the Google Chrome settings interface that opens, click [Manage ch

What is the use of net4.0 What is the use of net4.0 May 10, 2024 am 01:09 AM

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

How to configure MongoDB automatic expansion on Debian How to configure MongoDB automatic expansion on Debian Apr 02, 2025 am 07:36 AM

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

How to ensure high availability of MongoDB on Debian How to ensure high availability of MongoDB on Debian Apr 02, 2025 am 07:21 AM

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

What is the CentOS MongoDB backup strategy? What is the CentOS MongoDB backup strategy? Apr 14, 2025 pm 04:51 PM

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

Major update of Pi Coin: Pi Bank is coming! Major update of Pi Coin: Pi Bank is coming! Mar 03, 2025 pm 06:18 PM

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

See all articles