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-serverDebian
Debian ships MariaDB:
sudo apt update
sudo apt install mariadb-serverRHEL, Rocky Linux, AlmaLinux
sudo dnf install mysql-server # MySQL
# or
sudo dnf install mariadb-server # MariaDBIf 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.logManaging the service
The service name depends on the system:
| System | Service 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 logCheck that the server is listening:
sudo ss -tlnp | grep 3306Securing the installation
Run the security script once after installing:
sudo mysql_secure_installation # MySQL
sudo mariadb-secure-installation # MariaDBIt 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 mysqlCheck the version and leave again:
SELECT VERSION();
exitDo 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 shopImportant files and folders
| What | Typical 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
- MySQL on Linux: Installation and First Steps
- The mysql Command-Line Client
- MySQL Databases, Tables and Data Types
- MySQL Basics: INSERT, SELECT, UPDATE and DELETE
- MySQL Joins, Grouping and Subqueries
- MySQL Indexes and Query Performance
- MySQL Transactions and InnoDB
- MySQL Users, Privileges and Security
- MySQL Backup and Restore
- 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.