저장을 습관화
에러 기록 - MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 본문
에러 기록 - MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
ctrs 2023. 6. 15. 01:56순서
1. 에러 발생 상황과 증상
2. 해결 방법
1. 에러 발생 상황과 증상
node.js 공부 중
app.js 파일에서 express를 이용해 웹서버를 가동시키고,
./schemas/index.js에 등록되어있는 내용에 따라 mongoDB에 연결하려함
터미널에 node app.js을 입력하였고
'3000 포트로 서버가 열렸어요!' express 정상 가동 로그를 확인함
그리고 조금 후 터미널에 아래와 같은 에러 로그가 출력됨
$ node app.js
3000 포트로 서버가 열렸어요!
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at _handleConnectionErrors (C:\(생략)\node_modules\mongoose\lib\connection.js:792:11)
at NativeConnection.openUri (C:\(생략)\node_modules\mongoose\lib\connection.js:767:11)
at runNextTicks (node:internal/process/task_queues:60:5)
at listOnTimeout (node:internal/timers:538:9)
at process.processTimers (node:internal/timers:512:7) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
몽고디비 연결 에러 MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (C:\(생략)\node_modules\mongodb\lib\sdam\topology.js:278:38)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined,
[Symbol(errorLabels)]: Set(0) {}
}
mongoDB 연결에 이상이 발생
이 상태에서 DB에 접근하여 조작하려고 하면
2. 해결 방법
mongoDB 연결 정보가 들어있는 index.js에서 localhost를 127.0.0.1로 변경
const mongoose = require("mongoose");
const connect = () => {
mongoose
.connect("mongodb://127.0.0.1/spa_mall") // 기존 localhost에서 127.0.0.1로 변경
.catch((err) => console.log(err));
};
mongoose.connection.on("error", (err) => {
console.error("몽고디비 연결 에러", err);
});
module.exports = connect;
[내용 출처]
https://mingg123.tistory.com/129
[MongoDB]MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 에러 해결
require('dotenv').config(); const Koa = require('koa'); const Router = require('koa-router'); const bodyParser = require('koa-bodyparser'); const mongoose = require('mongoose'); const { PORT, MONGO_URI } = process.env; mongoose .connect(MONGO_URI, { useNew
mingg123.tistory.com
'공부 > node.js' 카테고리의 다른 글
.toHexString() (0) | 2023.06.19 |
---|---|
node.js mongoose 시간 적는 법 (0) | 2023.06.19 |
에러 기록 - MongoServerError: E11000 duplicate key error (0) | 2023.06.18 |
과제 기록 - Node.js, express, MongoDB, mongoose, AWS EC2 (0) | 2023.06.18 |
mongoose의 __V 필드 (0) | 2023.06.15 |