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?

Tom Marthenal
  • 2,116
  • 7
  • 25
  • 37

8 Answers8

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.

mouviciel
  • 476
  • 4
  • 6
  • 3
    +1 for incorporating the word "devive" into your sentence. Now I know one more English word. – Gani Simsek Aug 13 '11 at 21:24
  • http://en.wiktionary.org/wiki/devive says "devive - To render lifeless." I'm confused. Maybe it should be "device"? – anon Aug 14 '11 at 00:13
  • 7
    So basically, "*that's the last traceroute he'll ever run*" – Xeoncross Aug 14 '11 at 00:36
  • @anon, That's probably what he/she meant, seems like a keyboard typo since "c" and "v" are next to each other on QWERTY and the word has "the" in front of it. But here within this context it might also mean "dies", i.e. the command borns at the host, goes through (hopefully not so) many routers, enjoys a short life journey, and is rendered lifeless (dies / ends up) at 1.2.3.4 IP address. It sounds more poetic. But that's just my imagination :) – Gani Simsek Aug 14 '11 at 00:46
  • 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 '11 at 02:16
  • @Gani Simsek - Sorry for the poetry, I fixed the typo. @anon - The question is tagged linux, so I didn't bother finding a portable solution. On Windows, I believe that cygwin includes `traceroute`. – mouviciel Aug 14 '11 at 08:52
  • 6
    in windows you'd use tracert – Mauro Aug 14 '11 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.

Shi
  • 435
  • 2
  • 11
  • 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. – Nathan Moos Aug 13 '11 at 19:26
  • 2
    @Nathan Moos As stated in [MySQL manual](http://dev.mysql.com/doc/refman/5.0/en/connecting.html): *For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file.* – Shi Aug 13 '11 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. – Nathan Moos Aug 13 '11 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.

Chopper3
  • 101,299
  • 9
  • 108
  • 239
  • 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 '11 at 08: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 '11 at 08:53
3

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

mailq
  • 17,023
  • 2
  • 37
  • 69
  • 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 '11 at 16:21
  • @BillThor sure, but that was not the question. – mailq Aug 13 '11 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 '11 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 '11 at 17:14
  • @Lukman - not always. You can have static ip addresses as well – Joel Coel Aug 13 '11 at 19:32
  • 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 '11 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

anthonysomerset
  • 4,233
  • 2
  • 21
  • 24
1

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

  • Not even close to correct. Local network? – navaho Aug 14 '11 at 03: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. – Steven Striga Aug 15 '11 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 '11 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.

John Gardeniers
  • 27,458
  • 12
  • 55
  • 109
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.

Andrew Case
  • 3,489
  • 3
  • 23
  • 39