docker/节点js/mongodb部署:Mongoose错误[Mongoose服务器选择错误]:连接超时

bsxbgnwa  于 2022-12-22  发布在  Docker
关注(0)|答案(2)|浏览(146)
    • 更新2**

I face another error, if I use path as follows, I will get "invalid volume specification" error even change path to "C:/docker-entrypoint-initdb.d/", the init-mongo.sh will not be executed, then I get "UserNotFound: Could not find user "xxxx" for db "admin"

volumes:
      - ./init-mongo/:/docker-entrypoint-initdb.d/
      - dbdata:/data/db

因此我切换到Linux容器,所有错误消息都消失了。

    • 更新**

我在本地主机上禁用mongodb服务后解决了问题

net stop MongoDB

并使用端口27017

mongodb:
....
    ports:
      - "27017:27017"
.....

但是我想知道为什么我不能将端口27017更改为其他端口?我更改端口是因为如果mongodb在我的计算机上运行(使用27017端口),我会得到下面的错误信息,谢谢。

for mongodb  Cannot start service mongodb: failed to create endpoint mongodb on network server_app_network: hnsCall failed in Win32: The process cannot access the file because it is being used by another process. (0x20)
    • 原件**

我正在Windows 10上使用Docker Windows容器。经过两天的努力,我仍然无法成功部署我的项目。我的配置如下,感谢帮助。
server.js

const dbUrl = "mongodb://mongodb:27018/";
mongoose
  .connect(`${dbUrl}mydb`, { useUnifiedTopology: true, useNewUrlParser: true })
  .then(res => {
    db = mongoose.connection;
    app.listen(port, () => {
      console.log(`listening on port ${port}`);
    });
  })
  .catch(error => console.log(error));

docker-compose.yml

version: "3"
services:
  express-server:
    build:
      context: .
      dockerfile: Dockerfile
    image: express-server
    container_name: express-server
    restart: unless-stopped
    ports:
      - "3000:3000"
    links:
      - mongodb
    depends_on: 
      - mongodb
    networks:
      - app_network

  mongodb:
    image: mongo
    container_name: mongodb
    restart: unless-stopped
    ports:
      - "27018:27018"
    networks:
      - app_network
volumes:
  dbdata:
networks:
  app_network:
    driver: nat

运行docker-composeup--build-d之后

CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                 NAMES
7fac2380c803        express-server      "node server.js"         57 seconds ago       Up 43 seconds       0.0.0.0:3000->3000/tcp                express-server
9afc21569a1a        mongo               "mongod --bind_ip_all"   About a minute ago   Up 57 seconds       27017/tcp, 0.0.0.0:27018->27018/tcp   mongodb
06odsfpq

06odsfpq1#

我认为您需要在配置中传递dbName。

mongoose.connect(
    connectionString,
    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false,
      useCreateIndex: true,
      dbName: 'database-name', // IMPORTANT TO HAVE IT HERE AND NOT IN CONNECTION STRING
    },
    err => { throw err; },
  );
83qze16e

83qze16e2#

我费了很大劲才解决这个错误,最后我意识到这个错误与Docker网络有关,我已经分享了一些信息来解决link中的这个错误

相关问题