1.更新yum
Centos 的安装工具,安装时间稍长
yum -y update
2.添加MongoDB源文件
在/etc/yum.repos.d 创建一个 mongodb-org.repo 文件
touch /etc/yum.repos.d/mongodb-org.repo
编辑mongodb-org.repo文件
vim /etc/yum.repos.d/mongodb-org.repo
添加文件内容(这里我使用的版本3.2)
[mongodb-org]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/3.2/x86_64/
gpgcheck=0
enabled=1
3.安装Mongo
yum install -y mongodb-org
4.启动mongodb
service mongod start
我在这里报错了
出现这个错误:
Starting mongod (via systemctl):
Job for mongod.service failed because the control process exited with error code.
See “systemctl status mongod.service” and “journalctl -xe” for details.
解决办法:
将mongoDB添加到systemd
vim /usr/lib/systemd/system/mongod.service
[Unit]
Description=mongodb database
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=--quiet -f /etc/mongod.conf"
ExecStart=/usr/bin/mongod $OPTIONS run
PIDFile=/var/run/mongodb/mongod.pid
[Install]
WantedBy=multi-user.target
建立链接
ln -s /usr/lib/systemd/system/mongod.service /etc/systemd/system/multi-user.target.wants/
重新加载systemctl
systemctl daemon-reload
将mongoDB添加到systemd后错误变为:
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
打印错误
systemctl status mongod.service
错误如下
mongod.service - SYSV: Mongo is a scalable, document-oriented database.
Loaded: loaded (/etc/rc.d/init.d/mongod; bad; vendor preset: disabled)
Active: failed (Result: exit-code) since 五 2021-03-26 15:05:55 CST; 5min ago
Docs: man:systemd-sysv-generator(8)
Process: 50458 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=1/FAILURE)
3月 26 15:05:55 localhost.localdomain systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database....
3月 26 15:05:55 localhost.localdomain mongod[50458]: Error starting mongod. /var/run/mongodb/mongod.pid exists.
3月 26 15:05:55 localhost.localdomain systemd[1]: mongod.service: control process exited, code=exited status=1
3月 26 15:05:55 localhost.localdomain systemd[1]: Failed to start SYSV: Mongo is a scalable, document-oriented database..
3月 26 15:05:55 localhost.localdomain systemd[1]: Unit mongod.service entered failed state.
3月 26 15:05:55 localhost.localdomain systemd[1]: mongod.service failed.
这时候试着去看一下mongo的log
tail -f /var/log/mongodb/mongod.log
出现这个错误
......
2021-03-26T14:58:31.238+0800 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted
......
查看一下/tmp/mongodb-27017.sock的读写权限
ls -l /tmp/mongodb-27017.sock
结果是文件权限是root
srwx------ 1 root root 0 3月 26 14:42 /tmp/mongodb-27017.sock
把所有者修改为mongod
chown mongod:mongod /tmp/mongodb-27017.sock
重新启动mongo
service mongod start
现在启动就成功了
5.设置开机启动
chkconfig mongod on
6。配置远程访问
修改mongod.conf配置文件
vim/etc/mongod.conf
7.重启mongod
service mongod restart