Table of Contents
Do you need to configure a database for your WordPress installation? Maybe you already installed WordPress and now you need to create a new database with a user for your website to work.
In any case, here is all you need to know.
Create your WordPress database #
There are several ways to crate a database in your new server. The easiest one is probably to use a database visual admin like phpMyAdmin. Here is an official article from WordPress outlining how to do all of this with phpMyAdmin, Plesk, and others.
But in this article we are all about the command line.
It can happen (and will certainly happen to you some day if you do this as part of your job) that all you have to connect to a server is the terminal.
So here’s how to do it. First, we open the MySQL command line.
Open the MySQL command line interface #
To create a database, you will –of course– need to connect via SSH to your server, and open the MySQL command line with the following command:
$ sudo mysql
If that does not work, that means that you are probably already a root user, and you need to connect as the root. In this case, you will need your root password for your MySQL installation. If you don’t have this, you will need to contact your web hosting provider.
mysql --user=root --password
Create a new MySQL database #
Now that you are in the command line, you will see a prompt. Now you can create your database like this:
CREATE DATABASE database_name;
Important: Don’t forget to add the semicolons at the end of your command or it wont run.
Note down the name of the database because you will need it to configure WordPress later.
Create a user for that database #
The next thing you need is a user that will use this database. This will be the user that your WordPress installation will use to interact with the database and add the posts, comments, and all the information of your website. All of it will pass through the same MySQL user.
To create a new user in MySQL, you will need to run this command (again, don’t forget the semicolon at the end):
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
You can replace “username” with the name that you want to give your user. Also, be sure to make your password long and complex.
For more information on how to create a user in MySQL and how to configure it using the command line, you can check this amazing tutorial from DigitalOcean.
Give all privileges on that database to your user #
Once you have your database and your user, you need to allow your user to make changes in that database. That’s what we call give all privileges. You can do that with this command:
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
You need to replace database_name with your database name (the one you created above). Remember to add the dot and the asterisk
.*. That indicates that your user will have all privileges on the database and also all its tables.
Now you should have the following information:
MYSQL INFO user: username dabase: database_name password: password
Good! We are all set to configure our WordPress installation. Just replace these values in your wp-config.php file.