How to Create MySQL Database and User on Command Line

Create a MySQL Database and Database User via cat /etc/psa/.psa.shadow on Linux (CentOS, Debian, Ubuntu) with Termius Command Line App.

Jul 3, 2023 - 22:04
 0
How to Create MySQL Database and User on Command Line

Summary this Article:

In this article, you can find information on how to connect to your CentOS/Ubuntu/Debian VPS or VDS with Linux operating system via SSH, how to log in to your MySQL as an administrator, and how to create a MySQL database and user.

-

Let's start our article by recommending the Termius software for connecting to the SSH command panel. You can download the Termius from here.

Open the Termius

MacOS/Windows Termius SSH Installation

1- First, click on the Create Host button and enter your server's SSH Connection details in the opened right window. Then, create your connection by clicking on the "->|" icon at the top right.
2- Start your SSH Connection by double-clicking on your server that appears on the Termius main screen.

Termius SSH Connection Screen

After double-clicking on your established server connection, the visual on your screen should appear as shown below

SSH Connection with Termius is Success

How to Connect to MySQL as Admin Rights via SSH Terminal?

To connect to MySQL with Admin privileges from your Linux SSH panel, type the following line in your Termius command line and press Enter.

mysql -uadmin -p`cat /etc/psa/.psa.shadow`

You should encounter the message "Welcome to the MariaDB Monitor" as shown in the screenshot below.

How to Create a MySQL Database via SSH Command Line on Linux? (CentOS, Debian, Ubuntu)

Now that we have successfully logged into MySQL as an administrator, we will enter the necessary command to create a new database. Replace "yourDatabaseName" in the command below with the name of the database you want to create, enter it in Termius, and press Enter.

create database yourDatabaseName;

If you see the "Query OK" message as shown in the screenshot below, you have successfully created your database.

How to Create a MySQL Database via SSH Command Line on Linux? (CentOS, Debian, Ubuntu)

How to Create a MySQL Database User via SSH Command Line on Linux?

As seen in the screenshot provided, we have created a new database named "mersevBilisimBlog." Now let's create a database user with full privileges on this database. In your Termius command panel, enter the following command, replacing "databaseUserName" with the desired username for the database user and "databaseUserPassword" with the desired password for the database user. Then press Enter.

CREATE USER 'databaseUserName'@'localhost' IDENTIFIED BY 'databaseUserPassword';

If you see the "Query OK" message as shown in the screenshot below, you have successfully created your database user as well.

How to Grant Full Privileges to Your MySQL Database User?

GRANT ALL PRIVILEGES ON yourDatabaseName.* TO 'databaseUserName'@'localhost';

By using this command, you have granted all privileges to the user you created on your database. Finally, to ensure that all these operations take effect, enter the following command in your Termius panel and press Enter.

FLUSH PRIVILEGES;

You have now completed all the steps. If you wish, you can use the following command to list all the databases in MySQL running on your server and verify the creation of your database as well.

SHOW DATABASES;

MySQL List All Databases on SSH Terminal

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow