# MySQL and databases on shared hosting

> What a database is for, the four tasks you will actually do, and the one connection mistake that causes most of the errors people hit.

Source: https://cwp.sg/tutorials/mysql/  
Author: Ben Johnson  
Last verified: 2026-09-21

**In short:** - A database stores the parts of your site that change: posts, users, orders. - On shared hosting you create the database in a panel, then connect your code to it. - The host for your database is almost always localhost, not your domain name. - Most "database" errors are really connection errors, and they have short fixes. - Back up before you import or change anything. This is the one non-optional step.

A database is where your site keeps the things that change. Flat HTML files are
fine for a page that stays the same. The moment you have posts, users, comments
or orders, you need somewhere to store them and read them back. On shared
hosting that store is almost always MySQL, or its near-identical cousin MariaDB.
This page is the map of what you do with it.

You do not write a database from scratch. Software like WordPress creates its own
tables and manages them for you. Your job is smaller: make the database, connect
your code to it, move data in and out, and keep a backup. Four tasks, and this
page links each one to its how-to.

## Making one

You create a database in your hosting panel, not in code. The panel gives you
three things: a database name, a username, and a password. Write them down
exactly, including any prefix the host adds to the name. The full steps, and what
each field means, are in
[create a MySQL database](/tutorials/create-a-mysql-database/).

A free plan gives you plenty of databases but only a few connections at once.
That connection limit matters more than the number of databases, and it is why a
busy site can start refusing visitors while the database itself is nearly empty.

## Connecting your code to it

This is where most people get stuck, and it is almost always the same mistake.
Your code needs four values: host, database name, username, and password. Three
of them are obvious. The fourth, the host, is the one people get wrong.

The database host is not your domain name. It is often `localhost`, because the
database usually runs on the same server as your site — though some hosts put it
on a separate machine and give you its address in the panel. Typing your domain
there is the single most common reason a connection fails. The full explanation,
with the exact code, is in
[connect PHP to MySQL](/tutorials/connect-php-to-mysql/).

When a connection fails, the site usually shows a database connection error. That
page —
[error establishing a database connection](/errors/error-establishing-a-database-connection/)
— walks through the four causes in order, and the first one it checks is the host
value.

## Moving data in and out

Sooner or later you will move a database, usually because you are migrating a
site. You export it to a file on the old host and import that file on the new
one. The tool for both is phpMyAdmin, which every shared host provides. The steps
are in [import a database](/tutorials/import-a-database/).

Before you import anything, export what is already there first. An import can
overwrite existing tables, and there is no undo. A backup file you did not need
costs you nothing. The one you skipped costs you the site.

One thing a database is not for: your images, stylesheets and uploads. Those are
files, and they belong on disk, not in MySQL. Storing large files as database rows
is slow, bloats every backup, and eats into the connection budget for no benefit.
The database holds the text and the structure; the files stay in `public_html`.
Keeping that line clean keeps both the database and the backups small.

## The limit you can hit

A database keeps every connection open for a short time. If your code opens more
connections than the plan allows, new visitors get turned away with a
[too many connections](/errors/too-many-connections/) error, even though nothing
is broken. The fix is usually to close connections properly in code, or to add
caching so most visitors never touch the database at all.

## The whole job on shared hosting

If you are setting up a database-driven site from scratch, the order is:

1. Create the database in the panel and record the four values.
2. Put those values into your code, with the host set to `localhost`.
3. Load your site and confirm it connects.
4. Before any migration, export a backup first.
5. If you hit a connection limit, add caching before you do anything else.

That is the whole of it. Each step is a linked how-to, and the one to never skip
is the backup. Everything else here is recoverable; a database you overwrote
without a copy is not. Get into the habit of exporting before you change anything,
and the worst day a database can give you becomes a five-minute restore.

---

Content Website Platform (cwp.sg) operates the free hosting it writes about; see https://cwp.sg/about/ for the methodology and the commercial disclosure.
