Process: 9171 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)
SERVER [x.x.x.x:27017] (Type: UNKNOWN)
|_/ Connection error (MongoSocketOpenException): Exception opening socket
|____/ Socket error: Connection refused (Connection refused)
Details:
Timed out after 5000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}, {address=x.x.x.x:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
connection binding to localhost:27017 but not x.x.x.x:27017,this is the problem.
1. find the mongodb configuration file
写道
DEFAULT CONFIGURATION FILE
On Linux, a default /etc/mongod.conf configuration file is included when using a package manager to install MongoDB.
On Windows, a default <install directory>/bin/mongod.cfg configuration file is included during the installation.
On macOS, a default /usr/local/etc/mongod.conf configuration file is included when installing from MongoDB’s official Homebrew tap.
2.vim mongod.conf and then edit the 127.0.0.1 to 0.0.0.0 or special IP
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#Enter 127.0.0.1, :: to bind local machine
[root@xxx bin]# systemctl restart mongodb.service
Failed to restart mongodb.service: Unit not found.
[root@xxx bin]# netstat -nltp|grep mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 7533/mongod
Failed to start mariadb.service: Unit not found.
[root@xxx ~]# sudo systemctl enable mongod
[root@xxx ~]# sudo service mongod restart
Redirecting to /bin/systemctl restart mongod.service
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
[root@xxx ~]# systemctl status mongod.service
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu 2020-01-09 18:40:03 CST; 18s ago
Docs: https://docs.mongodb.org/manual
Process: 9171 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)
Process: 9168 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 9166 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 9164 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Main PID: 7533 (code=exited, status=0/SUCCESS)
Jan 09 18:40:03 xxx systemd[1]: Starting MongoDB Database Server...
Jan 09 18:40:03 xxx mongod[9171]: about to fork child process, waiting un...s.
Jan 09 18:40:03 xxx mongod[9171]: forked process: 9179
Jan 09 18:40:03 xxx mongod[9171]: ERROR: child process failed, exited wit...14
Jan 09 18:40:03 xxx mongod[9171]: To see additional information in this o...n.
Jan 09 18:40:03 xxx systemd[1]: mongod.service: control process exited, c...14
Jan 09 18:40:03 xxx systemd[1]: Failed to start MongoDB Database Server.
Jan 09 18:40:03 xxx systemd[1]: Unit mongod.service entered failed state.
Jan 09 18:40:03 xxx systemd[1]: mongod.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
Cause by Mongodb SOCK isn't belong to mongodb group & user。
[root@xxx ~]# cd /tmp
[root@xxx tmp]# ls
Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>
mongodb-27017.sock
systemd-private-7a4de57bec754f068733415dfd6b94a6-chronyd.service-wJqM50
[root@xxx tmp]# sudo chown mongodb:mognodb mongodb-27017.sock
chown: invalid user: ‘mongodb:mognodb’
[root@xxx tmp]# sudo chown mongod:mognod mongodb-27017.sock
chown: invalid group: ‘mongod:mognod’
[root@xxx ~]# groups mongod
mongod : mongod
[root@xxx ~]sudo cchown mongod:mongod /tmp/mongodb-27017.sock
[root@xxx ~]# sudo service mongod start
Redirecting to /bin/systemctl start mongod.service
[root@xxx ~]# systemctl start mongod.service
[root@xxx ~]# sudo service mongod status
Redirecting to /bin/systemctl status mongod.service
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2020-01-15 10:59:13 CST; 25s ago
it works!
Process: 23490 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=48), Failed to start MongoDB...
1.查看 mongod.conf 配置文件找到 MongoDB日志path (/etc/mongod.conf):
path: /var/log/mongodb/mongod.log
2.查看日志具体报错信息
[initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1,xxx.xx.xx.xx", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid", timeZoneInfo: "/usr/share/zoneinfo" }, storage: { dbPath: "/var/lib/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
[initandlisten] Failed to set up listener: SocketException: Cannot assign requested address
[initandlisten] now exiting
[initandlisten] shutting down with code:48
最后有一个 Failed to set up listener: SocketException: Cannot assign requested address 错误信息,bind_ip 不要用公网地址,要用内网地址
3.vim /etc/mongod.conf 去掉 bindIp: "127.0.0.1,xxx.xx.xx.xx" 后的公网IP
重启 systemctl restart mongod.service
Active: active (running) since Mon 2020-02-03 11:46:53 CST; 2min 52s ago
It works!