12

To clarify:

I'm using my public hostname to connect to a MySQL database. The hostname resolves to my server's external IP (e.g. 1.2.3.4). Is the data I'm sending/receiving via the MySQL connection going over the internet at all? Would it be faster to use localhost? Will it take up my server's bandwidth?

8 Answers 8

28

If you want to be sure, you can use traceroute 1.2.3.4. This will list all the routers between the host running the command and the device with 1.2.3.4 IP address.

7
  • 3
    +1 for incorporating the word "devive" into your sentence. Now I know one more English word. Aug 13, 2011 at 21:24
  • en.wiktionary.org/wiki/devive says "devive - To render lifeless." I'm confused. Maybe it should be "device"?
    – anon
    Aug 14, 2011 at 0:13
  • 7
    So basically, "that's the last traceroute he'll ever run"
    – Xeoncross
    Aug 14, 2011 at 0:36
  • 5
    @xeoncross - That's one hell of a traceroute. Where do I get the version that has this command line option? I am on Windows XP. I am also interested in installing the ping of death.
    – anon
    Aug 14, 2011 at 2:16
  • 6
    in windows you'd use tracert
    – Mauro
    Aug 14, 2011 at 14:21
15

If you use "localhost" as hostname for connecting to your MySQL server, MySQL won't use TCP, but sockets. This should be the fastest way to connect to a locally running MySQL server.

If your server has the external IP set up "properly", that is, it is not behind a firewall or proxy in an internal network, the traffic won't leave your server as it knows that the target IP address is the same system.

3
  • 1
    Also, if you're on Linux, using "localhost" or "127.0.0.1" will connect via the lo adapter, which is pseudo-adapter that's only used for those addresses. Not sure about Windows, but I think it has something similar. Aug 13, 2011 at 19:26
  • 2
    @Nathan Moos As stated in MySQL manual: For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file.
    – Shi
    Aug 13, 2011 at 19:33
  • 1
    I was describing a more generic situation where you are connecting to localhost, not necessarily MySQL. Sorry that I ignored the wording of the question and answer. Aug 13, 2011 at 21:05
5

You don't mention anything about your routing or switching equipment but you could go out of your way to ensure your data goes outside before coming back but in reality almost certainly your data won't be going out this way.

2
  • I'm quite sure there is an implicit question: "What do I need to check to know that the data does not travel outside at all?"
    – Lie Ryan
    Aug 14, 2011 at 8:41
  • I agree, but he doesn't provide any details about his routing or switching, so it's impossible at this stage to add more without making wild assumptions.
    – Chopper3
    Aug 14, 2011 at 8:53
3

No if all is on one machine it does not leave the machine.

6
  • All traffic from the machine to the machine gets routed over the loopback interface. If you disconnect from your network it will still work.
    – BillThor
    Aug 13, 2011 at 16:21
  • @BillThor sure, but that was not the question.
    – mailq
    Aug 13, 2011 at 16:45
  • Not sure about this: it said it the name resolves to the machine's public (internet) IP address. If the machine is behind NAT using portforwarding, it has to go at least as far as the gateway.
    – Joel Coel
    Aug 13, 2011 at 17:10
  • @BillThor IP addresses other than 127.0.0.1 are assigned to the machine by the network (router/switch/ISP) so disconnecting from the network effectively unassigns the addresses.
    – Lukman
    Aug 13, 2011 at 17:14
  • 2
    This is only true if the server knows that it is 1.2.3.4, if it only knows that it has in internal ip (i.e 192.168.x.x) than the data will, at a minimum, be sent to the router.
    – Unkwntech
    Aug 13, 2011 at 19:57
2

if your server ip configuration on its machine uses the public IP address, then no traffic will leave your server (but its safer to be sure and use localhost anyway for the performance benefits)

if your IP is nat'ed and your router has the public ip and your server has an internal IP then at the very least traffic will likely leave your server and be reflected back onto your server from the router assuming that the router does port reflection as part of its port forwarding - so in a sense no traffic will leave your network but some traffic will leave your server

without any network setup info i cant be more definitive than that

1

Unplug your ethernet cable or disconnect your wifi and try your database connection and you will have your answer. No fuss, no muss!

3
  • Not even close to correct. Local network?
    – navaho
    Aug 14, 2011 at 3:06
  • @navaho Original poster said "would it be faster to use localhost" which indicates that the database is on the same machine as his application; therefore to use my solution local network access is not needed. Aug 15, 2011 at 18:05
  • The question was If I send data to my computer over the internet, does it actually go over the internet? The description is I'm using my >public hostname< to connect to a MySQL database. The hostname >resolves to my server's external IP< (e.g. 1.2.3.4). Is the data I'm sending/receiving via the MySQL connection going over the internet at all? Your answer did not answer the question as posed. Yank the cord and see what happens is, at best, coming up short. Yes, you answered the secondary question, would it be faster to use localhost, but as to the original question you came up short.
    – navaho
    Aug 15, 2011 at 22:06
0

Your results may differ but I'm able to access my system via the external address just fine with the router disconnected from the Internet. However, to do this I had to configure the router with the appropriate fixed IP address, as this is normally issued by the ISP via DHCP.

As for the speed difference, theoretically using localhost should be faster because it removes the various overheads involved in traversing a network. In real terms though the difference should be so minimal that you should never be able to notice it.

0

In addition to traceroute (already mentioned) you could use tcpdump or wireshark and watch for any mysql connections getting routed out of your host. Localhost should always be faster.

You must log in to answer this question.

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