In this article, we explain the basics of managing MariaDB using command-line (e.g. on VPSs running Ubuntu, Debian, or CentOS). Would you like to install MariaDB first? In our tutorials below we explain the installation and basic configuration for various OSs.
- Installing MariaDB in Ubuntu 16.04 or 18.04
- Installing MariaDB in Debian 9
- Installing MariaDB in CentOS 7
In order to manage MariaDB using command-line you make use of a SQL shell. The SQL shell is an interactive interface which is called upon using via command-line with the command:
mysql -u root -p
-u stands for user, root is the user with which your starting the SQL shell and -p ensures you're prompted for a password.
Commonly used commands
After starting the SQL shell you can create and edit databases and their content. Below follow several examples of commands for creating and editing databases.
-
Creating a database:
CREATE DATABASE yourdatabase;
-
Creating a user:
CREATE USER 'username'@'LOCALHOST' IDENTIFIED BY 'password';
-
Granting permissions/rights to a database:
Insert your awesome code here!
Replace 'yourdatabase' with the name of the desired database (or * for all databases) and 'username' with the username that you want to give access to.
-
Applying changes to permissions / creating new users
When you create a new user, or change permissions, these changes are only applied after using the following command:FLUSH PRIVILEGES;
-
Show the status of MariaDB:
STATUS;
-
Selecting a database:
USE yourdatabase;
-
Creating a table:
We explain the creation of a table based on an example:CREATE TABLE movielist (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(40), year INT);
- INT : stands for 'integer' and indicates that the corresponding column consists of numbers.
- NOT NULL : the column's value may not be empty. You're free to use this for each column (e.g. title VARCHAR(40) NOT NULL).
- AUTO_INCREMENT : After adding a new value to the table (e.g. a movie title and year), the value of the number that's added in the id-column is automatically increased by 1.
-
PRIMARY KEY : indicates that the ID column is a unique identifier for each record (i.e. each row) in the table.
-
Adding data to a table:
INSERT INTO movielist (id,
title,
year) VALUES (1, 'The Avengers: Endgame', 2019); INSERT INTO movielist (id,
title,
year) VALUES (2, 'Star Trek', 2009);
-
Showing data from a table:
SELECT * FROM movielist;
SELECT title FROM movielist WHERE id=2;
-
Removing databases:
DROP DATABASE yourdatabase;
-
Removing a table:
DROP TABLE yourtable
-
Removing data from a table:
When removing data, it's easies to do so per row. You remove the 2nd row in a table for example using:DELETE FROM yourtable WHERE id=2;
Would you like to remove multiple rows? The quickest method is to remove multiple rows with the same properties. Let's say that in our earlier example you'd like to remove all movies from 2009 from the movielist table, you'd use:DELETE FROM movielist WHERE year LIKE '2009';
There are of course more commands possible. Are there any you'd like us to explain in more detail? Let us know by leaving a message under 'Comments'.
Should you have any questions left regarding this article, do not hesitate to contact our support department. You can reach them via the ‘Contact Us’ button at the bottom of this page.