您的位置:首页 > 数据库 > Mongodb

mongodb学习1-安装、部署、shell

2015-02-05 15:44 288 查看
1.下载mongodb

 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.7.tgz?_ga=1.61439151.2035670845.1415171196
解压缩mongodb-linux-x86_64-2.6.7.tgz

tar -zxvf mongodb-linux-x86_64-2.6.7.tgz mongodb

[root@h1 home]# cd mongodb

[root@h1 home]# cd bin

启动 mogodb服务

[root@h1 bin]# ./mongod

2015-02-05T14:50:44.298+0800 [initandlisten] exception in initAndListen: 10296 

*********************************************************************

 ERROR: dbpath (/data/db) does not exist.

 Create this directory or give existing directory in --dbpath.

 See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************

, terminating

2015-02-05T14:50:44.298+0800 [initandlisten] dbexit: 

首次启动会出现 默认dbpath不存在

需要新建dbpath

[root@h1 bin]# mkdir ../data

重新启动,同时指定dbpath目录

[root@h1 bin]# ./mongod --dbpath=/home/mongodb/data --fork --logpath=/home/mongodb/mongodb.log --journal --profile=1 --slowms=2

 启动命令常用选项说明:

    --dbpath 指定数据库的目录。

    --port 指定数据库端口,模式是27017。

    --bind_ip 绑定IP。

    --derectoryperdb为每个db创建一个独立子目录。

    --logpath 指定日志存放目录。

    --logappend 指定日志生成方式(追加/覆盖)。

    --pidfilepath 指定进程文件路径,如果不指定,将不产生进程文件。

    --keyFile 集群模式的关键标识

    --journal 启用日志

    --nssize 指定.ns文件的大小,单位MB,默认是16M,最大2GB。

    --maxConns 最大的并发连接数。

    --notablescan 不允许进行表扫描

    --noprealloc 关闭数据文件的预分配功能

    --fork 以后台Daemon形式运行服务

     --shutdown kill a running server kill掉正在运行的服务./mongod --shutdown --dbpath=/home/mongodb/data/

    更多的选项利用 mongod --help 进行查看

        新建etc/mongodb.cnf  key/value

dbpath=/home/mongodb/data
port=27017  
fork=true
logpath=/home/mongodb/mongodb.log
journal=true
profile=1
slowms=2

    识别mongodb唯一包括prot和dbpath两个,可以通过区分不同dbpath和prot启动不同的实例

    root      3088  3.3  6.1 1580296 63224 ?       Sl   18:16   0:00 ./mongod -f ../etc/mongodb.cnf

    root      3101 26.7  3.2 467208 33632 ?        Sl   18:16   0:04 ./mongod -f ../etc/mongodb1.cnf

    [root@h1 bin]# netstat -natp| grep mongod

    tcp        0      0 0.0.0.0:37017               0.0.0.0:*                   LISTEN      3101/./mongod       

    tcp        0      0 0.0.0.0:27017               0.0.0.0:*                   LISTEN      3088/./mongod  

    可以看到两个实例

    [root@h1 bin]# ./mongod -f ../etc/mongodb.cnf 通过配置文件启动

    关闭数据库的方式

    ./mongod --shutdown --dbpath=/home/mongodb/data/

    ./mongod -f ../etc/mongodb1.cnf

   ps aux | grep mongodb

    root      3128  0.5  6.4 1580280 65528 ?       Sl   18:19   0:00 ./mongod -f ../etc/mongodb.cnf

    root      3141  0.5  3.2 466184 32764 ?        Sl   18:20   0:00 ./mongod -f ../etc/mongodb1.cnf

    kill -2 3128 不能kill -9 因为在生产环境中mongodb一直在运行中,mongodb内存中有脏数据没有写入,需要刷新中到数据库中

    kill -2 是安全的 ,kill -9 重新启动可能导致mongodb无法启动而挂掉

    > db.shutdownServer()
shutdown command only works with the admin database; try 'use admin'
> use admin
switched to db admin
> db.shutdownServer()
2015-02-05T18:26:48.621+0800 DBClientCursor::init call() failed
server should be down...

       [root@h1 bin]# ps -aux | grep mongodb
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root      3141  0.5  3.2 466184 32772 ?        Sl   18:20   0:02 ./mongod -f ../etc/mongodb1.cnf
root      3176  0.0  0.0 103252   824 pts/0    S+   18:27   0:00 grep mongodb

       可以发现已经被关闭

mongodb shell操作数据库:

1.  超级用户相关: 

         1. #进入数据库admin 

          use admin 

         2. #增加或修改用户密码 

          db.addUser('name','pwd') 

         3. #查看用户列表 

          db.system.users.find() 

         4. #用户认证 

          db.auth('name','pwd') 

         5. #删除用户 

          db.removeUser('name') 

         6. #查看所有用户 

          show users 

         7. #查看所有数据库 

          show dbs /databases

> show dbs

admin  0.078GB

hxf    0.453GB

local  0.078GB

> show databases

admin  0.078GB

hxf    0.453GB

local  0.078GB

         8. #查看所有的collection/tables

          show collections /show tables

> use hxf
switched to db hxf

> db.users.count()
999999
> db.users.insert({"userid":1,"username":"Michael"})
WriteResult({ "nInserted" : 1 })
> show databases
admin  0.078GB
hxf    0.453GB
local  0.078GB
> show dbs
admin  0.078GB
hxf    0.453GB
local  0.078GB
> use hxf
switched to db hxf
> show collections
system.indexes
system.profile
users
> db.users.count()
1000000

> show tables

startup_log

system.indexes

system.profile

> show collections

startup_log

system.indexes

system.profile

> db.users2.drop()
true
> show collections
system.indexes
system.profile
users
> db.users2.drop()
false

