1.创建yum源文件
vi /etc/yum.repos.d/mongodb-4.2.repo
内容
[mngodb]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/4.2/x86_64/
gpgcheck=0
enabled=1
2.安装并修改配置
yum install -y vim mongodb
vim /etc/mongod.conf
bindIp: 172.0.0.1 改为 bindIp: 0.0.0.0 #注意冒号与ip之间需要一个空格
3.启动,停止,查看状态,重启
systemctl start mongod.service
systemctl stop mongod.service
systemctl status mongod.service
systemctl restart mongod.service
4.关闭firewall:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
5.设置开机启动
systemctl enable mongod.service
6.启动Mongo shell,查看数据库
mongo
show dbs
7.需要的话启用权限控制:
编辑mongod.conf注释bindIp,并重启mongodb
vim /etc/mongod.conf
systemctl restart mongod.service
8.springboot测试代码
https://github.com/gzz2017gzz/spring-boot2-example/tree/master/58-spring-boot-MongoTemplate