当前位置:   article > 正文

数据库应用:Ubuntu 20.04 安装MongoDB_ubuntu mongodb

ubuntu mongodb

目录

一、理论

1.MongoDB

二、实验

1.Ubuntu 20.04 安装MongoDB

三、问题

1.Ubuntu Linux的apt 包管理器更新安装软件报错

2.Ubuntu20.04安装vim报错

3.Ubuntu20.04如何更换阿里源

4.Ubuntu22.04如何更换阿里源


一、理论

1.MongoDB

(1)概念

MongoDB 是由C++语言编写并基于分布式文件存储的开源数据库,属于NOSQL 。

MongoDB 是一款介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的NOSQL数据库。它面向文档存储,而且安装和操作起来都比较简单和容易,而且它支持各种流行编程语言进行操作,如Python,Node.js,Java,C++,PHP,C#等。

目前在大数据、内容管理、持续交付、移动应用、社交应用、用户数据管理、数据中心等领域皆有广泛被使用。

(2)MongoDB相对于RDBMS的优势

  1. 1) 无固定结构 。
  2. 2) 数据结构由键值(key=>value)对组成。MongoDB 的文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组,单个对象的结构是清晰的。
  3. 3) 没有复杂的表连接。不需要维护表与表之间的内在关联关系。
  4. 4) 查询功能强大。MongoDB的查询功能几乎与SQL一样强大,使用基于文档的查询语言,可以对文档进行动态查询。
  5. 5) 易于调优和扩展。具备高性能、高可用性及可伸缩性等特性
  6. 6) 应用程序对象与数据库对象天然对应。
  7. 7) 可以基于内存存储或者硬盘文件存储,提供丰富的查询操作和索引支持,也有事务操作,可以更快地更稳定的访问数据。(mongoDB4.0以后才真正支持所谓的多文档事务操作)

(3)术语对比

表1 SQL与MongoDB对比

SQLMongodb
库(database)库(database)
表(Table)集合(Collection)
行/记录(Row)文档(Document)
列/字段(Col)字段/键/域(Field)
主键(Primary Key)对象ID(ObjectId)
索引(Index)索引(Index)

(4)基本操作

  1. db.help() help on db methods 查看操作数据的方法
  2. db.mycoll.help() help on collection methods 查看集合的操作方法
  3. sh.help() sharding helpers 查看分片集share的帮助信息
  4. rs.help() replica set helpers 查看复制集的帮助信息
  5. help admin administrative help 查看管理的操作帮助信息
  6. help connect connecting to a db help
  7. help keys key shortcuts
  8. help misc misc things to know
  9. help mr mapreduce
  10. show dbs show database names 查看当前系统所有的数据库
  11. show collections show collections in current database 查看当前数据库所有的数据集合
  12. show users show users in current database 查看当前数据库中所有的管理员用户
  13. show profile show most recent system.profile entries with time >= 1ms
  14. show logs show the accessible logger names 查看全部日志
  15. show log [name] prints out the last segment of log in memory, 'global' is default 查看指定日志信息
  16. use <db_name> set current database 切换操作的数据库
  17. db.mycoll.find() list objects in collection mycoll 列出当前指定集合下的所有文档
  18. db.mycoll.find( { a : 1 } ) list objects in mycoll where a == 1 按条件查询指定集合下所有文档
  19. it result of the last line evaluated; use to further iterate
  20. 查看更多的查询结果,相当于下一页
  21. DBQuery.shellBatchSize = x set default number of items to display on shell
  22. 修改返回结果数据的单页显示数量,默认20条
  23. exit quit the mongo shell 退出终端

二、实验

1.Ubuntu 20.04 安装MongoDB

(1)安装依赖包

sudo apt-get install -y libcurl4 openssl

(2)关闭和卸载原有的mongodb

  1. sudo systemctl stop mongod
  2. sudo apt-get purge mongodb*
  3. sudo apt-get auto-remove
  4. sudo rm -r /var/log/mongodb
  5. sudo rm -r /var/lib/mongodb

(3)导入包管理系统使用的公钥

  1. # 导入包管理系统使用的公钥
  2. wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
  3. # 如果命令执行结果没有显示OK,则执行此命令在把上一句重新执行:sudo apt-get install gnupg

(4)注册mongodb源

  1. echo "deb https://mirrors.tuna.tsinghua.edu.cn/mongodb/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
  2. # echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

(5)更新源

sudo apt-get update

(6)安装mongodb

sudo apt-get install -y mongodb-org

(7)创建数据存储目录

sudo mkdir -p /data/db

(8)启动和关闭MongoDB

  1. # 重新加载配置,并启动mongodb
  2. sudo systemctl daemon-reload
  3. sudo systemctl start mongod
  4. # 查看运行状态
  5. sudo systemctl status mongod
  6. # 如果mongodb状态为stop,则运行 sudo systemctl enable mongod
  7. # 停止mongodb
  8. sudo systemctl stop mongod
  9. # 重启mongodb
  10. sudo systemctl restart mongod

(9)进入交互终端

  1. root@node1:/etc/apt# mongo
  2. MongoDB shell version v4.4.25
  3. connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
  4. Implicit session: session { "id" : UUID("3a390cc8-34b9-4160-bfe5-ddcd5149a056") }
  5. MongoDB server version: 4.4.25
  6. Welcome to the MongoDB shell.
  7. For interactive help, type "help".
  8. For more comprehensive documentation, see
  9. https://docs.mongodb.com/
  10. Questions? Try the MongoDB Developer Community Forums
  11. https://community.mongodb.com
  12. ---
  13. The server generated these startup warnings when booting:
  14. # 警告:强烈建议使用XFS文件系统,并使用WiredTiger存储引擎。
  15. # 解释:因为当前ubuntu使用的是ext4文件系统,mongodb官方建议使用XFS文件系统功能更能发挥mongodb的性能,忽略不管
  16. 2023-11-26T18:37:17.202-08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
  17. # 警告:当前mongodb没有为数据库启用访问控制。对数据和配置的读写访问是不受限制的。
  18. # 解释:后面会创建数据库用户采用密码登陆的。暂时不用管
  19. 2023-11-26T18:37:17.594-08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
  20. ---

(10)查看默认库

  1. > db
  2. test
  3. > show db;
  4. uncaught exception: Error: don't know how to show [db] :
  5. shellHelper.show@src/mongo/shell/utils.js:1145:11
  6. shellHelper@src/mongo/shell/utils.js:819:15
  7. @(shellhelp2):1:1
  8. > show dbs;
  9. admin 0.000GB
  10. config 0.000GB
  11. local 0.000GB
  12. >

(11)退出交互终端

  1. exit
  2. # quit()

(12)查看版本

  1. mongo --version
  2. # 或者终端内部使用 version()

三、问题

1.Ubuntu Linux的apt 包管理器更新安装软件报错

(1)报错

Could not get lock /var/lib/dpkg/lock-frontend

(2)原因分析

因为某些程序在系统后台进行着某些 apt 操作,因此锁定了 apt 数据库,所以暂时不能进行 apt 操作。就像windows上某程序或文件被另一进程所占有时,其他进程也无法访问一样,这是符合设计逻辑的。

(3)解决方法

首先找出是哪个进程占用了锁文件 /var/lib/dpkg/lock

  1. root@node1:~# sudo lsof /var/lib/dpkg/lock
  2. lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvf
  3. Output information may be incomplete.
  4. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  5. unattende 163898 root 11uW REG 8,5 0 787519 /var/lib/dpkg/l

接着kill 掉这个进程

root@node1:~# sudo kill -9 163898

然后删除锁文件

root@node1:~# sudo rm /var/lib/dpkg/lock

最后运行以下命令

root@node1:~# sudo dpkg --configure -a

2.Ubuntu20.04安装vim报错

(1)报错

(2)原因分析

  1. vim : 依赖: vim-common (= 2:8.0.1453-1ubuntu1) 但是 2:8.1.2269-1ubuntu5.7 正要被安装
  2. E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。

(3)解决方法

  1. apt-get purge vim-common
  2. apt install vim

3.Ubuntu20.04如何更换阿里源

(1)查看系统信息

  1. root@master1:~# lsb_release -a
  2. No LSB modules are available.
  3. Distributor ID: Ubuntu
  4. Description: Ubuntu 20.04.3 LTS
  5. Release: 20.04
  6. Codename: focal

(2)先备份下原始源

sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup

(3)修改文件   

sudo vim  /etc/apt/source.list

清除原有的,替换以下源

  1. deb http://mirrors.aliyun.com/ubuntu/ focal main restricted
  2. deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted
  3. deb http://mirrors.aliyun.com/ubuntu/ focal universe
  4. deb http://mirrors.aliyun.com/ubuntu/ focal-updates universe
  5. deb http://mirrors.aliyun.com/ubuntu/ focal multiverse
  6. deb http://mirrors.aliyun.com/ubuntu/ focal-updates multiverse
  7. deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
  8. deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted
  9. deb http://mirrors.aliyun.com/ubuntu/ focal-security universe
  10. deb http://mirrors.aliyun.com/ubuntu/ focal-security multiverse

(4)保存下,更新软件库

sudo  apt update

(5)升级软件

sudo  apt upgrade

4.Ubuntu22.04如何更换阿里源

(1)查看系统信息

  1. root@ubuntu2204:~# lsb_release -a
  2. No LSB modules are available.
  3. Distributor ID: Ubuntu
  4. Description: Ubuntu 22.04 LTS
  5. Release: 22.04
  6. Codename: jammy

(2)先备份下原始源

sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup

(3)修改文件   

sudo vim  /etc/apt/source.list

清除原有的,替换以下源

  1. deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
  2. deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
  3. deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
  4. deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
  5. deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
  6. deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
  7. deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
  8. deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
  9. deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
  10. deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

(4)保存下,更新软件库

sudo  apt update

(5)升级软件

sudo  apt upgrade

5.mongod和mongo的区别

(1)区别

  1. 1)mongod
  2. 它是处理MongoDB系统的主要进程。主要负责处理数据请求,管理数据存储,和执行后台管理操作。当我们运行mongod命令意味着正在启动MongoDB进程, 并且在后台运行。
  3. 2)mongo
  4. 它是一个命令行实现的客户端操作mongodb的工具,用于连接一个特定的mongod实例。当我们没有带参数运行mongo命令它将使用默认的localhost:27017和mongod进行连接。

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

闽ICP备14008679号