Skip to content
VDS · Databases

MySQL and MariaDB: installation and your first database

Installing a database server on VDS, secure initial setup, creating a database and user, checking the connection, and common first errors.

Updated August 23, 2026

MySQL and its compatible branch MariaDB are what most PHP sites run on. Their commands and client are the same; the differences start in fine-tuning.

Installation

apt update && apt install -y mariadb-server      # Debian, Ubuntu
dnf install -y mariadb-server                    # AlmaLinux, CentOS Stream
systemctl enable --now mariadb

If you specifically need Oracle's MySQL, install mysql-server from its official repository. The service will be called mysql.

Close the basic holes right away:

mariadb-secure-installation

The script will ask you to set the root password, remove anonymous users, disallow remote root login, and drop the test database. Answer “yes” to everything.

Database and user

Don't run your application as root — create a separate user for each project:

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

The utf8mb4 encoding is mandatory: the old utf8 in MySQL stores only three bytes per character and breaks on emoji and some characters.

Check:

mariadb -u shop -p shop -e "SELECT 1;"

For more on permissions, see users and grants.

Initial setup

The settings live in /etc/mysql/mariadb.conf.d/50-server.cnf or /etc/my.cnf.d/. Here's the minimum you should fix right away:

[mysqld]
bind-address = 127.0.0.1
innodb_buffer_pool_size = 1G
max_connections = 100

bind-address = 127.0.0.1 keeps the database accessible only locally. If your application is on the same machine, that's how it should be. If you need the database from outside, read remote access before changing anything.

The buffer pool size is chosen by tier — see memory tuning.

After editing: systemctl restart mariadb.

Common errors

SymptomCauseWhat to do
Access denied for userWrong password or the user entry is bound to a different hostCheck SELECT user, host FROM mysql.user;
Can't connect through socketThe service isn't runningsystemctl status mariadb, check journalctl -u mariadb
Too many connectionsThe application doesn't close connectionsPool on the application side, then max_connections
Garbled textDifferent encodings for the database and connectionBring everything to utf8mb4
The database doesn't start after a rebootThe service isn't set to start on bootsystemctl enable mariadb

There are no automatic backups of the machine. Set up a dump right after your first working table — database backups. The NVMe disk is fast, but there's only one.