当前位置:   article > 正文

Ubuntu 12.04上安装 MongoDB并运行

“totally legit signing key ”未改变

Ubuntu 12.04上安装 MongoDB并运行

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

在Terminal输入

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

结果显示如下:

  1. wrr@ubuntu:~$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  2. [sudo] password for wrr:
  3. 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
  4. gpg: requesting key 7F0CEB10 from hkp server keyserver.ubuntu.com
  5. gpg: key 7F0CEB10: public key "Totally Legit Signing Key <mallory@example.org>" imported
  6. gpg: key 7F0CEB10: public key "Richard Kreuter <richard@10gen.com>" imported
  7. gpg: no ultimately trusted keys found
  8. gpg: Total number processed: 2
  9. gpg: imported: 2 (RSA: 2)
  10. wrr@ubuntu:~$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
  11. deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
  12. wrr@ubuntu:~$ sudo apt-get update
  13. wrr@ubuntu:~$ sudo apt-get install mongodb-10gen

下载完成后,查看版本号

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

如果不行,执行下面操作

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

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

  1. sudo service mongodb start
  2. sudo service mongodb stop

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

  1. wrr@ubuntu:~$ sudo service mongodb start
  2. mongodb start/running, process 4004
  3. wrr@ubuntu:~$ pgrep mongo -l
  4. 4004 mongod
  5. wrr@ubuntu:~$ mongo
  6. MongoDB shell version: 2.4.14
  7. connecting to: test
  8. Welcome to the MongoDB shell.
  9. For interactive help, type "help".
  10. For more comprehensive documentation, see
  11. http://docs.mongodb.org/
  12. Questions? Try the support group
  13. http://groups.google.com/group/mongodb-user
  14. Server has startup warnings:
  15. Wed Dec 19 13:11:44.276 [initandlisten]
  16. Wed Dec 19 13:11:44.276 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
  17. Wed Dec 19 13:11:44.276 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
  18. Wed Dec 19 13:11:44.276 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
  19. Wed Dec 19 13:11:44.276 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
  20. Wed Dec 19 13:11:44.276 [initandlisten]
  21. > show dbs
  22. local 0.03125GB

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

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

插入数据

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

查找数据

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

修改数据

将李四的年龄修改为30

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

删除数据

删除一条学生记录

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

删除学生数据集

  1. > db.student.drop()
  2. true
  3. > show collections
  4. system.indexes
  5. teacher

退出

exit

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

 

 

 

转载于:https://www.cnblogs.com/kailugaji/p/10146444.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/655946
推荐阅读
相关标签
  

闽ICP备14008679号