数据库应用:Ubuntu 20.04 安装MongoDB

目录

一、理论

1.MongoDB

二、实验

[1.Ubuntu 20.04 安装MongoDB](#1.Ubuntu 20.04 安装MongoDB)

三、问题

[1.Ubuntu Linux的apt 包管理器更新安装软件报错](#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的优势

bash 复制代码
1)   无固定结构 。

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

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

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

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

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

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

(3)术语对比

表1 SQL与MongoDB对比

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

(4)基本操作

bash 复制代码
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)安装依赖包

bash 复制代码
sudo apt-get install -y libcurl4 openssl

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

bash 复制代码
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)导入包管理系统使用的公钥

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

(4)注册mongodb源

bash 复制代码
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)更新源

bash 复制代码
sudo apt-get update

(6)安装mongodb

bash 复制代码
sudo apt-get install -y mongodb-org

(7)创建数据存储目录

bash 复制代码
sudo mkdir -p /data/db

(8)启动和关闭MongoDB

bash 复制代码
# 重新加载配置,并启动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)进入交互终端

bash 复制代码
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)查看默认库

bash 复制代码
> 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)退出交互终端

bash 复制代码
exit
# quit()

(12)查看版本

bash 复制代码
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

bash 复制代码
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 掉这个进程

bash 复制代码
root@node1:~# sudo kill -9 163898

然后删除锁文件

bash 复制代码
root@node1:~# sudo rm /var/lib/dpkg/lock

最后运行以下命令

bash 复制代码
root@node1:~# sudo dpkg --configure -a

2.Ubuntu20.04安装vim报错

(1)报错

(2)原因分析

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

(3)解决方法

bash 复制代码
apt-get purge vim-common
apt install vim

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

(1)查看系统信息

bash 复制代码
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)先备份下原始源

bash 复制代码
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup

(3)修改文件

bash 复制代码
sudo vim  /etc/apt/source.list

清除原有的,替换以下源

bash 复制代码
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)保存下,更新软件库

bash 复制代码
sudo  apt update

(5)升级软件

bash 复制代码
sudo  apt upgrade

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

(1)查看系统信息

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

(2)先备份下原始源

bash 复制代码
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup

(3)修改文件

bash 复制代码
sudo vim  /etc/apt/source.list

清除原有的,替换以下源

bash 复制代码
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)保存下,更新软件库

bash 复制代码
sudo  apt update

(5)升级软件

bash 复制代码
sudo  apt upgrade

5.mongod和mongo的区别

(1)区别

bash 复制代码
1)mongod
它是处理MongoDB系统的主要进程。主要负责处理数据请求,管理数据存储,和执行后台管理操作。当我们运行mongod命令意味着正在启动MongoDB进程, 并且在后台运行。

2)mongo
它是一个命令行实现的客户端操作mongodb的工具,用于连接一个特定的mongod实例。当我们没有带参数运行mongo命令它将使用默认的localhost:27017和mongod进行连接。
相关推荐
后端小张22 分钟前
Redis 执行 Lua,能保证原子性吗?
数据库·redis·缓存
离开地球表面_9923 分钟前
索引失效?查询结果不正确?原来都是隐式转换惹的祸
数据库·后端·mysql
lipviolet30 分钟前
Redis系列---Redission分布式锁
数据库·redis·分布式
Zhen (Evan) Wang32 分钟前
.NET 6 API + Dapper + SQL Server 2014
数据库·c#·.net
毕设木哥1 小时前
25届计算机专业毕设选题推荐-基于python+Django协调过滤的新闻推荐系统
大数据·服务器·数据库·python·django·毕业设计·课程设计
洛阳泰山1 小时前
Llamaindex 使用过程中的常见问题 (FAQ)
java·服务器·数据库·python·llm·rag·llamaindex
没有名字的小羊2 小时前
Linux基础命令——账户简单管理
linux·运维·服务器·网络·数据库
单字叶2 小时前
MySQL数据库管理
数据库·mysql
ID_云泽2 小时前
MVCC机制解析:提升数据库并发性能的关键
数据库·mvcc
小灰灰是码农...2 小时前
MySQL的数据库课程设计的基本步骤和考虑因素
数据库