简体   繁体   中英

Change Name of MongoDB Server

In MongoDB, how do I change the name of the server/daemon, or create a new server with a name other than localhost ? The docs specify a way to connect to a server with a specific name, such as localhost (the --host option on the mongo command), as well as a way to specify the names of each server in a replication set, which I forget at the moment, but not a method to say the name of the server to create an instance of for the mongod command. It seems to have to be localhost .

I have node.js and apache , so I can use virtual hosting, and I obviously have a hosts file ( /etc/hosts on Unix-like OS's and C:\\WINDOWS\\System32\\drivers\\etc\\hosts on Windows), so I can presumably create a server with a different name from localhost , but the question is can I do this with the built-in commands like mongod , or with a configuration file?

Unlike the HTTP protocol (as used by your web apps running in a web server like Apache), the MongoDB Wire Protocol does not use hostnames when determining how to handle an incoming request. That means there is no equivalent of a host name or virtualhost directive for your MongoDB server.

The MongoDB server configuration file allows you to bind to one or more IP addresses . The bind IP configuration option specifies which IP address(es) the server listens to for incoming connections. For example, this might be: 127.0.0.1 (aka localhost ), a comma-delimited list of IP addresses, or all network interfaces if you don't specify a bind IP restriction.

When you connect to MongoDB and specify a hostname in your command line or driver options, this name just has to be resolvable from the external point of view. You can use whatever form of resolvable hostname you want to create outside of MongoDB (for example, in your /etc/hosts file or DNS). As long as the MongoDB server is listening to the IP your hostname resolves to (and there are no firewalls or network connectivity issues), you should be able to connect irrespective of the hostname used.

For example, while localhost is the default name available for the loopback IP address (127.0.0.1) and should always be defined .. normally your computer will also have a unique hostname defined. You can check what your default hostname resolves to in the mongo shell with getHostName() :

> getHostName()
Webscale.local

Assuming no bind IP restriction and a hostname of Webscale.local with an IP address of 192.168.1.1 , you would be able to connect with any of:

  • Webscale.local
  • 192.168.1.1
  • localhost
  • 127.0.0.1

Make use of Docker Environment, create two containers, one for your Mongo Server and another for your App, run both containers inside one docker-compose.yml file, specify that your app container would depend_on your mongo container and then replace "localhost" the connection string in your app with the name of your mongo container. Take reference from the two snippets as below:

docker-compose.yml :

version: '3'
 services:
   img_flaskapp:
      build: ./Flask/gie
      container_name: flaskapp
      ports:
        - "8000:8000"
      depends_on: 
        - img_mongodb
   img_mongodb:
      image: mongo
      container_name: mongodb
      ports:
       - "27017:27017"

app code :

connect (host="mongodb://mongodb:27017/nameofyourdb")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM