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