MongoDB日常运维操作命令集锦

总所周知,MongoDB是一个NoSQL非数据库系统,即一个数据库可以包含多个集合(Collection),每个集合对应于关系数据库中的表;而每个集合中可以存储一组由列标识的记录,列是可以自由定义的,非常灵活,由一组列标识的实体的集合对应于关系数据库表中的行。下面通过熟悉MongoDB的基本管理命令,来了解MongoDB提供的DBMS的基本功能和行为。

0)MongoDB的安装

[root@CentOS6-vm01 ~]# curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz
[root@centos6-vm01 ~]# tar -zxvf mongodb-linux-x86_64-3.0.6.tgz
[root@centos6-vm01 ~]# mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb
[root@centos6-vm01 ~]# vim /etc/profile
......
export PATH=$PATH:/usr/local/mongodb/bin/
[root@centos6-vm01 ~]# source /etc/profile

启动mongodb
[root@centos6-vm01 ~]# mkdir -p /data/db
[root@centos6-vm01 ~]# cd /usr/local/mongodb/bin/
[root@centos6-vm01 bin]# ./mongod &

[root@centos6-vm01 bin]# lsof -i:27017
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 24304 root 5u IPv4 187360 0t0 TCP *:27017 (LISTEN)
mongod 24304 root 12u IPv4 187456 0t0 TCP localhost:27017->localhost:48742 (ESTABLISHED)
mongo 24319 root 3u IPv4 187455 0t0 TCP localhost:48742->localhost:27017 (ESTABLISHED)

连接mongodb报错:
Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly.

解决办法:
[root@centos6-vm01 ~]# vim /etc/profile
......
export LC_ALL=C
[root@centos6-vm01 ~]# source /etc/profile

1)MongoDB命令帮助系统
在安装MongoDB后,启动服务器进程(mongod),可以通过在客户端命令mongo实现对MongoDB的管理和监控。看一下MongoDB的命令帮助系统:

[root@centos6-vm01 ~]# mongo
MongoDB shell version: 3.0.6
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see

Questions? Try the support group

Server has startup warnings:
2018-01-02T23:24:23.304+0000 I CONTROL [initandlisten] WARNING: You are running this process as the root user, which is not recommended.
2018-01-02T23:24:23.304+0000 I CONTROL [initandlisten]
2018-01-02T23:24:23.307+0000 I CONTROL [initandlisten]
2018-01-02T23:24:23.307+0000 I CONTROL [initandlisten]
WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-01-02T23:24:23.307+0000 I CONTROL [initandlisten] We suggest setting it to 'never'
2018-01-02T23:24:23.307+0000 I CONTROL [initandlisten]
2018-01-02T23:24:23.307+0000 I CONTROL [initandlisten]
WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-01-02T23:24:23.307+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-01-02T23:24:23.307+0000 I CONTROL [initandlisten]

help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce

show dbs show database names show collections show collections in current database show users show users in current database show profile show most recent system.profile entries with time >= 1ms show logs show the accessible logger names show log [name] prints out the last segment of log in memory, 'global' is default use <db_name> set current database db.foo.find() list objects in collection foo db.foo.find( { a : 1 } ) list objects in foo where a == 1 it result of the last line evaluated; use to further iterate DBQuery.shellBatchSize = x set default number of items to display on shell exit quit the mongo shell

这是MongoDB最顶层的命令列表,主要告诉我们管理数据库相关的一些抽象的范畴:数据库操作帮助、集合操作帮助、管理帮助。如果你想了解数据库操作更详细的帮助命令,可以直接使用db.help(),如下所示:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/f4892ae8b524347eef5d3289f45192bc.html