Every now and then I need to create a MySQL or MariaDB database from the command line using the MySQL Client. And I tend to forget exactly what commands to run. Lots of things to remember as a web developer. That is why I created loads of gists at Github. Unfortunately their search is not that great and it is often hard to find stuff as you cannot write solid descriptions like you would do on a blog.
So here let me tell you what to do when you need to quickly create the database for a WordPress site from the command line. In my case I have Laravel Valet up and running on OSX with PHP7 and MariaDB running based on Homebrew. So all I need is to log into the MySQL Client and add the database. So how to proceed.
- open the terminal
- run the command mysql -u root -p
- enter your password
That should show you the MySQL client prompt demonstrating you logged on. Then you need to run the following commands – with adjustments for the names and passwords:
CREATE DATABASE databasename;
And then you add privileges for a user:
GRANT ALL PRIVILEGES ON databasename.* TO "wordpressusername"@"hostname" IDENTIFIED BY "password";
Make sure you change the
- database name to the one you picked,
- choose a database user and
- choose a password.
And then you flush all privileges:
So you would see something like this:
mysql> CREATE DATABASE databasename; Query OK, 1 row affected (0.00 sec) mysql> GRANT ALL PRIVILEGES ON databasename.* TO "wordpressusername"@"hostname" IDENTIFIED BY "password"; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec) mysql> EXIT Bye
As you can see not really hard to do, but nice to have handy to quickly modify, copy and paste is it not?
NB Do not forget to note down your used credentials for adding to wp-config.php
If you would like to check if the database is there run:
Your database with your name should be there.
Question? Leave a comment here below!