How to set up a MySQL database and access it with phpMyAdmin

MySQL is one of the most powerful and versatile open-source database systems available, and often goes hand in hand with PHP. Understanding how to get a database set up and running however, can be slightly confusing for first-timers.cPanel, along with phpMyAdmin make setting up a MySQL database a lot easier than the other option, which would be via the command line (gulp!)

Step 1 - Log into cPanel and select MySQL Databases

Databases AvailableWhen you log in to cPanel, you should see a breakdown of your hosting package on the page. Make sure you have the ability to add new databases in the Databases section.

mySQL databases iconDepending on your version of cPanel and theme in use, this may be shown with an icon like here, or a simple text link.

Step 2 - Create a new Database

Strictly speaking, MySQL is the database engine which can run multiple databases at the same time, so the same copy of MySQL runs no matter how many databases there are. Very often it's easier to create a separate database for each application or system you are using, eg. one for your phpBB Forum, and another for your WordPress Blog. This reduces the possibility of conflicting table names, and makes it easier to remove certain software from your website by simply deleting (or "dropping" in MySQL speak) the whole database.

Enter a descriptive name for your new database:

New Database

Step 3 - Create a new "database user"

In the next section, choose a username and a secure password. Note that the system may add your hosting username to the beginning, so keep it short.

Create User

Step 4 - Assign database privileges

A database can have different users that have varying privileges. Best practice is to give only the minimal permissions to a user/script that they need to function correctly, so that if there is a security breach those credentials can't be used to compromise the database. Here I have selected SELECT, INSERT, UPDATE, DELETE - these are the basic commands needed in most day-to-day operations. Being cautious like this also helps prevent bits of the database being deleted accidentally! After checking the correct boxes, click "Add User to Database".

Privileges

Step 5 - Try accessing the database with phpMyAdmin

phpMyAdmin is a great tool included in cPanel that lets you view and edit your database directly. Normally you'd use phpMyAdmin to set up your tables and check that everything is running smoothly. Click the link at the bottom of the screen, or the phpMyAdmin icon in the main cPanel menu (versions differ). You should see the following:

phpMyAdmin running smoothly

You can see that the hosthow_events table appears on the left with (0) by it - which means no tables have been created. You will now be able to use the username and password in any script or program that is using MySQL, and the host localhost in this case, indicated after the @ sign in the figure above.

Tags: , | Category: Control Panels, Databases, Server Security