安装
step1:配置yum源
vi /etc/yum.repos.d/mongodb-org-3.4.repo
添加以下内容:
[mongodb-org-3.4]
name=MongoDB 3.4 Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=0
enabled=1
step2:安装
sudo yum install -y mongodb-org-3.4.10 mongodb-org-server-3.4.10 mongodb-org-shell-3.4.10 mongodb-org-mongos-3.4.10 mongodb-org-tools-3.4.10
step3:selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
需要重启服务器生效
setenforce 0 #临时生效,重启服务器无效
step4:防火墙设置
systemctl stop firewalld #关闭防火墙
或者开放端口号
firewall-cmd --zone=public --add-port=27017/tcp #mongodb默认端口号
firewall-cmd --reload #重新加载防火墙
step5:开启mongodb
systemctl start mongod.service #开启MongoDB
chkconfig mongod on #加入开机启动
systemctl restart mongod.service #重启MongoDB
step6:检查是否安装成功
ps -aux | grep mongod #查看数据库的进程是否存在
step7:启动mongoshell
mongo
step8:设置远程访问
编辑mongod.conf(/etc/mongod.con)注释bindIp,并重启mongodb.
卸载
mongodb卸载
step1:查看已安装的mongodb
yum list installed | grep mongo
step2:卸载
yum erase mongodb-server.x86_64 (mongodb-server.x86_64 为上一步查出的list)
mongodb设置外网访问
1)修改配置文件
vim /etc/mongod.conf
注释掉#bindIp: 127.0.0.1
2)重启
cd /usr/bin
./mongod -f /etc/mongod.conf
错误总结
1)[root@VM_0_15_centos etc]# mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
2019-07-30T12:13:03.194+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2019-07-30T12:13:03.194+0800 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed
解决方法:1)全盘搜索 find / -name mongod.lock 2)删除搜索出来的文件 3)修复:mongod --repair 4)重启
2) exception in initAndListen: 29 Data directory /data/db not found., terminating
解决方法:mkdir /data/db/ -p 然后重启
网友评论