Fatal error cannot open and lock privilege tables

APPLIES TO:

MariaDB - Community Server 10.6

SYMPTOMS

The custom system variables and options:

[mariadb]
datadir = /data/mysql
socket = /data/mysql/mysql.sock
log_error = mariadb-test.err
innodb_buffer_pool_size = 1G

Starting mariadb.service fails with privilege tables no exist.

"/data/mariadb-test.err"
2024-03-04 23:37:09 0 [ERROR] Could not open mysql.plugin table: "Table 'mysql.plugin' doesn't exist". Some plugins may be not loaded
2024-03-04 23:37:09 0 [Note] InnoDB: Buffer pool(s) load completed at 240304 23:37:09
2024-03-04 23:37:09 0 [ERROR] Can't open and lock privilege tables: Table 'mysql.servers' doesn't exist
2024-03-04 23:37:09 0 [Note] Server socket created on IP: '0.0.0.0'.
2024-03-04 23:37:09 0 [Note] Server socket created on IP: '::'.
2024-03-04 23:37:09 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.db' doesn't exist
2024-03-04 23:37:09 0 [ERROR] Aborting

CAUSE

The Mariadb datadir path is /var/lib/mysql as default. After change datadir, must copy or install system tables to new path.

SOLUTION

sudo mariadb-install-db --defaults-file=/etc/my.cnf --datadir=/data/mysql --user=mysql

sudo systemctl start mariadb.service