Guide Databases & MySQL

How to create a MySQL database and user

Every application needs a database, a user, and that user attached to the database — here is the order to do it in.

Updated 6 min read Beginner

Almost everything that needs a database — WordPress, a custom PHP application, a forum, a shopping cart — needs the same three things underneath it: a database to store data in, a user allowed to read and write it, and that user attached to that database. Get these three right and the application's own installer takes care of the rest.

This is done in the databases section of your control panel, not by writing SQL by hand. It takes about two minutes.

Why a database and a user are separate things

It is easy to assume a database comes with its own login, the way a filing cabinet comes with its own key. It does not. A MySQL database is just a named container for tables, and a MySQL user is a completely separate login that has to be explicitly told which databases it may touch. One user can be attached to several databases, and one database can have several users attached to it, each with different privileges.

Keeping them separate is deliberate: it is what lets you give a website's application user full access to its own database while that same user has no ability to see any other database on the account, even though both live on the same server.

One database and user per application

Resist the temptation to reuse one database user across several sites "to save time". If one application is compromised, a shared user gives an attacker a path into every database it can touch. A dedicated user per database costs nothing and contains the damage if something ever does go wrong.

Step 1: create the database

In the databases section of your control panel, choose the option to create a new database and give it a name. Two things are worth getting right at this point, because both are awkward to change later:

  • Use a name that describes what it is for. On an account running several sites, example_shop is far easier to identify in six months than db1.
  • Note any automatic prefix. Some control panels prepend your account name to whatever you type, so the database you create as shop might actually be named account_shop. Whatever name is shown after saving is the real name — use that one, not the one you typed.

Step 2: create a database user

Still in the databases section, create a new user. This is a separate form from the database itself. Give it:

  • A username, which is often also prefixed by your control panel in the same way as the database name.
  • A strong password. Use a generated one if your control panel offers to create it, and store it in a password manager rather than a text file — you will need to paste it into your application's configuration in a moment.
Copy the password carefully

A password copied with a trailing space, or retyped by hand with one character wrong, produces exactly the same symptom as no password at all: the application refuses to connect and reports a generic error. Copy it directly from the panel rather than re-typing it.

Step 3: attach the user to the database

A user that exists but is not attached to any database cannot do anything. In the same databases section, find the option to add a user to a database — sometimes a separate step, sometimes a dropdown on the database's own page — and select the user you just created against the database you just created.

You will usually be asked which privileges to grant. For an application you are installing yourself, choose full or all privileges. Restricting a user to only some privileges is occasionally worth doing for security — see how to grant a user access to a database for when that applies and how to do it — but it will stop an installer that expects to create its own tables if you get it wrong on a fresh install.

Step 4: gather the four connection details

Every application configuration file asks for the same four pieces of information, usually under names close to these:

ValueWhere it comes from
Database hostAlmost always localhost on shared hosting — see why the host is "localhost" if that looks wrong.
Database nameExactly as shown in the control panel, including any account prefix.
Database userExactly as shown, including any account prefix.
Database passwordThe one you set in step 2. It cannot be read back from the panel afterwards, only reset.

Paste these into your application's setup screen or configuration file — for WordPress that is the database fields in wp-config.php, or the equivalent screen during a fresh install.

Step 5: confirm it actually connects

Run the application's installer, or a short standalone test if you are building something custom. A working connection is the signal that all four values were entered correctly and the privileges were granted properly; if it fails, the error is almost always one of the values being slightly wrong rather than anything more serious.

Our guide to fixing a database connection error walks through each of the likely causes in the order worth checking them, and connecting an application to MySQL covers the connection code itself if you are writing it rather than pasting it into someone else's installer.

If you have worked through the setup and the connection still will not succeed, contact us with the database name and what you have already checked, and we can look at the account side directly.

Related reading