-1

Some hosting providers doesn't have localhost as mysql hostname, instead they give-out mysql.example.com as host-name to be used in applications.

Could there be any difference in performance, as they, instead of having 127.0.0.1 (or even localhost) in host-name, use domain-name type hosts (mysql.example.com). Doesn't that take a bit more time to resolve dns to specific IP?

4 Answers 4

2

It's very hard to make anything out of your question.

I decided that your question is

Will a remote database at my provider that I can access via mysql.example.com be slower then a local database that I run on my machine/VPS and that I can access via localhost.

The answer is: It depends on many things, among them

  • The specs of your own server/VPS
  • The specs of the database server
  • The load of the database server
  • The load of your machine for non-db work
  • The type of queries you perform

and the range will be from "local is a lot faster then remote" to the opposite "the remote is a lot faster".

1

It's not entirely clear to me what you're asking, but I get the impression you're talking about a web server like Apache or nginx running on the same leased server as your MySql database in a hosting provider's data center. You're worried about the connection from the web server to the database, and the only difference between the two choices is what name you use in the connection string.

Given that scenario, it shouldn't matter which one you use. What can matter is how the two names are routed. Localhost will always be routed through the loopback interface (barring some very strange configuration). mysql.example.com can end up going out a physical interface before looping back in an upstream router our switch somewhere. You want to avoid that scenario: there's an extra hop there, the potential to use metered bandwidth, and also the fact the loopback interface often emulate 10Gbps connections (or better; at least 40Gbps is possible), while your physical interface might only be 1Gbps.

However, this isn't really a property of using the wrong name. It's a property of not having things configured so that this connection uses the loopback interface.


On the other hand, you may be talking about connecting from your local office to a MySql database hosted in a remote data center somewhere. In this case, then yes: it will be much slower, because traffic to the database must pass through your WAN connection and the internet, rather than just the local network (or your personal loopback interface, considering that you are comparing this to localhost).

However, this isn't what I'd call a "production" deployment. What I generally try to do is host all of the parts of an application on the same physical network. If it's a client server app, where the clients runs on user desktops inside a company network, then the database server should also live on that network (note that it doesn't have to be localhost: just not going out through a WAN conneciton). If it's a web app, then the database should be in the same data center (not the same server, but same network switch segment) as the web server, but those two servers can easily be a thousand miles away from the people viewing the web pages.

3
  • I got this answer from another user: Even if the domain resolves to the local server localhost should resolve quicker as it uses the loopback interface which bypasses network hardware.
    – T.Todua
    Jul 15, 2015 at 8:49
  • 1
    Connections to localhost will not necessarily go through the loopback interface. Quote from the PHP documentation: Note: Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost".. Since this comes from the mysql client library, I would assume that other MySQL implementation behave the same. Jul 15, 2015 at 9:02
  • True, but named pipe has a similar effect... it's fast compared to the physical interface and avoids metered connections or hairpinned routing.
    – Joel Coel
    Jul 15, 2015 at 13:33
1

They use a dedicated database server because it's easier to manage.

Having dedicated database servers and web servers makes it easier to update, easier to backup, etc. If you have everything for a single customer on a single server you have to run updates and backups on every single instance. By separating them you can have as much customers on a given server as the machine can handle.

A server reaches it's limit? Just add another one of the same type.

In most cases the lower speed or higher latency is negligible, if you (the customer) are at a point where it affects your application you can still move to a different kind of hosting, e.g. a root server where you control every aspect, down to which software version you want to install.

2
  • in a simple words, localhost is faster or slower in this case?
    – T.Todua
    Jul 15, 2015 at 12:06
  • 1
    As others already explained here, it depends, mainly on the hardware involved. You asked why they do it that way. The answer is: ease of management. Jul 15, 2015 at 12:15
1

When you're using "localhost" notation in fact MySQL uses UNIX domain sockets. They're more performant, comparing to TCP/IP, and for high-load it can make noticeable difference. But UNIX domain is local to the host by its nature, it's not networking, so this implies you can't use remote MySQL server with it.

2
  • please, tell us in simple words, it is faster or slower , than localhost?
    – T.Todua
    Jul 15, 2015 at 12:04
  • 1
    Actually my answer was simply put, indeed. If you compare TCP/IP stack and UNIX domain sockets (aka IPC sockets) for data transfer on the same host, IPC would be faster. But IPC is limited to same host, it doesn't support "remote hosts" connections at all. It means if you have to connect to remote SQL server, you can't use IPC for that directly.
    – poige
    Jul 15, 2015 at 12:31

Not the answer you're looking for? Browse other questions tagged .