基本上,通过 rootUser/创建的用户进行的 mongo-auth 在本地工作正常,但在通过 Coolify/Dokploy 部署后在我的服务器上失败 这是我的 docker 撰写文件:
version: '3.8'
services:
mongodb-dev:
image: mongo:latest
container_name: mongodb-dev
restart: always
# env_file: ${ENV_FILE:-.env}
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
ports:
- '27017:27017'
volumes:
- mongodbdata:/data/db
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro //This should be executed by Mongo during startup
networks:
- my-network
my-nest-backend:
build: .
container_name: my-nest-backend
ports:
- '${PORT}:${PORT}'
depends_on:
- mongodb-dev
networks:
- my-network
# env_file: ${ENV_FILE:-.env}
environment:
DATABASE_NAME: ${DATABASE_NAME}
DATABASE_USER: ${DATABASE_USER}
DATABASE_PASS: ${DATABASE_PASS}
MONGODB_HOST: ${MONGODB_HOST}
NODE_ENV: ${NODE_ENV}
PORT: ${PORT}
MONGO_URI: mongodb://${DATABASE_USER}:${DATABASE_PASS}@${MONGODB_HOST}:27017/${DATABASE_NAME}?authSource=admin&directConnection=true
command: npm run start:debug
volumes:
mongodbdata:
networks:
my-network:
driver: bridge
NestJS:
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
MongooseModule.forRootAsync({
useFactory: async () => {
const logger = new Logger('MongooseModule');
const uri = process.env.MONGO_URI;
try {
// Establish a MongoDB connection with the provided URI
return {
uri,
useNewUrlParser: true,
useUnifiedTopology: true
};
} catch (error) {
logger.error(`MyLog Failed to connect to MongoDB: ${error.message}`, error.stack);
throw error; // Rethrow to prevent app from starting with bad DB connection
}
},
}),
QuoteModule,
PhilosopherModule,
AuthModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule implements OnModuleInit {
private readonly logger = new Logger(AppModule.name);
async onModuleInit() {
this.logger.log('MyLog AppModule initialized');
}
}
没有任何细节就失败:
MongoServerError: Authentication failed.
at Connection.sendCommand (/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:297:27)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Connection.command (/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:325:26)
at async executeScram (/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/auth/scram.js:79:22)
at async ScramSHA1.auth (/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/auth/scram.js:39:16)
at async performInitialHandshake (/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connect.js:101:13)
at async connect (/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connect.js:19:9)
可能出了什么问题?
因为您的本地数据库与远程数据库不同,请尝试为数据库使用外部服务,例如 mongo atlas。所以在部署的版本中也许你没有用户