Connect your PHP site to MySQL: host, database name and credentials
For a PHP site to read and write its MySQL database, it needs four pieces of information: the host, the database name, the user name and the user's password. This article shows where to find them in cPanel and how to put them in your code.
Before you start
- You already have a database and a user linked to it. If not, create them first with the article "Create a MySQL database and its user in cPanel".
- You can edit your site's files (cPanel File Manager or FTP).
- On a cPanel hosting account, database and user names usually start with a prefix followed by an underscore (
_), taken from your account name. The prefix is part of the name: copy it too.
Steps
- Open the list of your databases. In cPanel, under "Databases", open Manage My Databases. On older cPanel versions the screen is called "MySQL Databases".
- Note the full database name. In the Current Databases table, the "Database" column gives the exact name, prefix included (for example
account_myshop). The "Privileged Users" column gives the exact name of the user allowed to use it (for exampleaccount_shop). - Use
localhostas the host. In the usual case the database is on the same server as the site, so the host islocalhost. One exception, according to the cPanel documentation: if the screen shows a "Remote Database Host" section, the database is on a separate server. Use the address shown there. - Find the password. cPanel never displays it. If you lost it, go down to the "Current Users" table, click Change Password next to the user, enter a new one, then update it in your site's configuration file.
- Write the connection in your code. Here is a complete PDO example. Replace the four values with yours.
<?php
// config.php: keep this file outside the public folder if you can
$host = 'localhost';
$database = 'account_myshop';
$user = 'account_shop';
$password = 'your-password';
try {
$pdo = new PDO(
"mysql:host=$host;dbname=$database;charset=utf8mb4",
$user,
$password,
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
} catch (PDOException $e) {
error_log($e->getMessage());
exit('Could not connect to the database.');
}
If you use a CMS, do not touch the code: change the four values in its configuration file (for WordPress, wp-config.php at the root of the site).
6. Test. Open a page of the site that reads the database. If it displays normally, the connection works.
Expected result
The site reads and writes the database with no error message. No password appears on any public page.
If something goes wrong
- "Access denied for user…" (error 1044 or 1045). The user or the password is wrong, or the user was not added to the database. Check the "Privileged Users" column; otherwise, in Manage My Databases, in the "Add User To Database" section, pick the user and the database, click Add, tick ALL PRIVILEGES and confirm.
- "Unknown database…" (error 1049). The database name is wrong, most often because the prefix is missing. Copy the name from the table.
- Blank page or 500 error. Do not show PHP errors to visitors: the PHP documentation says to set
display_errorsto 0 in production, because a message can contain your credentials. Read the site's error log instead (the "Errors" screen in cPanel). - It works on your computer but not online. On your computer the database probably has another name, user and password. Take the four values from cPanel.
FAQ
What is the MySQL host on cPanel?
It is localhost, unless the Manage My Databases screen shows a "Remote Database Host" section: in that case, use the address shown there.
Do I need the prefix in the database name? Yes. The prefix is part of the name, for the database and for the user. Copy the names exactly as displayed.
Can I see my MySQL password in cPanel? No, cPanel does not show it. You can set a new one with "Change Password" and copy it into your site.
PDO or mysqli? Both connect to the same database with the same four values. The PHP documentation has examples for each.
Going further
Your site needs a database, PHP and a cPanel space to run: see our web hosting.