In this process, we will learn how to create a new database and database user for the MariaDB server. MariaDB can be used in conjunction with a wide variety of graphical tools (for example, the free MySQL Workbench), but in situations where you simply need to create a database, provide an associated user, and assign the correct permissions, it is often useful to perform this task from the command line. Known as the MariaDB shell, this simple interactive and text based-command line facility supports the full range of SQL commands and affords both local and remote access to your database server. The shell provides you with complete control over your database server, and for this reason, it represents the perfect tool for you to start your MariaDB work.
To Start With: What Do You Need?
To complete this process, you will require a working installation of the CentOS 7 operating system. It is expected that a MariaDB server is already installed and running on your server.
The Process
The MariaDB command-line tool supports executing commands in both the batch mode (reading from a file or standard input) and interactively (typing in statements and waiting for the results). We will use the latter in this process.
- To begin, log in on your CentOS 7 server with any system user you like and type the following command in order to access the MariaDB server using the MariaDB shell with the main MariaDB administration user called root (use the password created in the previous process):
mysql -u root -p - On successful login, you will be greeted with the MariaDB command-line interface. This feature is signified by the MariaDB shell prompt:
MariaDB [(none)]> - In this first step, we will create a new database. To do this, simply customize the following command by substituting an appropriate value for the new
value using:
CREATE DATABASECHARACTER SET utf8 COLLATE utf8_general_ci; Note
If this is your first introduction to the MariaDB shell, remember to end each line with a semi-colon (;) and press the Enter key after typing each command. - Having created our database, we will now create a MariaDB user. Each user will consist of a username and a password that is completely independent of the operating system’s user. For reasons of security, we will ensure that access to the database is restricted to localhost only. To proceed, simply customize the following command by changing the values
,
, and to reflect your needs:
GRANT ALL ON.* TO ' '@'localhost' IDENTIFIED BY '
' WITH GRANT OPTION; - Next, make the MariaDB DBMS aware of your new user:
FLUSH PRIVILEGES; - Now simply type the following command to exit the MariaDB shell:
EXIT; - Finally, you can test the accessibility of your new
by accessing the MariaDB shell from the command-line in the following way:
mysql -u-p - Now back at the MariaDB shell (MariaDB [(none)]>), type the following commands:
SHOW DATABASES;
EXIT;
How Does It Work?
During the course of this process, you were shown not only how to create a database, but also how to create a database user.
So what did we learn from this experience?
We started the process by accessing the MariaDB shell as the root user with the mysql command. By doing this, we were then able to create a database with a simple SQL function called CREATE DATABASE, providing a custom name for the
DROP DATABASE IF EXISTS
Having done this, it is simply a matter of adding a new database user with the appropriate permissions by running our GRANT ALL command. Here we provided
GRANT [type of permission] ON