创建Mongodb数据库
由于Mongodb不是关系型数据库文件,实际上,它并不存在传统关系型数据库中的所谓“数据库”的概念,但不用担心,当你第一次新增数据时,mongodb就会以collection集合的形式进行保存和新建,而不需要你手工去新建立。下面是例子:
1)列出当前的数据库
MongoDB shell version: 1.8.1
connecting to: test
> show dbs
admin 0.03125GB
local (empty)
connecting to: test
> show dbs
admin 0.03125GB
local (empty)
可以使用show dbs 来列出当前有多少个数据库,上面看到的是有两个,分别是admin和local。
2) 定义新的数据库名
我们通过使用“use new-databasename”的语法去使用一个新的数据库,注意,即使你的数据库还没建立起来,依然可以这样使用,因为mongodb会在真正插入了数据后,才会真正建立起来。
> use mkyongdb
switched to db mkyongdb
> show dbs
admin 0.03125GB
local (empty)
switched to db mkyongdb
> show dbs
admin 0.03125GB
local (empty)
注意,在use mkyongdb后,mkyongdb实际上还没真正建立起来,只是表明目前是在使用mkyongdb了。
3)保存数据
定义一个collection,名为“users”,然后插入数据,如下:
> db.users.save( {username:"mkyong"} )
> db.users.find()
{ "_id" : ObjectId("4dbac7bfea37068bd0987573"), "username" : "mkyong" }
>
> show dbs
admin 0.03125GB
local (empty)
mkyongdb 0.03125GB
> db.users.find()
{ "_id" : ObjectId("4dbac7bfea37068bd0987573"), "username" : "mkyong" }
>
> show dbs
admin 0.03125GB
local (empty)
mkyongdb 0.03125GB
可以看到,用db.users.find()可以找出已插入的数据。这个时候,名为“users”的collection已经建立起来了,同时,数据库mkyongdb也建立起来了。
修复mongodb数据库
当mongodb发生问题时,就要修复其数据库,比如遇到以下的信息:
**************
old lock file: \data\db\mongod.lock. probably means unclean shutdown
recommend removing file and running --repair
see: http://dochub.mongodb.org/core/repair for more information
*************
Mon May 09 12:37:43 [initandlisten] exception in initAndListen std::exception: old lock file,
terminating
Mon May 09 12:37:43 dbexit:
Mon May 09 12:37:43 [initandlisten] shutdown: going to close listening sockets...
Mon May 09 12:37:43 [initandlisten] shutdown: going to flush diaglog...
Mon May 09 12:37:43 [initandlisten] shutdown: going to close sockets...
Mon May 09 12:37:43 [initandlisten] shutdown: waiting for fs preallocator...
Mon May 09 12:37:43 [initandlisten] shutdown: closing all files...
Mon May 09 12:37:43 closeAllFiles() finished
Mon May 09 12:37:43 dbexit: really exiting now
old lock file: \data\db\mongod.lock. probably means unclean shutdown
recommend removing file and running --repair
see: http://dochub.mongodb.org/core/repair for more information
*************
Mon May 09 12:37:43 [initandlisten] exception in initAndListen std::exception: old lock file,
terminating
Mon May 09 12:37:43 dbexit:
Mon May 09 12:37:43 [initandlisten] shutdown: going to close listening sockets...
Mon May 09 12:37:43 [initandlisten] shutdown: going to flush diaglog...
Mon May 09 12:37:43 [initandlisten] shutdown: going to close sockets...
Mon May 09 12:37:43 [initandlisten] shutdown: waiting for fs preallocator...
Mon May 09 12:37:43 [initandlisten] shutdown: closing all files...
Mon May 09 12:37:43 closeAllFiles() finished
Mon May 09 12:37:43 dbexit: really exiting now
那么这个时候,可以采用如下方法去修复它:
找到\data\db\mongod.lock这个文件,删除它
采用mongod –repair命令进行修复
Mon May 09 12:42:57 [initandlisten] db version v1.8.1, pdfile version 4.5
//......
Mon May 09 12:42:57 [initandlisten] shutdown: going to close listening sockets...
Mon May 09 12:42:57 [initandlisten] shutdown: going to flush diaglog...
Mon May 09 12:42:57 [initandlisten] shutdown: going to close sockets...
Mon May 09 12:42:57 [initandlisten] shutdown: waiting for fs preallocator...
Mon May 09 12:42:57 [initandlisten] shutdown: closing all files...
Mon May 09 12:42:57 closeAllFiles() finished
Mon May 09 12:42:57 [initandlisten] shutdown: removing fs lock...
Mon May 09 12:42:57 dbexit: really exiting now
采用mongod –repair命令进行修复
Mon May 09 12:42:57 [initandlisten] db version v1.8.1, pdfile version 4.5
//......
Mon May 09 12:42:57 [initandlisten] shutdown: going to close listening sockets...
Mon May 09 12:42:57 [initandlisten] shutdown: going to flush diaglog...
Mon May 09 12:42:57 [initandlisten] shutdown: going to close sockets...
Mon May 09 12:42:57 [initandlisten] shutdown: waiting for fs preallocator...
Mon May 09 12:42:57 [initandlisten] shutdown: closing all files...
Mon May 09 12:42:57 closeAllFiles() finished
Mon May 09 12:42:57 [initandlisten] shutdown: removing fs lock...
Mon May 09 12:42:57 dbexit: really exiting now
小结
在本文中,简单为大家介绍了Mongodb在widnows下和Linux下的安装和一些基本操作,在下一篇教程中,将学习如何使用Java对Mongodb进行操作,敬请期待。