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?
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.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.
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.
No if all is on one machine it does not leave the machine.
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
Unplug your ethernet cable or disconnect your wifi and try your database connection and you will have your answer. No fuss, no muss!
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.
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.