1、下载和安装
1.1版本匹配
Linux下运行 uname -a 查看Linux系统的版本
[root@VM_0_7_centos bin]# uname -a
Linux VM_0_7_centos 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
1.2下载与其匹配的mongo包
下载地址:https://www.mongodb.org/downloads
安装包: mongodb-linux-x86_64-rhel62-3.4.3.tgz
1.3解压
[root@master software]# tar -zxvf mongodb-linux-x86_64-rhel62-3.4.3.tgz -C /usr/app/
mongodb-linux-x86_64-rhel62-3.4.3/README
mongodb-linux-x86_64-rhel62-3.4.3/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel62-3.4.3/MPL-2
mongodb-linux-x86_64-rhel62-3.4.3/GNU-AGPL-3.0
mongodb-linux-x86_64-rhel62-3.4.3/bin/mongodump
mongodb-linux-x86_64-rhel62-3.4.3/bin/mongorestore
。。。。
1.4创建一个mongo,把解压的包放进去
[root@master app]# mkdir mongo
[root@master app]# mv mongorhel62.3.4/ mongo
[root@master app]# ls
activemq5.15 jdk1.8.0_161 mongo tomcat7.0 zookeeper.out
hbase0.98.12 kafka2.10 storm1.2.1 zookeeper-3.4.11
[root@master app]# cd mongo/
1.5在mongo目录下创建一个数据目录mongodbdata, 一个日志文件mongo.logs,一个配置文件mongo.conf。结构如下
[root@master mongo]# ll
total 12
-rwxr-xr-x. 1 root root 2091 Mar 28 07:31 mongo.conf // touch mongo.conf
drwxr-xr-x. 2 root root 4096 Mar 28 07:25 mongodbdata // mkdir mongodbdata
-rwxr-xr-x. 1 root root 0 Mar 28 07:25 mongo.logs
drwxr-xr-x. 5 root root 4096 Mar 28 07:42 mongorhel62.3.4
1.6修改配置文件 vim mongo.conf 复制粘贴进去就行,修改自己的日志文件位置和数据目录位置
#----------------------------------------------------------------------
#启用日志文件,默认启用
journal=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=false
# 日志文件位置
logpath=/usr/app/mongo/mongo.logs
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork = true
# 默认27017
port = 27017
# 数据库文件位置
dbpath=/usr/app/mongo/mongodbdata
# 启用定期记录CPU利用率和 I/O 等待
#cpu = true
# 是否以安全认证方式运行,默认是不认证的非安全方式
#auth = true
#noauth = true
# 详细记录输出
#verbose = true
# Inspect all client data for validity on receipt (useful for developing drivers)用于开发驱动程序时验证客户端请求
#objcheck = true
# Enable db quota management
# 启用数据库配额管理
#quota = true
# 设置oplog记录等级
# Set oplogging level where n is
# 0=off (default)
# 1=W
# 2=R
# 3=both
# 7=W+some reads
#diaglog=0
# Diagnostic/debugging option 动态调试项
#nocursors = true
# Ignore query hints 忽略查询提示
#nohints = true
# 禁用http界面,默认为localhost:28017
#nohttpinterface = true
# 关闭服务器端脚本,这将极大的限制功能
# Turns off server-side scripting. This will result in greatly limited functionality
#noscripting = true
# 关闭扫描表,任何查询将会是扫描失败 Turns off table scans. Any query that would do a table scan fails.
#notablescan = true
# 关闭数据文件预分配
# Disable data file preallocation.
#noprealloc = true
# 为新数据库指定.ns文件的大小,单位:MB
# Specify .ns file size for new databases.
# nssize =
# Replication Options 复制选项
# in replicated mongo databases, specify the replica set name here
#replSet=setname
# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#指定存储身份验证信息的密钥文件的路径
#keyFile=/path/to/keyfile
#---------------------------------------------------------------------------
2、启动 cd 到mongo插件的bin目录下先,用我们写好的配置文件启动 显示端口,和成功提示就表示成功了
[root@VM_0_7_centos bin]# ./mongod --config/usr/app/mongo/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 10445
child process started successfully, parent exiting
2.2进入操作的shell页面
[root@VM_0_7_centos bin]# ./mongo
MongoDB shell version v3.4.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings: 下面是一堆提示信息
2.3进行相应的操作
2018-03-28T14:56:29.581+0800 I CONTROL [initandlisten]
> use test; 用test用户登录
switched to db test
> db --help
2018-03-28T14:57:50.993+0800 E QUERY [thread1] SyntaxError: missing ; before statement @(shell):1:5
> show dbs 显示目前所有的库
admin 0.000GB
local 0.000GB
> use person 切换到person库下,如果没有就创建
switched to db person
> db.createCollection('steve') 用person库创建一个steve的集合
{ "ok" : 1 }
> 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
> q
2018-03-28T15:00:39.865+0800 E QUERY [thread1] ReferenceError: q is not defined :
@(shell):1:1
> quit() 退出shell脚步
[root@VM_0_7_centos bin]#
B、1、基本增删改查的操作
1.1: show dbs 查看当前的数据库
1.2 use databaseName 选库
1.2 show tables/collections 查看当前库下的collection
1.3 如何创建库?
Mongodb的库是隐式创建,你可以use一个不存在的库
然后在该库下创建collection,即可创建库
1.4 db.createCollection(‘collectionName’)
创建collection
1.5 collection允许隐式创建
Db.collectionName.insert(document);
1.6 db.collectionName.drop() ,
删除collection
1.7 db.dropDatabase();
删除database
------------------- 待补充 ------------------------------