Creating a MySQL database and user via SSH

Popular database-driven PHP websites (ex. phpBB, Plogger, Gallery2, etc…) need access to a database in order to work.

When you install services such as the ones mentioned above, you are usually asked for at least these four things:

  • Database location
  • Database name
  • Database username
  • Database password

You need to configure your database before starting the installation process so that you can fill in this information. Here is how this is done.

Macintosh and Linux users, you can simply use your console for typing these commands. Windows users, you will need to download, install, and use a third-party application such as PuTTY to do this.

You need to create a database. To do this, follow these steps:

1. Log into your server via SSH

With Mac or Linux, you can directly enter

ssh admin@000.000.000.000

in your SSH client's window, replacing the numbers with your server's IP.

In PuTTY, you'll need to enter your server's IP address in the “Host Name” field and then click “Open.” A shell window will open with a “login as” field to fill in. You should login as admin; and then give the password associated with your server's admin account (no characters will be displayed when you type the password).

Gandi support doesn't have access to this password. If you have forgotten it, you'll need to create a new server.

2. Log into your MySQL database

Now, log into your MySQL database as root by typing:

mysql -p

Then, it will ask you for your password. This is the password that you entered for your database when you installed your MySQL database on your server.

3. Create the database

It is time to actually create the database you will be using for your website. To do this, first choose a good name (we'll use the name gallery as an example), and type:

create database gallery;

…and press enter. You will then see the following appear:

Query OK, 1 row affected (0.16 sec)

This is good, and confirms that the database has been added.

4. Create a database user and password

Now, create a username and a password for your new database. The password should be very secure.

Let's say that the username will be ryan and the password will be GanD1do. You will then type the following:

grant all privileges on gallery.* to 'ryan'@'localhost' identified by "GanD1do"; 

Where you replace:

  • gallery with the name of the database you just created,
  • ryan with the username you chose
  • GanD1do with the password you chose for your user

Then, type:

flush privileges;

and finally:

exit;

5. Exit your server

You may now exit your server by typing:

exit

Complete the installation of your web application

Congrats! You have now created a database on your server with a username and password, which is more than a lot of people do with their day.

Now, simply return to your installation process and enter the information that you now have. For example, we can now provide the following:

  • Database location: localhost
  • Database name: gallery
  • Database username: ryan
  • Database password: GanD1do

DO NOT USE the above example literally for creating your database! For security reasons, please choose a unique username and a good password. You are responsible for the security of your server.

Last modified: 04/19/2016 at 18:25 by Alexandre L. (Gandi)