一、docker安装
1. 添加 docker-compose.yml文件
version: '3'
services:
mongo:
image: mongo
container_name: mongo
hostname: mongo
restart: always
ports:
- "27017:27017"
environment:
TZ: Asia/Shanghai
MONGO_INITDB_DATABASE: test
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 123456
volumes:
- /etc/localtime:/etc/localtime
- ./data:/data/db
command: mongod
2.安装mongo镜像并启动容器
docker-compose up -d
二、手动安装
1.创建mongo安装文件
touch /etc/yum.repos.d/mongodb-org.repo
vi /etc/yum.repos.d/mongodb-org.repo
2.编辑mongodb-org.repo文件
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
3.安装mongo
yum install -y mongodb-org
4.进入mongo配置文件
vim /etc/mongod.conf
5.更改配置内容
bindIp: 0.0.0.0
6.启动mongo配置
sudo service mongod restart
7.启动mongo
mongo
8.查看27017端口
ss -lpn src :27017
9.查看版本号
db.version()
10.使用数据库
use admin;
11.创建账号密码和权限
db.createUser({user:"root",pwd:"myadmin",roles:[{role:"readWrite",db:"admin"}]})
db.createUser({user:"root",pwd:"myadmin",roles:[{role:"readWrite",db:"pay"}]})
12.增加权限
db.auth('root','myadmin');