Ubuntu 12.04上安装 MongoDB并运行

栏目: 数据库 · 发布时间: 5年前

内容简介:作者:凯鲁嘎吉 - 博客园在Terminal输入结果显示如下:

Ubuntu 12.04上安装 MongoDB 并运行

作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/

在Terminal输入

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
sudo apt-get install mongodb-10gen

结果显示如下:

wrr@ubuntu:~$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
[sudo] password for wrr: 
Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /tmp/tmp.vcN87sDcVF --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
gpg: requesting key 7F0CEB10 from hkp server keyserver.ubuntu.com
gpg: key 7F0CEB10: public key "Totally Legit Signing Key <mallory@example.org>" imported
gpg: key 7F0CEB10: public key "Richard Kreuter <richard@10gen.com>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 2
gpg:               imported: 2  (RSA: 2)
wrr@ubuntu:~$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
wrr@ubuntu:~$ sudo apt-get update
wrr@ubuntu:~$ sudo apt-get install mongodb-10gen

下载完成后,查看版本号

wrr@ubuntu:~$ mongo -version
MongoDB shell version: 2.4.14

如果不行,执行下面操作

wrr@ubuntu:~$ sudo rm /var/cache/apt/archives/lock
wrr@ubuntu:~$ sudo rm /var/lib/dpkg/lock
wrr@ubuntu:~$ sudo apt-get update

然后在查看版本号即可。以下命令为启动与关闭MongoDB。

sudo service mongodb start
sudo service mongodb stop

以下为启动MongoDB并进行简单操作。

wrr@ubuntu:~$ sudo service mongodb start
mongodb start/running, process 4004
wrr@ubuntu:~$ pgrep mongo -l
4004 mongod
wrr@ubuntu:~$ mongo
MongoDB shell version: 2.4.14
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 has startup warnings: 
Wed Dec 19 13:11:44.276 [initandlisten] 
Wed Dec 19 13:11:44.276 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Wed Dec 19 13:11:44.276 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Wed Dec 19 13:11:44.276 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Wed Dec 19 13:11:44.276 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Wed Dec 19 13:11:44.276 [initandlisten] 
> show dbs
local	0.03125GB

创建数据库School以及集合teacher与student

wrr@ubuntu:~$ sudo service mongodb start
start: Job is already running: mongodb
wrr@ubuntu:~$ mongo
MongoDB shell version: 2.4.14
connecting to: test
Server has startup warnings: 
Wed Dec 19 13:11:44.276 [initandlisten] 
Wed Dec 19 13:11:44.276 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Wed Dec 19 13:11:44.276 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Wed Dec 19 13:11:44.276 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Wed Dec 19 13:11:44.276 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Wed Dec 19 13:11:44.276 [initandlisten] 
> show dbs
School	0.0625GB
local	0.03125GB
test	(empty)
> use School
switched to db School
> db.createCollection('teacher')
{ "ok" : 1 }
> db.createCollection('student')
{ "ok" : 1 }
> show collections
student
system.indexes
teacher

插入数据

> db.student.insert({_id:2018001, sname:'zhangsan', sage:20})
> db.student.save({_id:2018002, sname:'lisi', sage:22})
> db.student.find()
{ "_id" : 2018001, "sname" : "zhangsan", "sage" : 20 }
{ "_id" : 2018002, "sname" : "lisi", "sage" : 22 }

查找数据

> db.student.find({sname:'lisi'})
{ "_id" : 2018002, "sname" : "lisi", "sage" : 22 }
> db.student.find({},{sname:1, sage:1})
{ "_id" : 2018001, "sname" : "zhangsan", "sage" : 20 }
{ "_id" : 2018002, "sname" : "lisi", "sage" : 22 }
> db.student.find({sname:'zhangsan', sage:22})
> db.student.find({$or: [{sage: 22},{sage:25}]})
{ "_id" : 2018002, "sname" : "lisi", "sage" : 22 }

修改数据

将李四的年龄修改为30

> db.student.find().pretty()
{ "_id" : 2018001, "sname" : "zhangsan", "sage" : 20 }
{ "_id" : 2018002, "sname" : "lisi", "sage" : 22 }
> db.student.update({sname:'lisi'},{$set:{sage:30}},false,true)
> db.student.find({sname:'lisi'})
{ "_id" : 2018002, "sname" : "lisi", "sage" : 30 }

删除数据

删除一条学生记录

> db.student.remove({sname:'zhangsan'})
> db.student.find()
{ "_id" : 2018002, "sname" : "lisi", "sage" : 30 }

删除学生数据集

> db.student.drop()
true
> show collections
system.indexes
teacher

退出

exit

如果想看更详细的解读,请看 Ubuntu下MongoDB安装与使用教程_厦大数据库实验室博客


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

测试驱动开发的艺术

测试驱动开发的艺术

Lasse Koskela / 李贝 / 人民邮电出版社 / 20101023 / 59.00元

在传统的软件开发中,开发人员对于代码是否正确心中无底,一切依赖于后期的测试环节。极限编程反其道而行之,主张采用测试驱动开发(TDD)的方法,即通过测试定义所要开发的功能的接口,然后实现功能的开发过程。TDD通过不断地测试推动代码的开发,既简化了代码,又保证了软件质量。 本书采用“手把手”的教学方式,通过大量实例来解释TDD,还专门用几章的篇幅来讲解如何为难于测试的技术编写单元测试。全书内容循......一起来看看 《测试驱动开发的艺术》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具