Ubuntu 12.04上安装 MongoDB并运行
程序员文章站
2022-06-25 08:15:21
Ubuntu 12.04上安装 MongoDB并运行 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 在Terminal输入 结果显示如下: 下载完成后,查看版本号 如果不行,执行下面操作 然后在查看版本号即可。以下命令为启动与关闭MongoDB。 以 ......
ubuntu 12.04上安装 mongodb并运行
作者:凯鲁嘎吉 - 博客园
在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安装与使用教程_厦大数据库实验室博客
上一篇: 1.1 数据库系统概述
下一篇: Vue.js基础解析
推荐阅读
-
Ubuntu系统安装并运行sublime
-
Ubuntu Server 16.04 LTS上怎样安装下载安装Nginx并启动
-
如何在两台Ubuntu linux上安装mysql server并配置主从复制?
-
在 Ubuntu 12.04 Server 上安装部署 Ruby on Rails 应用
-
在Ubuntu 18上安装和运行Hadoop和Spark
-
Python教程(在电脑上安装并运行Python)
-
Ubuntu 12.04上安装 MongoDB并运行
-
Ubuntu 12.04上安装HBase并运行
-
ubuntu16.04上安装CUDA 8.0,报如下错误:The driver installation is unable to locate the kernel source并附解决方案(亲测)
-
Ubuntu 12.04上安装MySQL并运行