运维开发网

ubuntu 12.04上安装 MongoDB并运行

运维开发网 https://www.qedev.com 2020-05-09 15:58 出处:网络 作者:运维开发网整理
ubuntu 12.04上安装 MongoDB并运行 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/

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

结果显示如下:

[email protected]:~$ 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 <[email protected]>" imported
gpg: key 7F0CEB10: public key "Richard Kreuter <[email protected]>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 2
gpg:               imported: 2  (RSA: 2)
[email protected]:~$ 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
[email protected]:~$ sudo apt-get update
[email protected]:~$ sudo apt-get install mongodb-10gen

下载完成后,查看版本号

[email protected]:~$ mongo -version
MongoDB shell version: 2.4.14

如果不行,执行下面操作

[email protected]:~$ sudo rm /var/cache/apt/archives/lock
[email protected]:~$ sudo rm /var/lib/dpkg/lock
[email protected]:~$ sudo apt-get update

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

sudo service mongodb start
sudo service mongodb stop

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

[email protected]:~$ sudo service mongodb start
mongodb start/running, process 4004
[email protected]:~$ pgrep mongo -l
4004 mongod
[email protected]:~$ 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

[email protected]:~$ sudo service mongodb start
start: Job is already running: mongodb
[email protected]:~$ 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 }

删除数据

0

精彩评论

暂无评论...
验证码 换一张
取 消