3

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?

trysis
  • 8,086
  • 17
  • 51
  • 80
  • Are you looking for a way to connect a client to a mongod interface other than localhost? If so, I would check to see whether the bindip option is set either as an argument to mongod or on your mongod.conf file. This option is likely what would be limiting mongod to a local interface. – James Wahlin Jul 22 '14 at 16:25
  • No, I'm looking for a way to **name** a server something besides `localhost`, even if it is on my local machine. I don't need to connect to any other machine, nor does this question ask about **connecting** at all. – trysis Jul 22 '14 at 17:38
  • In your question you state "The docs specify a way to connect to a server with a specific name". This means connecting a client to your mongod instance. mongod itself is not configured with "name" information outside of in the replica set definition - when running a replica set. – James Wahlin Jul 22 '14 at 18:14
  • @JamesWahlin, That's disappointing, but if it's the answer, it's the answer. I'll accept if you make it such. – trysis Jul 22 '14 at 18:20
  • Why disappointing? What is the problem you are trying to solve? When mongod starts on a server it opens a port and is available to any client that can connect to that port (given the interface isn't restricted using the bindip option). That means you should be able to connect the shell via --host with any of localhost, hostname or IP address. – James Wahlin Jul 22 '14 at 18:29
  • What's disappointing is that I can't use virtual hosting to change the name of the server from `localhost`. This is possible with website "servers", but I suppose database "servers" are a different beast. – trysis Jul 22 '14 at 19:26
  • mongod is just a process that runs within your hosted virtual machine, not a flavor of server or virtual machine. – James Wahlin Jul 22 '14 at 19:37
  • "Within your hosted virtual machine": Do you mean that MongoDB creates a virtual machine for you, like Java or C#, or that you believe I am using a virtual machine while accessing MongoDB? Because I am not (currently) using a virtual machine to access the OS with MongoDB. – trysis Jul 22 '14 at 19:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57786/discussion-between-james-wahlin-and-trysis). – James Wahlin Jul 22 '14 at 19:48

2 Answers2

5

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
Stennie
  • 63,885
  • 14
  • 149
  • 175
  • So does this mean we can only have one MongoDB server per whatever-MongoDB-counts-as-a-computer (without replication/sharding)? – trysis Jul 23 '14 at 02:32
  • @trysis No, you can still run multiple instances that are bound to different ports on the same server. See http://stackoverflow.com/questions/15124610/multiple-instances-of-mongo-db-on-same-server – JohnnyHK Jul 23 '14 at 04:32
  • Oh, duh, why didn't I think of that? Also, if you have a computer with more than one IP address, you could have more than one server this way, too. – trysis Jul 23 '14 at 05:05
  • It seems you can use virtual hosts. You just can't use the `bindIp` parameter. It's really strange because in my `hosts` file I bind many, many hosts to 127.0.0.1, but I can only connect to them by name if I don't bind to 127.0.0.1. – trysis Jul 23 '14 at 06:34
  • @trysis: Typically you do not want to run multiple database servers in the same VM/host instance (i.e. similar to how "virtualhost" in your Apache config is running one web server instance listening to multiple hostnames). Rather than setting up a lot of hostnames for the same database server, I would encourage you to use different database names and credentials for your applications. This will be a lot easier to manage (and isn't specific to MongoDB). – Stennie Jul 23 '14 at 11:31
  • I probably won't use many virtual hosts for a single Apache/Node instance when I'm actually working. Right now I probably have a couple dozen but they're mainly for practice/development. I'm not actually using most of them, and many of the connected production websites are probably defunct. I figured it may be the same for MongoDB, though, as you said, @Stennie, I could just do multiple databases instead. – trysis Jul 23 '14 at 13:09
0

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")