What the database hostname is
On a cPanel account the database hostname is localhost, because MySQL runs on the same server as the site. Open cPanel, then Server Information, and read Server Name only when the database sits elsewhere. Enter localhost in wp-config.php or .env, and a refused connection means the value is wrong.
The hostname is the address your application uses to reach the MySQL server. It is not the domain name, not the cPanel login URL, and not the database name. Those are three other strings in the same config file.
Read it from cPanel in three clicks
Log in to cPanel for the account, not the billing area. The MySQL Databases page lists your databases and users, but it does not print the host, which is why people hunt for it there and give up.
- Open cPanel and find the General Information panel on the right of the home screen.
- Click Server Information. The Server Name row is the machine's hostname, and Shared IP Address is the number behind it.
- If your site and database are on this account, type localhost in the app config. Use the Server Name only for a remote database.
You know it worked when the app loads without a database error and the MySQL Databases page shows the same user connected. The prefix on the database and user is covered below.
Confirm the server with hostname and host
If cPanel Terminal or SSH is available, four commands confirm what the panel reports. Run them on the server, not on your laptop.
| Command | Output to read |
|---|---|
| hostname | The short server name, for example server42 |
| hostname -f | The full name, for example server42.webji.services |
| host server42.webji.services | The name followed by its IP address |
| dig +short server42.webji.services | The IP address on its own, for example 203.0.113.10 |
The address above is a documentation placeholder; yours will differ. CentOS, AlmaLinux and Ubuntu all answer these commands the same way. On a cPanel server the database still answers at localhost, so the server name matters only for an outside client.
Test the host before you redeploy. This command connects as the database user and asks for one row:
| mysql -h localhost -u cpuser_wp -p cpuser_wp -e "SELECT 1;" | Prints a one-row table containing 1. Any error names the host or the credentials. |
Where the value goes in wp-config.php and .env
WordPress stores the host in DB_HOST inside wp-config.php, next to DB_NAME, DB_USER and DB_PASSWORD. Laravel stores the same key as DB_HOST in .env. Both take localhost on a cPanel account.
Change the file in File Manager or over SFTP, save it, then reload the site. A cached config can hide the edit: clear the Laravel config cache with php artisan config:clear when the old value keeps coming back.
localhost or 127.0.0.1, and why the difference matters
localhost tells the MySQL client to use the local Unix socket. 127.0.0.1 forces a TCP connection to the loopback address. Both reach the same server, but they are different paths.
MySQL grants are per host. A user created as 'cpuser_wp'@'localhost' can connect over the socket and be refused over TCP. If localhost works and 127.0.0.1 does not, the host value is not the problem; the grant is.
What a wrong hostname looks like
The error text tells you whether the host or the password is wrong. Read it before changing either.
| Message | What it means |
|---|---|
| Error establishing a database connection | WordPress cannot connect. Check DB_HOST first, then the user and password. |
| SQLSTATE[HY000] [2002] Connection refused | Nothing is listening at that address and port, so the host value is wrong or the server is down. |
| Unknown MySQL server host | DNS cannot resolve the name. Fix the spelling, or use localhost. |
| Can't connect through socket | The client expected a local socket. The database is remote, so use its hostname. |
| Access denied for user (using password: YES) | The host was reached. The user name or password is wrong, not the host. |
A wrong host never produces "access denied". That message proves the connection arrived, so leave DB_HOST alone and fix the credentials.
When the database is on another server
A remote database needs two changes. In cPanel open Remote MySQL and add the connecting IP as an access host. Then set DB_HOST to the database server's hostname, not localhost.
Many shared hosts block port 3306 from outside for safety. If the grant is right and the connection still times out, the port is closed and the database has to be reached from the same network. That is a support question, not a config edit.
Database names and users carry the cPanel prefix
cPanel prefixes every database and user with the account name. A database you named shop is really cpuser_shop, and the user is cpuser_shopuser. The full prefixed names go in the same config file as the host.
Use the nameserver lookup only when the site itself will not resolve. A database error is not a DNS problem, and changing nameservers will not fix it. The cPanel hosting page covers the panel itself, and the domain IP lookup shows what the site resolves to.
Support is by email at support@webji.services, 24/7 in Hindi and English, with an average first reply under 60 minutes. The office phone is not a support line.
Frequently Asked Questions
How do I find my database hostname in cPanel?
On a cPanel account it is localhost. Open cPanel, then General Information, then Server Information, and read Server Name. That server name is the host only when the database runs on a different machine from the site.
Is the database host always localhost?
For MySQL on the same cPanel server, yes. It is not localhost when the application connects to a remote database, a managed database service or a second server.
What is the difference between localhost and 127.0.0.1?
localhost uses the local Unix socket, while 127.0.0.1 forces a TCP connection. A MySQL user granted only at localhost can fail over 127.0.0.1 even though both reach the same machine.
Where does WordPress store the database host?
In DB_HOST inside wp-config.php. Laravel stores the same value as DB_HOST in .env. Save the file and reload the site after the change.
What does error 2002 mean?
SQLSTATE HY000 error 2002 means the client could not reach a MySQL server at that address or port. The host value is wrong, the server is down, or a remote connection is blocked.
Why does my database name have a prefix?
cPanel prefixes every database and user with the account name. A database called shop is really cpuser_shop, and that full name goes in the same config file.

