Install MariaDB on Debian 12 Bookworm: Easy Database Management

Posted on

Install MariaDB on Debian 12 Bookworm: Easy Database Management

Install MariaDB on Debian 12 Bookworm: Easy Database Management

In this guide, we aim to walk you through the process of Install MariaDB on Debian 12 Bookworm. MariaDB is a robust and widely-used relational database management system (RDBMS). Debian 12 comes with the current stable version of MariaDB, specifically version 10.11. Follow the steps below to Install MariaDB on Debian 12 Bookworm for seamless database management.

Before you begin to Install MariaDB on Debian 12 Bookworm, it is crucial to log in to your server as a non-root user with sudo privileges. You can find a detailed guide on how to achieve this by consulting this article: Initial Server Setup with Debian 12 Bookworm.

Now, let’s dive into the steps required to Install MariaDB on Debian 12 Bookworm:

Step 1 – Install MariaDB from CLI on Debian 12

First, update your system’s package index. This ensures you’re working with the latest information about available packages. Use the following command:

sudo apt update

Next, use the following command to Install MariaDB on Debian 12 Bookworm:

sudo apt install mariadb-server -y

This command installs the MariaDB server package and all its dependencies. The -y flag automatically answers "yes" to any prompts during the installation, streamlining the process.

Upon successful installation, the MariaDB service should automatically start. To verify its status, use the following command:

sudo systemctl status mariadb

This command will display the current status of the MariaDB service. You should see output similar to the following:

**Output**
● mariadb.service - MariaDB 10.11.3 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; preset: enab>
     Active: **active** (**running**) since Thu 2023-06-15 08:27:21 EDT; 51s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 28617 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 15 (limit: 4653)
     Memory: 183.2M
        CPU: 818ms
     CGroup: /system.slice/mariadb.service
...

The output indicates that the MariaDB service is active and running. If the service is not running, you can start it manually using the command shown in the next step.

Step 2 – Manage MariaDB Service on Debian 12

You can manage your MariaDB service using systemctl. Here are some common commands:

To start the MariaDB server on Debian 12:

sudo systemctl start mariadb

To enable the MariaDB service to start automatically on boot:

sudo systemctl enable mariadb

To stop the MariaDB service:

sudo systemctl stop mariadb

To restart the MariaDB service:

sudo systemctl restart mariadb

These commands provide the fundamental control you need over your MariaDB server’s lifecycle.

Step 3 – Run MariaDB Security Script on Debian 12

For enhanced security, it’s highly recommended to run the mysql_secure_installation script. This script guides you through several important security configurations. Execute the script with the following command:

sudo mysql_secure_installation

The script will ask a series of questions. Respond as shown below, and make sure to choose a strong password for your MariaDB root user:

[Image of MariaDB Security Script on Debian 12, as provided in the original article]

The questions typically cover the following:

  • Setting a root password.
  • Removing anonymous users.
  • Disallowing remote root login.
  • Removing the test database.
  • Reloading privilege tables.

Answering "yes" to these questions generally improves the security of your MariaDB installation.

After running the security script, you can access the MariaDB shell using the following command:

sudo mariadb -u root -p

Enter the root password you set during the security script, and you should gain access to the MariaDB shell, as shown below:

Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 39
Server version: 10.11.3-MariaDB-1 Debian 12

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]>

Step 4 – Start To Use MariaDB Server

Now, let’s demonstrate some basic MariaDB usage by creating a database and a user.

First, create a new database. Replace orca_database with your desired database name:

**MariaDB [(none)]>** CREATE DATABASE orca_database;

Next, create a new user and grant them full privileges on the database you just created. Replace olivia_user with your desired username and password with a strong, secure password:

**MariaDB [(none)]>** GRANT ALL ON orca_database.* TO 'olivia_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

This command creates the user olivia_user, grants them all privileges on the orca_database database, and allows them to grant privileges to other users. The 'localhost' specifies that this user can only connect from the local machine.

To ensure that the new user and privileges are immediately available, flush the privileges:

**MariaDB [(none)]>** FLUSH PRIVILEGES;

Verify that the user has access to the orca_database database by listing the available databases:

**MariaDB [(none)]>** SHOW DATABASES;

The output should include your newly created database:

**Output**
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| orca_database      |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.003 sec)

Finally, exit the MariaDB shell:

**MariaDB [(none)]>** EXIT;

For more in-depth information, refer to the official MariaDB Documentation.

Conclusion

You have now successfully learned how to Install MariaDB on Debian 12 Bookworm. You’ve also learned how to manage the MariaDB service, secure your installation using the security script, and create a database and user.

Here are some related articles you might find interesting:

