Centos7下yum安装mongodb

1.配置MongoDB的yum源

vim /etc/yum.repos.d/mongodb-org-4.2.repo

#添加以下内容:

[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc 
#这里可以修改 gpgcheck=0, 省去gpg验证    

2.安装MongoDB

安装命令:

yum -y install mongodb-org

安装完成后

Running transaction
  正在安装    : mongodb-org-server-4.2.6-1.el7.x86_64             1/5 
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
  正在安装    : mongodb-org-shell-4.2.6-1.el7.x86_64              2/5 
  正在安装    : mongodb-org-mongos-4.2.6-1.el7.x86_64             3/5 
  正在安装    : mongodb-org-tools-4.2.6-1.el7.x86_64              4/5 
  正在安装    : mongodb-org-4.2.6-1.el7.x86_64                    5/5 
  验证中      : mongodb-org-tools-4.2.6-1.el7.x86_64              1/5 
  验证中      : mongodb-org-mongos-4.2.6-1.el7.x86_64             2/5 
  验证中      : mongodb-org-shell-4.2.6-1.el7.x86_64              3/5 
  验证中      : mongodb-org-4.2.6-1.el7.x86_64                    4/5 
  验证中      : mongodb-org-server-4.2.6-1.el7.x86_64             5/5 

已安装:
  mongodb-org.x86_64 0:4.2.6-1.el7                                                                                           

作为依赖被安装:
  mongodb-org-mongos.x86_64 0:4.2.6-1.el7  mongodb-org-server.x86_64 0:4.2.6-1.el7  mongodb-org-shell.x86_64 0:4.2.6-1.el7 
  mongodb-org-tools.x86_64 0:4.2.6-1.el7  

完毕!

查看mongo安装位置 :

whereis mongod

查看修改配置文件 :

vim /etc/mongod.conf

3.启动MongoDB

启动mongodb :

systemctl start mongod.service

停止mongodb :

systemctl stop mongod.service

查到mongodb的状态:

systemctl status mongod.service

4.外网访问需要关闭防火墙:

关闭firewall:

systemctl stop firewalld.service     #停止firewall
systemctl disable firewalld.service  #禁止firewall开机启动

5.启动Mongo shell

命令:

mongo 

查看数据库:

show dbs

6.设置mongodb远程访问:

编辑mongod.conf注释bindIp,并重启mongodb.(这句配置代表只能本机使用,所以需注释)

vim /etc/mongod.conf

重启mongodb使修改生效:

systemctl restart mongod.service

一些常规操作:

查看所有的数据库:

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

查看当前连接在哪个数据库下面,可以直接输入db:

> db 
test
>  

切换到test数据库下面:

> use test
switched to db test
>   

查看admin数据库下有哪些表或者叫collection,可以输入:

> show collections
system.version
> 
posted @ 2020-05-07 16:05  好极了  阅读(215)  评论(0编辑  收藏  举报