Skip to content
Servers and Linux2 min read

What is localhost?

Short answer

localhost is a reserved name meaning "this machine", pointing to 127.0.0.1 in IPv4 and ::1 in IPv6. A connection to localhost never leaves for the network; it stays inside the computer or server the program is running on.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Where you’ll see it
  2. localhost or 127.0.0.1?
  3. Common mistakes
  4. On ArabyHost

This kind of connection is called loopback: data goes out and comes straight back to the same machine. So the meaning depends on where the code runs. localhost on your laptop is your laptop; localhost in code running on a server is that server.

Where you’ll see it

  1. Local development: run a project on your computer and open it at something like http://localhost:3000, where 3000 is the port.
  2. Database settings: when MySQL runs on the same server, wp-config.php says:
define( 'DB_HOST', 'localhost' );

or, in a Laravel .env file:

DB_HOST=localhost
  1. The hosts file: the name-to-address mapping lives in /etc/hosts on Linux and macOS, and in C:\Windows\System32\drivers\etc\hosts on Windows.

localhost or 127.0.0.1?

Usually it makes no difference, but MySQL and MariaDB on Linux tell them apart: localhost connects through a local socket file, while 127.0.0.1 connects over TCP on port 3306. If you see Can’t connect to local MySQL server through socket, try the other value. Sometimes localhost also resolves to the IPv6 ::1 while the service only listens on IPv4, and the connection is refused.

Common mistakes

  • An app listening only on localhost: nothing outside the machine can reach it, which is usually intentional for security, as with databases and Redis.
  • localhost links left after a move: build a site on your laptop, upload it, and image and page URLs may still say localhost in the database, so visitors see an unstyled page.
  • A changed database host: after moving to another host, check DB_HOST if you get Error establishing a database connection.

On ArabyHost

MySQL/MariaDB databases run on your account’s own server, so DB_HOST is localhost in WordPress, Laravel and similar apps. On the Developer plans, keep connection details in environment variables or a .env file rather than in your code, and if a connection error appears, the Araby AI assistant reads the error log and explains the cause and the fix.

FAQ

Why does my database host say localhost instead of the server's address?

Because the database usually runs on the same server as your site. Connecting through localhost is faster and safer because the traffic never leaves the machine.

I sent a friend a localhost link and it didn't open. Why?

On their computer, localhost means their computer, not yours. To share a project, put it on hosting or use an address that's reachable from the internet.

Are localhost and 127.0.0.1 the same thing?

In most software, yes. MySQL and MariaDB on Linux treat them differently: localhost means a local socket file, while 127.0.0.1 means a TCP connection.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.