Controlling the MySQL or MariaDB service is the fastest way to recover from outages, apply configuration changes, and schedule maintenance without guessing whether the database daemon is actually running.

On most modern Linux systems, the database server runs as the mysqld daemon under systemd, which exposes a unit such as mysql.service, mariadb.service, or mysqld.service. The systemctl command is the primary interface for starting, stopping, restarting, and checking health, with status output also showing recent log lines from the unit.

Service names and unit aliases vary by distribution and package, so confirming the exact unit name prevents “Unit not found” errors. Restarting the service terminates existing database connections, and disabling a unit only changes boot-time behavior without stopping a currently running daemon, so maintenance windows and application impact still matter.

Steps to manage MySQL/MariaDB database in Linux:

  1. Identify the installed database service unit name.
    $ systemctl list-unit-files --type=service | grep --extended-regexp '^(mysql|mariadb|mysqld)\.service'
    mysql.service                              enabled
    mariadb.service                            disabled

    Use the unit name shown on the left in later commands.

  2. Check the status of the database service.
    $ sudo systemctl status mysql
    ● mysql.service - MySQL Community Server
         Loaded: loaded (/lib/systemd/system/mysql.service; enabled; preset: enabled)
         Active: active (running) since Tue 2023-02-07 08:39:17 +08; 37min ago
        Process: 866 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
       Main PID: 1068 (mysqld)
         Status: "Server is operational"
          Tasks: 38 (limit: 2225)
         Memory: 360.7M
            CPU: 4.988s
         CGroup: /system.slice/mysql.service
                 └─1068 /usr/sbin/mysqld
    
    Feb 07 08:39:16 host systemd[1]: Starting MySQL Community Server...
    Feb 07 08:39:17 host systemd[1]: Started MySQL Community Server.

    Replace mysql with mariadb or mysqld when that is the unit name on the host.

    Some MariaDB packages expose mysql.service as a compatibility alias.

  3. Stop the database service.
    $ sudo systemctl stop mysql

    Stopping the service drops active connections and can break applications until the service starts again.

  4. Start the database service.
    $ sudo systemctl start mysql
  5. Restart the database service.
    $ sudo systemctl restart mysql

    Restarting interrupts active sessions and can cause transient errors in clients during the restart window.

  6. Disable the database service from starting on system boot.
    $ sudo systemctl disable mysql
    Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install.
    Executing: /lib/systemd/systemd-sysv-install disable mysql
    Removed "/etc/systemd/system/multi-user.target.wants/mysql.service".

    disable changes boot behavior only, so a running service remains running until a separate stop.

    Replace mysql with mysqld for RedHat-based platforms.

  7. Enable the database service to start on system boot.
    $ sudo systemctl enable mysql
    Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install.
    Executing: /lib/systemd/systemd-sysv-install enable mysql
    Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
  8. View recent unit logs when the service shows failed or exits unexpectedly.
    $ sudo journalctl --unit=mysql.service --no-pager --lines=50
    Feb 07 08:39:16 host systemd[1]: Starting MySQL Community Server...
    Feb 07 08:39:17 host systemd[1]: Started MySQL Community Server.
    ##### snipped #####

    Replace mysql.service with the unit discovered earlier, such as mariadb.service or mysqld.service.

  9. Verify the service is currently running.
    $ systemctl is-active mysql
    active
  10. Verify the service start-on-boot setting.
    $ systemctl is-enabled mysql
    enabled
Discuss the article:

Comment anonymously. Login not required.