How To Connect Mariadb In Ubuntu

In this tutorial, you will learn how to install, secure, and start using MariaDB on Ubuntu. MariaDB is a popular database server often used as an alternative to MySQL due to its compatibility, speed, and open-source nature.

Here are the step-by-step instructions on how to connect MariaDB in Ubuntu:

Step 1: Update the system repositories

Before installing MariaDB, it’s essential to update your system’s package index to ensure that you’re installing the latest version.

To update your system repositories, run the following command:

Step 2: Install MariaDB

To install MariaDB, you can use the following command:

The package manager will handle the installation process and configure the default settings for you.

Once MariaDB is installed, you can check its status with the following command:

Step 3: Secure the MariaDB installation

By default, MariaDB installation comes with some insecure settings like a blank password for the root user. To secure the installation, run the following command:

This script will ask you to change the root password, remove anonymous users, disable remote root login, and remove the test database. It’s recommended to answer “yes” to all these questions to improve the security of your MariaDB installation.

Step 4: Connect to MariaDB

You can connect to the MariaDB server using the mysql command-line client. To do this, run the following command as root:

Enter your MariaDB root user password when prompted. If the connection is successful, you will see a MySQL command prompt:

MariaDB [(none)]>

Step 5: Create a new database and user

To create a new database, run the following command, replacing “my_database” with the desired name for your new database:

Next, you’ll want to create a new user and grant them privileges to manage the new database. Replace “my_user” with the desired username and “my_password” with the desired password:

Don’t forget to reload the privilege tables after creating the new user and assigning them the appropriate privileges:

Step 6: Test the new user connection

To test the newly created user and their connection to the database, exit the current MySQL shell:

Now, connect to MariaDB using the new user:

Enter the password for the new user when prompted. If the connection is successful, you’ll see the MySQL command prompt:

MariaDB [(none)]>

Full code

Conclusion

Now, you’ve successfully connected to MariaDB in Ubuntu, created a new database, and set up a new user with appropriate privileges. You can start working with your databases and make use of the many features that MariaDB has to offer. Remember to follow best security practices and keep your system and packages up-to-date to ensure optimal security and performance.