MongoDB在Windows、Linux、Docker环境下的安装

Docker

Docker安装

拉取镜像

docker pull mongo

创建和启动容器

 docker run -d --restart=always -p 27017:27017 --name mymongo -v $PWD/db:/data/db -d mongo

进入容器

docker exec -it mymongo /bin/bash 

使用MongoDB客户端进行操作

root@d8110bf377a7:/# mongo
MongoDB shell version v5.0.5
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session {
    
     "id" : UUID("8157afb1-535f-4b9c-9aef-5f6a0e32c5a2") }
MongoDB server version: 5.0.5
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.

查询所有的数据库

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> 

远程连接

#更新源
apt-get update
#安装 vim
apt-get install vim
#修改 mongo 配置文件
vim /etc/mongod.conf.orig

开启远程连接

# 注释掉# bindIp: 127.0.0.1 或者改成bindIp: 0.0.0.0 
# network interfaces
net:
  port: 27017
 # bindIp: 127.0.0.1
  bindIp: 0.0.0.0

创建用户

# 远程连接要有对应的用户名和数据库

# 使用admin数据库
>use amdin

# 创建用户admin,密码123456
>  db.createUser({
    
     user:'admin',pwd:'123456',roles:[ {
    
     role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});

# 使用创建用户信息进行连接
> db.auth('admin', '123456')

Windows

Windows安装

官网下载Mongodb服务器

在这里插入图片描述

双击安装,选择Custom自定义安装路径

在这里插入图片描述
在这里插入图片描述

视情况选择,data: 数据库路径 log: 日志路径

在这里插入图片描述
安装完毕后,自动将MongoDB加入到系统服务

在这里插入图片描述

服务相关命令

启动MongoDB服务
net start MongoDB

关闭MongoDB服务
net stop MongoDB

移除MongoDB服务
"D:\program files\mongodb\Server\4.4\bin\mongod.exe" ‐‐remove

安装MongoDB服务
mongod.exe ‐‐config "D:\program files\mongodb\Server\4.4\bin\mongod.cfg" ‐‐install

压缩包形式安装

若不是安装包形式安装,只需在mongod.exe目录指定一个配置文件mongodb.conf ,以指定配置文件形式安装

#数据库路径 
dbpath=D:\program files\mongodb\Server\4.4\data 

#日志输出文件路径 
logpath=D:\program files\mongodb\Server\4.4\log\mongod.log

#错误日志采用追加模式 
logappend=true 

#启用日志文件,默认启用 
journal=true 

#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false 
quiet=true 

#端口号 默认为27017 
port=27017 	

Mac、Ubuntu、Centos一键安装

Mac

参考文档:https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/

brew install mongodb

Ubuntu

参考文档:https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/

sudo apt-get install mongodb

centos

参考文档:https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-red-hat/

yum -y install mongodb mongodb-devel mongodb-server

源码安装

选择相应版本和操作系统并下载

https://www.mongodb.com/try/download/community

解压

tar -zxvf mongodb-linux.tgz

移动到/usr/local/目录下

mv -r mongodb-linux/ /usr/local/mongodb

在shell的初始化脚本.bashrc中添加mongodb可执行文件到环境变量PATH中

1.进入.bashrc文件中

cd ~
sudo vi .bashrc

2.在.bashrc文件的最后添加:

export PATH=/usr/local/mongodb/bin:$PATH

使用Atlas免费MongoDB云数据库

Atlas免费服务即官方提供云托管MongoDB数据库。

官网地址:https://www.mongodb.com/zh-cn/cloud/atlas

申请云数据库

1.注册账号
在这里插入图片描述

2.设置账户信息
在这里插入图片描述

3.选择shared类型服务
在这里插入图片描述
4.选择云提供商和区域
在这里插入图片描述
5.等待自动部署集群服务
在这里插入图片描述
6.部署完成
在这里插入图片描述

连接测试

1.点击Connet,添加访问IP与添加访问账户
在这里插入图片描述
2.获取连接地址
在这里插入图片描述

3.选择连接方式,如:MongoDB Shell

在这里插入图片描述

4.复制连接地址字符串
在这里插入图片描述
5.连接测试

安装MongoDB Shell 或不安装,使用mongo命令也可连接
C:\Users\Administrator>mongo "mongodb+srv://cluster0.ruiuw.mongodb.net/myFirstDatabase" --username root
MongoDB shell version v4.4.2
Enter password:
connecting to: mongodb://cluster0-shard-00-00.ruiuw.mongodb.net:27017,cluster0-shard-00-02.ruiuw.mongodb.net:27017,cluster0-shard-00-01.ruiuw.mongodb.net:27017/myFirstDatabase?authSource=admin&compressors=disabled&gssapiServiceName=mongodb&replicaSet=atlas-hnmioy-shard-0&ssl=true
Implicit session: session {
    
     "id" : UUID("6c75e4f3-7bea-4d1e-acc7-12dc4ab1ceb1") }
MongoDB server version: 4.4.8
MongoDB Enterprise atlas-hnmioy-shard-0:PRIMARY>

猜你喜欢

转载自blog.csdn.net/qq_38628046/article/details/129170256