Alternative Solutions for Installing MariaDB on Debian 12

While the apt package manager is the standard and recommended method for installing MariaDB on Debian 12, here are two alternative approaches: using a Docker container and compiling from source.

1. Using Docker

Docker provides a containerization platform that allows you to run applications in isolated environments. This is useful for managing dependencies and ensuring consistency across different systems.

  • Explanation: Instead of installing MariaDB directly on your Debian 12 system, you can run it inside a Docker container. This isolates the database environment from the host system, preventing potential conflicts with other applications. Docker images are pre-configured and provide a consistent environment for MariaDB.

  • Steps:

    1. Install Docker: If you don’t have Docker installed, you can install it using the following commands:

      sudo apt update
      sudo apt install docker.io
      sudo systemctl start docker
      sudo systemctl enable docker
    2. Pull the MariaDB Docker image: Obtain the official MariaDB image from Docker Hub.

      sudo docker pull mariadb:10.11

      This command downloads the MariaDB 10.11 image. You can specify a different version if needed.

    3. Run the MariaDB container: Create and run a container based on the MariaDB image. Be sure to replace <your_root_password> with a strong password.

      sudo docker run --name mariadb-container -e MYSQL_ROOT_PASSWORD=<your_root_password> -d -p 3306:3306 mariadb:10.11
      • --name mariadb-container: Assigns a name to the container.
      • -e MYSQL_ROOT_PASSWORD=<your_root_password>: Sets the root password for the MariaDB instance.
      • -d: Runs the container in detached mode (in the background).
      • -p 3306:3306: Maps port 3306 on the host to port 3306 in the container, allowing you to connect to the database from the host.
    4. Connect to the MariaDB container: You can connect to the MariaDB server running inside the container using a MariaDB client. Install mariadb-client on your Debian host.

      sudo apt install mariadb-client

      Then, connect using the following command:

      mariadb -u root -p -h 127.0.0.1

      Enter the root password you set when running the container.

    5. Stop and Remove the Container (Optional):

      sudo docker stop mariadb-container
      sudo docker rm mariadb-container

      These commands stop and remove the Docker container respectively, freeing up system resources.

  • Advantages:

    • Isolation: MariaDB runs in a container, isolated from the host system.
    • Consistency: Ensures a consistent environment for MariaDB regardless of the host system.
    • Ease of deployment: Simplifies deployment and management of MariaDB instances.
  • Disadvantages:

    • Overhead: Docker containers introduce some overhead compared to native installations.
    • Complexity: Requires familiarity with Docker concepts.

2. Compiling from Source

Compiling MariaDB from source gives you the most control over the installation process, allowing you to customize build options and optimize for your specific hardware.

  • Explanation: This method involves downloading the source code of MariaDB, configuring the build process, and then compiling and installing the software. This is a more advanced approach but provides the greatest flexibility.

  • Steps:

    1. Install build dependencies: Before compiling, you need to install the necessary build tools and libraries.

      sudo apt update
      sudo apt install build-essential cmake libssl-dev libncurses-dev bison
    2. Download the source code: Download the MariaDB source code from the MariaDB website or a mirror. Choose the desired version (e.g., 10.11).

      wget https://downloads.mariadb.org/f/mariadb-10.11.3/source/mariadb-10.11.3.tar.gz
      tar -zxvf mariadb-10.11.3.tar.gz
      cd mariadb-10.11.3
    3. Configure the build: Use cmake to configure the build process.

      cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb -DMYSQL_DATADIR=/usr/local/mariadb/data -DSYSCONFDIR=/etc
      • -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb: Specifies the installation directory.
      • -DMYSQL_DATADIR=/usr/local/mariadb/data: Specifies the data directory.
      • -DSYSCONFDIR=/etc: Specifies the configuration directory.
    4. Compile and install: Compile the source code and install MariaDB.

      make
      sudo make install
    5. Configure MariaDB: After installation, you’ll need to configure MariaDB. This includes creating a configuration file (/etc/my.cnf) and setting up the data directory. You also need to manually start the MariaDB service. This process is more complex and requires a deeper understanding of MariaDB configuration.

  • Advantages:

    • Customization: Full control over build options and optimization.
    • Latest features: Potential to use the very latest features and patches.
  • Disadvantages:

    • Complexity: Requires advanced technical knowledge and manual configuration.
    • Time-consuming: Compilation can take a significant amount of time.
    • Maintenance: You are responsible for all updates and security patches.

Choose the installation method that best suits your needs and technical expertise. For most users, the apt package manager provides the simplest and most reliable way to install MariaDB on Debian 12 Bookworm.

Leave a Reply

Your email address will not be published. Required fields are marked *