> db.users.findOne()
{
"_id" : ObjectId("54d31d69119eb82dd53c56ef"),
"userid" : 1,
"username" : "mengdeyu1"
}
> db.users.remove({})
WriteResult({ "nRemoved" : 1000000 })
> db.users.find()


         9. #查看各collection的状态 

          db.printCollectionStats() 

        10. #查看主从复制状态 

          db.printReplicationInfo() 

        11. #修复数据库 

          db.repairDatabase() 

        12. #设置记录profiling,0=off 1=slow 2=all 

          db.setProfilingLevel(1) 

        13. #查看profiling 

          show profile 

        14. #拷贝数据库 

          db.copyDatabase('mail_addr','mail_addr_tmp') 

        15. #删除collection 

          db.mail_addr.drop() 

        16. #删除当前的数据库 

          db.dropDatabase() 

2. 增删改 (其中下面的foo与user_addr为collection)

         1. #存储嵌套的对象 

            db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]}) 

         2. #存储数组对象 

            db.user_addr.save({'Uid':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']}) 

         3. #根据query条件修改,如果不存在则插入,允许修改多条记录 

            db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true) 

         4. #删除yy=5的记录 

            db.foo.remove({'yy':5}) 

         5. #删除所有的记录 

            db.foo.remove() 

   3. 索引 (其中下面的foo与user_addr为collection)

         1. #增加索引:1(ascending),-1(descending) 

         2. db.foo.ensureIndex({firstname: 1, lastname: 1}, {unique: true}); 

         3. #索引子对象 

         4. db.user_addr.ensureIndex({'Al.Em': 1}) 

         5. #查看索引信息 

         6. db.foo.getIndexes() 

         7. db.foo.getIndexKeys() 

         8. #根据索引名删除索引 

         9. db.user_addr.dropIndex('Al.Em_1') 

4. 查询 (其中下面的foo与user_addr为collection)

         1. #查找所有 

        2. db.foo.find() 

        3. #查找一条记录 

        4. db.foo.findOne() 

        5. #根据条件检索10条记录 

        6. db.foo.find({'msg':'Hello 1'}).limit(10) 

        7. #sort排序 

        8. db.deliver_status.find({'From':'deyu1982@gmail.com'}).sort({'Dt',-1}) 

         9. db.deliver_status.find().sort({'Ct':-1}).limit(1) 

        10. #count操作 

        11. db.user_addr.count() 

        12. #distinct操作,查询指定列,去重复 

        13. db.foo.distinct('msg') 

        14. #”>=”操作 

        15. db.foo.find({"timestamp": {"$gte" : 2}}) 

        16. #子对象的查找 

        17. db.foo.find({'address.city':'beijing'}) 

   5. 管理  (其中下面的deliver_status为collection)

         1. #查看collection数据的大小 

         2. db.deliver_status.dataSize() 

         3. #查看colleciont状态 

         4. db.deliver_status.stats() 

         5. #查询所有索引的大小 

         6. db.deliver_status.totalIndexSize() 

客户端启动

[root@h1 bin]# ./mongo

MongoDB shell version: 2.6.7

connecting to: test

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端窗口显示启动一个连接

2015-02-05T15:00:53.653+0800 [clientcursormon] mem (MB) res:44 virt:456

2015-02-05T15:00:53.653+0800 [clientcursormon]  mapped (incl journal view):160

2015-02-05T15:00:53.653+0800 [clientcursormon]  connections:1

show dbs 列出所有的数据库

admin  (empty)

hxf    0.078GB

local  0.078GB

use hxf  使用hxf数据库

show tables 列出所有的表

> show tables

system.indexes

users

> show collections 列出所有的集合

system.indexes

users

db.users.count() 表记录数

> db.users.count()

999999

查询数据,列出所有的数据

db.users.find()

db.users.findOne()列出查询一条记录

db.dropDatabase() 删除数据库,首先use hxf 然后db.dropDatabase()

./mongod --dbpath=/home/mongodb/data --fork --logpath=/home/mongodb/mongodb.log --journal --profile=1 --slowms=2

mongodb文件存储结构

数据存储结构

ns命名空间文件

数据文件(0,1,2...)

    查看data文件   

  65540 -rw-------. 1 root root  67108864 Feb  5 15:28 hxf.0\
16384 -rw-------. 1 root root  16777216 Feb  5 15:28 hxf.ns

    4 drwxr-xr-x. 2 root root      4096 Feb  5 15:30 journal

  65540 -rw-------. 1 root root  67108864 Feb  5 14:54 local.0

  16384 -rw-------. 1 root root  16777216 Feb  5 14:54 local.ns

      0 -rwxr-xr-x. 1 root root         0 Feb  5 15:30 mongod.lock
65540 -rw-------. 1 root root  67108864 Feb  5 15:24 test.0
131076 -rw-------. 1 root root 134217728 Feb  5 15:19 test.1
262144 -rw-------. 1 root root 268435456 Feb  5 15:24 test.2

  16384 -rw-------. 1 root root  16777216 Feb  5 15:24 test.ns

默认是16M大小

随着数据变多将会加倍增长,第二个是第一个两倍,第三个是第二个两倍

预分配数据空间,避免数据空间增长压力

日志存储结构

mongodb系统日志文件

--logpath=/home/mongodb/mongodb.log

journal 数据库保障,所有的数据库更新都先写入这个里面,然后在写入真正的数据库中

-rw-------. 1 root root 1073741824 Feb  5 16:09 j._0

-rw-------. 1 root root 1073741824 Feb  5 14:52 prealloc.1  预分配文件

-rw-------. 1 root root 1073741824 Feb  5 14:52 prealloc.2  预分配文件

 oplog复制操作日志文件

打开慢查询

--profile=1 --slowms=2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: