数据库应用:Ubuntu 20.04 安装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)   无固定结构 。

2)  数据结构由键值(key=>value)对组成。MongoDB 的文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组,单个对象的结构是清晰的。

3)   没有复杂的表连接。不需要维护表与表之间的内在关联关系。

4)   查询功能强大。MongoDB的查询功能几乎与SQL一样强大,使用基于文档的查询语言,可以对文档进行动态查询。

5)   易于调优和扩展。具备高性能、高可用性及可伸缩性等特性

6)   应用程序对象与数据库对象天然对应。

7)   可以基于内存存储或者硬盘文件存储,提供丰富的查询操作和索引支持,也有事务操作,可以更快地更稳定的访问数据。(mongoDB4.0以后才真正支持所谓的多文档事务操作)

(3)术语对比

表1 SQL与MongoDB对比

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

(4)基本操作

db.help()                    help on db methods   查看操作数据的方法
	db.mycoll.help()             help on collection methods  查看集合的操作方法
	sh.help()                    sharding helpers  查看分片集share的帮助信息
	rs.help()                    replica set helpers 查看复制集的帮助信息
	help admin                   administrative help 查看管理的操作帮助信息
	help connect                 connecting to a db help
	help keys                    key shortcuts
	help misc                    misc things to know
	help mr                      mapreduce

	show dbs                     show database names  查看当前系统所有的数据库
	show collections             show collections in current database 查看当前数据库所有的数据集合
	show users                   show users in current database  查看当前数据库中所有的管理员用户
	show profile                 show most recent system.profile entries with time >= 1ms
	show logs                    show the accessible logger names 查看全部日志
	show log [name]              prints out the last segment of log in memory, 'global' is default                          查看指定日志信息
	use <db_name>                set current database   切换操作的数据库
	db.mycoll.find()             list objects in collection mycoll  列出当前指定集合下的所有文档
	db.mycoll.find( { a : 1 } )  list objects in mycoll where a == 1 按条件查询指定集合下所有文档
	it                           result of the last line evaluated; use to further iterate
	                             查看更多的查询结果,相当于下一页
	DBQuery.shellBatchSize = x   set default number of items to display on shell
	                             修改返回结果数据的单页显示数量,默认20条
	exit                         quit the mongo shell  退出终端

二、实验

1.Ubuntu 20.04 安装MongoDB

(1)安装依赖包

sudo apt-get install -y libcurl4 openssl

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

sudo systemctl stop mongod
sudo apt-get purge mongodb*
sudo apt-get auto-remove
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

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

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

(4)注册mongodb源

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
# 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

# 重新加载配置,并启动mongodb
sudo systemctl daemon-reload
sudo systemctl start mongod

# 查看运行状态
sudo systemctl status mongod
# 如果mongodb状态为stop,则运行 sudo systemctl enable mongod

# 停止mongodb
sudo systemctl stop mongod

# 重启mongodb
sudo systemctl restart mongod

(9)进入交互终端

root@node1:/etc/apt# mongo
MongoDB shell version v4.4.25
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3a390cc8-34b9-4160-bfe5-ddcd5149a056") }
MongoDB server version: 4.4.25
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        # 警告:强烈建议使用XFS文件系统,并使用WiredTiger存储引擎。
		# 解释:因为当前ubuntu使用的是ext4文件系统,mongodb官方建议使用XFS文件系统功能更能发挥mongodb的性能,忽略不管
        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
        # 警告:当前mongodb没有为数据库启用访问控制。对数据和配置的读写访问是不受限制的。
        # 解释:后面会创建数据库用户采用密码登陆的。暂时不用管
        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
---

(10)查看默认库

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

(11)退出交互终端

exit
# quit()

(12)查看版本

mongo --version
# 或者终端内部使用 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

root@node1:~# sudo lsof /var/lib/dpkg/lock
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvf
      Output information may be incomplete.
COMMAND      PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
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)原因分析

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

(3)解决方法

apt-get purge vim-common
apt install vim

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

(1)查看系统信息

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

(2)先备份下原始源

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

(3)修改文件   

sudo vim  /etc/apt/source.list

清除原有的,替换以下源

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted
deb http://mirrors.aliyun.com/ubuntu/ focal universe
deb http://mirrors.aliyun.com/ubuntu/ focal-updates universe
deb http://mirrors.aliyun.com/ubuntu/ focal multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted
deb http://mirrors.aliyun.com/ubuntu/ focal-security universe
deb http://mirrors.aliyun.com/ubuntu/ focal-security multiverse

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

sudo  apt update

(5)升级软件

sudo  apt upgrade

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

(1)查看系统信息

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

(2)先备份下原始源

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

(3)修改文件   

sudo vim  /etc/apt/source.list

清除原有的,替换以下源

deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
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)mongod
它是处理MongoDB系统的主要进程。主要负责处理数据请求,管理数据存储,和执行后台管理操作。当我们运行mongod命令意味着正在启动MongoDB进程, 并且在后台运行。

2)mongo
它是一个命令行实现的客户端操作mongodb的工具,用于连接一个特定的mongod实例。当我们没有带参数运行mongo命令它将使用默认的localhost:27017和mongod进行连接。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值