Create a MySQL database
On web hosting, a MySQL database is created in KeyHelp, with a dedicated user and limited privileges. That separation prevents a compromised application from reading another site's databases.
1. Create the database and user
In KeyHelp, open the database section and create a database for your domain. The panel may prefix the name with your account identifier; note the exact name, you will need it in code.
Then create a user:
- one user per application;
- a long, unique password generated by a password manager;
- host
localhostfor a connection from the same server; - privileges limited to that database, without
GRANT.
The panel shows the host, port (3306 by default), database name and user. Keep these values out of Git.
2. Connect from PHP
The pdo_mysql extension is the standard route. Example connection with error handling:
<?php
declare(strict_types=1);
$dsn = 'mysql:host=localhost;dbname=YOUR_DB;charset=utf8mb4';
$pdo = new PDO($dsn, 'user', 'strong-password', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);Use prepared statements for any user-supplied value. Never build a query by concatenation. The configuration file holding the password must stay outside the web root.
3. Import data
For a modest dump, use the KeyHelp import interface (or phpMyAdmin when offered): .sql file, utf8mb4 encoding. Check that the dump contains no conflicting CREATE DATABASE or any DROP DATABASE.
For large volumes, the interface import may time out. Split the file or use the command line if the panel offers it. Then check:
- the expected number of tables;
InnoDBengine andutf8mb4encoding;- indexes on search columns;
- actual data, not just the schema.
4. Back up
A database without a backup is an outage waiting to happen. Export regularly from the KeyHelp interface (or phpMyAdmin when offered), download the archive and keep it offsite. Test the restore in a temporary database: import the dump, run a few queries, compare counts.
Never store a .sql dump in the web root: it would be downloadable and would contain all your data.
5. Verify
- The PHP connection returns a clear error when credentials are wrong.
SHOW TABLESlists the expected tables.- The user cannot access another database.
- A slow query does not block the whole site: watch indexes and connections.
On an Access denied error, check the host (localhost rather than a loopback address if the panel requires it) and the exact user name.
Advertisement
Was this article helpful?
Read next
- Manage files with FTP and SFTPChoose the right protocol, connect, respect the tree and protect sensitive files.
- Choose the PHP versionPHP 8.4 as the baseline, extensions, compatibility and a safe version change.
- Publish your first websitePrepare files, find the document root, upload, test and enable HTTPS.