Site icon Linux Genie

How to Install MariaDB on Ubuntu 24.04

Install mariadb on ubuntu

MariaDB is a well-known and open-source alternative to MySQL and offers features like ACID compliance, scalability, and high performance. By installing MariaDB, users can create databases, manage users, and run SQL queries efficiently.

The objective of installing MariaDB on Ubuntu 24.04 is to set up a robust and reliable relational database management system (RDBMS) that can handle data storage, retrieval, and management for various applications.

The content of this article is mentioned below:

Let’s start the article with the installation.

How to Install MariaDB on Ubuntu 24.04?

Whether you’re building web applications, content management systems, or any other software that requires a database, MariaDB provides a solid foundation for your data needs.

Let’s begin with the easy one.

Method 1: Install MariaDB on Ubuntu 24.04 Using the Ubuntu Default Repository

To install MariaDB on Ubuntu 24.04 using the Ubuntu default repository, follow the detailed steps:

Step 1: Update Package Index

First, users need to update the system repository to refresh the package index using the apt command:

sudo apt update

Step 2: Install mariadb-server Package

Now, use the apt command for installing the mariadb-server package using the sudo privileges:

sudo apt install mariadb-server

Step 3: Secure MariaDB Installation

After installation, execute the mysql_secure_installation script for restricting permission to the server and enhance security. This script guides users via several prompts to configure the MariaDB installation’s security options:

sudo mysql_secure_installation

During the script execution, you’ll be prompted to set a current database password for the root user. If users haven’t set one yet, simply press ENTER to indicate “none.” The script will guide you through other security options as well:

Step 4: Test/Verify MariaDB Installation

For verification, log in to the MariaDB shell as the root user and iput the root password:

sudo mysql -u root -p

Optional: Upgrade MariaDB on Ubuntu 24.04

To upgrade/install MariaDB’s latest version on Ubuntu 24.04, update system packages, add the MariaDB APT repository, and install MariaDB 11.0 server/client. After that, secure MariaDB installation and test MariaDB installation. Or users can also use the “mariadb-upgrade” command for upgrading:

sudo mariadb-upgrade

That is from method 1.

Method 2: Install MariaDB on Ubuntu 24.04 Using the MariaDB Repository

For installing the latest version of MariaDB on Ubuntu 24.04, use the official MariaDB repository. To install MariaDB on Ubuntu 24.04 using the MariaDB repository, follow these steps:

Step 1: Update Package list

Update the system packages list to ensure you are getting the latest version available:

sudo apt update

Step 2: Install Software Properties

Now, users are required to install the dependent package. It is important for adding the new repositories:

sudo apt install software-properties-common

Step 3: Import the MariaDB GPG Key

To verify the integrity of the repository, import the MariaDB GPG Key using the “apt-key” command:

sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'

Step 4: Add the MariaDB Repository

Now, add the MariaDB repository to the system. For this, replace “11.2.3” with the version you wish to install:

sudo add-apt-repository 'deb [arch=amd64] http://mariadb.mirror.globo.tech/repo/11.2.3/ubuntu jammy main'

Note: Users can replace “jammy” in the repository line if the Ubuntu version has a different codename. Or replace $(lsb_release -cs) with the Ubuntu version codename if it’s not automatically detected.

Step 5: Update Packages List

Now, update the packages indexing for installing the MariaDB from the added repository:

sudo apt update

Step 6: Install MariaDB Server

Now users can install MariaDB server and client packages from the official MariaDB repository:

sudo apt install mariadb-server mariadb-client

Step 7: Secure MariaDB Installation (optional but recommended)

In this step, execute the script to set a root password. The script will guide users via other security options as well:

sudo mysql_secure_installation

Step 8: Verify Installation

Now, check that MariaDB is active and running. For this, check MariaDB service status:

sudo systemctl status mariadb

Or users can access the MariaDB with the help of the mysql command as below:

sudo mysql -u root -p

That is all from this method for the latest MariaDB version.

How to Configure/Use MariaDB on Ubuntu 24.04?

To use/configure MariaDB on Ubuntu 24.04, first install it, then follow these steps:

Step 1: Access MariaDB

First, log in to the MariaDB shell as the root user on the Ubuntu server. Type the root password users set during installation:

sudo mysql -u root -p

Step 2: Create a New Database

For creating the new database, use the “CREATE DATABASE database_name” statement. Let’s create a database (e.g., “mydb”), as below:

CREATE DATABASE mydb;

Step 3: Create a User and Grant Permissions

Use the “CREATE USER user_name” statement to create a new user. Let’s create a new user (e.g., “linux_user”), and set a password “1212”:

CREATE USER 'linux_user'@'localhost' IDENTIFIED BY '1212';

Step 4: Grant Privileges to the User for the Database

Now, users need to grant privileges to the created user “linux_user” for the database “mydb”:

GRANT ALL PRIVILEGES ON mydb.* TO 'linux_user'@'localhost';

Step 5: Create a Table in MariaDB

To create a table on MariaDB, use the CREATE TABLE statement by specifying the table name. Before it, users must change the database via the “USE database_name”. Let’s creates a table linux_table with two columns, id, and data:

USE mydb
CREATE TABLE linux_table (
id INT AUTO_INCREMENT PRIMARY KEY,
data VARCHAR(255)
);

To display the created table, users can use the DESCRIBE statement to show the table structure. It enlists users in the columns, types, and other information about the table structure:

DESCRIBE linux_table;

Step 6: Apply Changes

To apply the changes that are performed above, use the “FLUSH PRIVILEGES” statement as below:

FLUSH PRIVILEGES;

Exit the MariaDB Shell

Finally, users can exit the MariaDB session by typing the exit key or press Ctrl+D:

That is all from the configuration.

How to Uninstall/Remove MariaDB on Ubuntu 24.04?

To uninstall/remove MariaDB on Ubuntu 24.04, users can execute the “autoremove” command:

sudo apt autoremove mariadb-server mariadb-client --purge -y # For Removing Packages
sudo add-apt-repository 'deb [arch=amd64] http://mariadb.mirror.globo.tech/repo/11.2.3/ubuntu jammy main' # For Removing Repository

This command will remove both the server and client components of MariaDB.

FAQ

How to Create a New Database and Insert a Table in MariaDB Using SQL Commands?

To create a new database, use the “CREATE DATABASE database_name”;. For this, replace database_name with the name that users want for their database.

How to Start Using MariaDB on Ubuntu?

To start MariaDB on Ubuntu, use the “mysql -u myuser -p” command. In this way, users can now create tables, insert data, and run SQL queries within your database.

How to Create a New Table Within the Database?

To create a new table within that database, first select the database using the “USE database_name;” command. Then, create a table with the desired structure:

CREATE TABLE table_name (
col1 datatype,
col2 datatype,
col3 datatype,
...
);

Replace table_name with the name users need/want for their table, and define the columns with their respective data types. Replace datatype with the specific data type you need for each column, like INT, VARCHAR, DATE, etc.

Conclusion

To install MariaDB on Ubuntu 24.04, update the package index and install the mariadb-server package using the “sudo apt install mariadb-server” command. After installation, run the mysql_secure_installation script to enhance security as well as configure MariaDB. For installing the latest MariaDB version on Ubuntu 24.04, add the MariaDB repository and install the mariadb-server and mariadb-client packages. To connect/start the MariaDB as the new user, use the “mysql -u myuser -p” command. Then, enter the user’s password. In this way, users can create tables, insert data, and run SQL queries within your database.

Exit mobile version