You see the "connection refused" error and don't know where to start
Your newly installed website is giving a database connection error. Or you've just set up WordPress and the installation page is asking for the database username and password. In both cases, you need one thing: creating a database, creating a separate user for it, and knowing the four values you need to write in your website's configuration file.
Do this wrong, and your website won't come up. Do it right, and it will take ten minutes.
Before you start: what you need
You need access to your hosting control panel (cPanel or DirectAdmin). If your website is on Linux hosting, these tools are inside the control panel. For a dedicated server, you usually need to install and manage MySQL yourself, which is a slightly different path.
Prepare three things in advance:
- Database name (e.g.,
mysite_db) - Username (e.g.,
mysite_user) - A strong password — at least 16 characters with uppercase letters, lowercase letters, numbers, and symbols
A weak password opens the door of your database to automated scanners. Take this seriously.
Creating a database in cPanel
In cPanel, find the MySQL® Databases section. This option is usually in the "Databases" category. Once you open the page, you'll see two forms: one for creating a database and one for creating a user.
First, create the database. The name you enter will have a prefix added to it. In cPanel, your hosting username is usually added to the beginning of the database name. For example, if your username is server1 and you enter mysite_db, the full database name becomes server1_mysite_db.
This is important: in your website's configuration file, you need to write the full name, not the name you entered yourself. Many people make a mistake here and write an incomplete name.
Creating a user and assigning permissions
Now, on the same page, fill out the user creation form. The username also gets a prefix. After creating it, you'll see the "Add User To Database" form. Add the user to the database.
Here's an important question: what permissions should we give?
The short answer: select ALL PRIVILEGES. Almost all content management systems (WordPress, Laravel, Joomla) need full access to their own database. If you give limited access, your website will throw errors at the strangest moments.
But there's one exception. If you're creating the database for a simple read-only application (e.g., a reporting dashboard that only runs SELECT queries), then limit the permissions. In practice, I give ALL PRIVILEGES for 99% of websites.
The four values you need in the configuration file
Now that the database and user are created, you need four values. Write these down in a temporary file:
- Full database name — with the prefix, e.g.,
server1_mysite_db - Full username — with the prefix, e.g.,
server1_mysite_user - Password — the one you entered when creating the user
- Database host — usually
localhost
Many people forget the fourth value. On shared hosting, the database is on the same server where your website runs, so localhost is correct. But if the database is on a separate server (e.g., on a dedicated server with a separate architecture), you need to write the IP address or hostname of the database server.
Connecting in WordPress
Open the wp-config.php file in your website's root directory. Find this section:
define( 'DB_NAME', 'server1_mysite_db' );
define( 'DB_USER', 'server1_mysite_user' );
define( 'DB_PASSWORD', 'your_strong_password' );
define( 'DB_HOST', 'localhost' );
Replace the values and save the file. That's it.
Connecting in Laravel
Edit the .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=server1_mysite_db
DB_USERNAME=server1_mysite_user
DB_PASSWORD=your_strong_password
Note that in Laravel, DB_HOST is sometimes written as 127.0.0.1 instead of localhost. Both usually work, but if one gives an error, try the other.
Where they make mistakes: passwords with special characters
You've chosen a password that includes $, #, or '. In the configuration file, these characters may be interpreted and corrupt the password.
What's the sign of this? An "Access denied for user" error even when you're sure the password is correct. Or in WordPress, an "Error establishing a database connection" error that doesn't resolve even after double-checking the password.
The solution: change the password and use only letters and numbers. Or if you must have special characters, you need to escape them with \ in PHP. The simplest way is to remove special characters. Compensate for security with password length, not with strange characters.
Common errors and quick troubleshooting
Your website still won't come up? Check these in order:
- Did you write the full database name or username with the prefix? — This is the most common error.
- Did you add the user to the database? — Creating the user and creating the database isn't enough. You must connect them in the "Add User To Database" form.
- Is the database host correct? — Changing from
localhostto127.0.0.1resolves some issues. - Is the port correct? — The default MySQL port is
3306. If your host uses a different port, you need to specify it.
If you get an "Unknown database" error, the database name is wrong. If you get "Access denied," the username or password is wrong. If you get "Connection refused," the database host address is incorrect.
After connecting: a few essential tasks
Connection established? Do two more things.
First, take a backup of the database. In phpMyAdmin, click the Export option and download the SQL file. Do this now, not after your website is full of content.
Second, if your website is WordPress, check the default tables. WordPress creates tables with the wp_ prefix during installation. If you already have a database with similar tables, there may be a conflict.
To test connection speed and ensure overall website health, you can use free webmaster tools. These tools tell you how fast your website is from the users' perspective.
When the database is on a separate server
On shared hosting, everything is on one server and localhost works. But in larger architectures, the database is placed on a separate server. In this case:
- Instead of
localhost, you write the IP address of the database server. - You need to open the web server's IP for port
3306in the database server's firewall. - Remote access to MySQL is usually disabled by default. You need to change
bind-addressin the MySQL configuration file.
This is more specialized, and if you're not familiar with it, that's okay. On shared hosting, everything is ready, and you just need to enter the names correctly.
Frequently asked questions
Why do I get an Access denied error even though the password is correct?
The most likely reason is a special character in the password that gets interpreted in the configuration file. Change the password to simple letters and numbers. Another reason is that the user is connected to the wrong database. In the control panel, check that the user is connected to exactly the same database you've written in the configuration file.
What's the difference between creating a database and creating a user?
The database is where data is stored. The user is an identity that has permission to access the database. One database can have multiple users, and one user can have access to multiple databases. For each website, it's better to create a separate database and a separate user so that if one website gets hacked, the others remain safe.
Should I write localhost or 127.0.0.1?
In most cases, both work. If one gives an error, try the other. In some specific configurations, localhost uses a Unix socket and 127.0.0.1 uses TCP/IP. If the database server is only listening on TCP/IP, 127.0.0.1 will work.
Should I install the website first or edit the configuration file first after creating the database?
First create the database and user, then edit the configuration file, and then start the installation. If you start the installation first, everything needs to be ready when it asks for database information. The correct order saves you time.