Before I can use a MySQL database in my scripts, I must know:
1) Where the database lives (or the database host)
2) The database name
3) The username and password for a user that is allowed to access that database
If my database is already setup, then I can look in the MySQL section in cpanel and find out all that information except for the password. When the password is created, it should be saved in a safe place because there isn’t a way to see it or change it from cpanel.
Note that the instructions I am giving below are how things work on my cpanel. There are different versions and different settings for cpanel, so unless you are hosting with me (and shouldn’t you be??), your hosting provider may have things setup differently. But, the basic concepts should be the same.
If my database hasn’t been setup yet, I can do that from the MySQL section in cpanel fairly quickly.
First, I need to create my database. Inside the MySQL section there is a text box for creating a new database. I just have to give the database a name and click on the submit button. My cpanel limits the name I choose to 16 characters. My cpanel will also prepend my website username and an underscore to the name I choose like this:
Once my database has been created I need to give a user access to my database. If I don’t have a MySQL user yet, I will need to created one. I can do this inside the same MySQL section of my cpanel. I choose a username and password and click submit. Cpanel has a password generator built in, but again, be sure to copy your password to a safe place.
Cpanel prepends my website username to the beginning of my database username. The database username that I chooose is limited to 7 characters, so my database username that I need to use in my scripts should look something like this:
Regardless of whether you just created a new user or had one already set up, you must give the user permission to access your new database. You can give the user permission to do anything he/she wants to do or you can limit access in several different ways. Cpanel has several different checkboxes to let you pick and choose what you want your user to do. I usually click on the ‘ALL PRIVILEGES’ at the top so that I can do everything from my scripts.
Of course, you could set up several different users for your database and give them different privileges if you want.
Now, all you have to do in your scripts is provide the host name (usually ‘localhost’ if your database is on the same server as your scripts), database name, user name and password to your database connection command.
Cpanel has a nice MySQL database wizard option that will walk you through this setup if you prefer.
Cpanel has also made a video tutorial to walk you through these steps. You can access the video from the MySQL section in your cpanel or you can see it here