Log in Sign up

MySQL or MariaDB?

MySQL is developed by Oracle. MariaDB is a community fork that started from MySQL 5.5 and is still largely compatible: the same SQL, the same port (3306), the same client commands. Many Linux distributions (Debian, Fedora, Arch) ship MariaDB as their default "MySQL". For everyday work – and for all articles in this category – the two behave the same; differences are pointed out where they matter.

Installation

Ubuntu

sudo apt update
sudo apt install mysql-server

Debian

Debian ships MariaDB:

sudo apt update
sudo apt install mariadb-server

RHEL, Rocky Linux, AlmaLinux

sudo dnf install mysql-server        # MySQL
# or
sudo dnf install mariadb-server      # MariaDB

If you installed from Oracle's own MySQL repository instead, a temporary root password is written to the log:

sudo grep 'temporary password' /var/log/mysqld.log

Managing the service

The service name depends on the system:

SystemService name
Ubuntu / Debian (MySQL)mysql
RHEL family (MySQL)mysqld
MariaDB (all)mariadb (often also mysql as alias)
sudo systemctl start mysql       # start
sudo systemctl stop mysql        # stop
sudo systemctl restart mysql     # restart after config changes
sudo systemctl enable mysql      # start at boot
systemctl status mysql           # is it running?
journalctl -u mysql -e           # recent service log

Check that the server is listening:

sudo ss -tlnp | grep 3306

Securing the installation

Run the security script once after installing:

sudo mysql_secure_installation        # MySQL
sudo mariadb-secure-installation      # MariaDB

It lets you set a password policy, remove anonymous users, forbid remote root login, and drop the test database. Answer yes to all of these on a server.

First login

On Ubuntu, Debian and MariaDB the root database user is authenticated through the Linux user (auth_socket / unix_socket). So you log in with sudo and no password:

sudo mysql

Check the version and leave again:

SELECT VERSION();
exit

Do not use root for applications. Create a separate user and database instead (see MySQL Users, Privileges and Security):

CREATE DATABASE shop CHARACTER SET utf8mb4;
CREATE USER 'shop'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT ALL PRIVILEGES ON shop.* TO 'shop'@'localhost';

Then log in as that user:

mysql -u shop -p shop

Important files and folders

WhatTypical location
Config (Debian/Ubuntu MySQL)/etc/mysql/mysql.conf.d/mysqld.cnf
Config (Debian MariaDB)/etc/mysql/mariadb.conf.d/50-server.cnf
Config (RHEL family)/etc/my.cnf and /etc/my.cnf.d/
Data directory/var/lib/mysql
Socket/var/run/mysqld/mysqld.sock (Debian/Ubuntu), /var/lib/mysql/mysql.sock (RHEL)
Error log/var/log/mysql/error.log or /var/log/mysqld.log, or the journal

Never edit or copy files in /var/lib/mysql by hand while the server runs – use the tools in MySQL Backup and Restore.

The MySQL series in this wiki

  1. MySQL on Linux: Installation and First Steps
  2. The mysql Command-Line Client
  3. MySQL Databases, Tables and Data Types
  4. MySQL Basics: INSERT, SELECT, UPDATE and DELETE
  5. MySQL Joins, Grouping and Subqueries
  6. MySQL Indexes and Query Performance
  7. MySQL Transactions and InnoDB
  8. MySQL Users, Privileges and Security
  9. MySQL Backup and Restore
  10. MySQL Configuration, Logs and Troubleshooting

See also Linux & Docker Command Cheat Sheet and, for using MySQL from PHP, PHP Databases with PDO.

Put together with AI as a quick reference I keep for my own work. Entries like this are usually not reviewed in detail by a person.