
Fabian Tech Tips

By following these steps, you can manage and maintain your MariaDB instance efficiently. From basic setup to advanced configurations, this guide equips you with the necessary tools and commands.
Oct 13, 2024
6 min read
0
2
0
By following these steps, you can manage and maintain your MariaDB instance efficiently. From basic setup to advanced configurations, this guide equips you with the necessary tools and commands to ensure your database runs smoothly and securely.
Install MariaDB
Install MariaDB Server:
yum -y install MariaDB-server
Start MariaDB Service:
systemctl start mariadb
Secure Installation:
mysql_secure_installation
Enable General Log:
SQL
SET GLOBAL general_log = 'ON';
Check Current Version:
SQL
SELECT VERSION();
Create Database:
SQL
CREATE DATABASE sales;
Task base guide for basic DBA tasks
MariaDB DBA task-based user guide
Install MariaDB
Enable login
Check version of MariaDB
Create database
Creat backup
Show binary logs
Flush log files
Configure swap file
Database performance testing
Setup replication
Advanced binary log point in time recovery.
Patch database
Install Password Plugin:
SQL
INSTALL SONAME 'simple_password_check';
Create Backup:
mariadb-dump -u root -p sales > sales_backup.sql
Create Backup:
mariabackup --backup --target-dir=/data/backup/
Prepare Backup:
mariabackup --prepare --target-dir=/data/backup/
Managing Binary Logs
SHOW BINARY LOGS
The SHOW BINARY LOGS statement lists all the binary log files on the server. This command is useful for identifying the log files that are currently being used and those that can be purged. It requires the SUPER, REPLICATION_CLIENT, or BINLOG MONITOR privilege (from MariaDB 10.5.2 onwards).
Syntax:
SHOW BINARY LOGS;
Example Output:
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 19039 |
| mariadb-bin.000002 | 717389 |
| mariadb-bin.000003 | 300 |
| mariadb-bin.000004 | 333 |
| mariadb-bin.000005 | 899 |
| mariadb-bin.000006 | 125 |
| mariadb-bin.000007 | 18907 |
| mariadb-bin.000008 | 19530 |
| mariadb-bin.000009 | 151 |
| mariadb-bin.000010 | 151 |
| mariadb-bin.000011 | 125 |
| mariadb-bin.000012 | 151 |
| mariadb-bin.000013 | 151 |
| mariadb-bin.000014 | 125 |
| mariadb-bin.000015 | 151 |
| mariadb-bin.000016 | 314 |
+--------------------+-----------+
This output shows the names and sizes of the binary log files1.
FLUSH LOGS
The FLUSH LOGS statement closes and reopens all the log files. This is particularly useful when you want to start a new binary log file without stopping the server. It ensures that the current binary log file is closed and a new one is created.
Syntax:
FLUSH LOGS;
Procedure Example:
Show Current Binary Logs:
SHOW BINARY LOGS;
This command lists all the current binary log files.
Flush Logs:
FLUSH LOGS;
This command closes the current binary log file and opens a new one.
Show Updated Binary Logs:
SHOW BINARY LOGS;
This command lists the binary log files again, showing the new log file created after flushing2.
By following these steps, you can manage and maintain your MariaDB binary logs efficiently.
1: MariaDB Knowledge Base - SHOW BINARY LOGS 2: MariaDB Knowledge Base - FLUSH LOGS
System and Configuration Management
Memory and Swap Management
Check Memory Usage:
free -h
This command displays the amount of free and used memory in the system in a human-readable format.
Check Current Swappiness Value:
cat /proc/sys/vm/swappiness
This command shows the current swappiness value, which controls the tendency of the kernel to move processes out of physical memory and onto the swap disk.
Set Swappiness Value Temporarily:
echo 1 > /proc/sys/vm/swappiness
This command sets the swappiness value to 1 temporarily.
Set Swappiness Value Permanently:
nano /etc/sysctl.conf
Add the following line to the file:
vm.swappiness=1
Disable Swap Temporarily:
swapoff -a
Enable Swap Again:
swapon -a
MariaDB Configuration
Edit MariaDB Configuration:
nano /etc/my.cnf.d/server.cnf
Add or modify the following settings under the [mariadb] section:
[mariadb]
key_buffer_size=8M
table_open_cache=1200
table_definition_cache=512
sort_buffer_size=16M
Restart MariaDB Service:
systemctl restart mariadb
Check MariaDB Service Status:
journalctl -u mariadb
or
systemctl status mariadb.service -l
Performance Testing with mariadb-slap
Basic Load Test:
mariadb-slap -a
Load Test with Iterations:
mariadb-slap -a -i 10
Load Test with Specific Engines:
mariadb-slap -a -i 10 -e InnoDB,MyISAM
Load Test with Concurrency:
mariadb-slap -a -i 10 -e InnoDB,MyISAM -c 4
Advanced Load Test:
mariadb-slap --delimiter=";" --create-schema=slappy --create="CREATE TABLE t (a int);INSERT INTO t VALUES (5)" --query="SELECT * FROM t" --concurrency=100 --iterations=150
Save Load Test Results:
mariadb-slap --delimiter=";" --create-schema=slappy --create="CREATE TABLE t (a int);INSERT INTO t VALUES (5)" --query="SELECT * FROM t" --concurrency=100 --iterations=150 > /tmp/load_test.txt
By following these steps, you can effectively manage system memory, configure MariaDB settings, and perform load testing to ensure optimal performance.
Setting Up the Master Server
Edit Configuration File:
nano /etc/my.cnf.d/server.cnf
Add the following lines:
[mariadb]
server_id=1
log_basename=master1
Grant Replication Privileges:
GRANT REPLICATION SLAVE ON . TO repuser@'%' IDENTIFIED BY 'aBcd123%';
SELECT * FROM mysql.user WHERE user='repuser'\G
Restart MariaDB:
exit
systemctl restart mariadb
Check MariaDB Status:
journalctl -u mariadb
# or
systemctl status mariadb.service -l
Get Master Status:
SHOW MASTER STATUS;
exit
Dump Databases:
mariadb-dump --master-data=2 --all-databases --single-transaction > /root/fulldump.sql
Setting Up the Slave Server
Transfer Dump File:
ip a
# Assuming IP is 192.168.0.147
scp /root/fulldump.sql root@192.168.0.147:~/fulldump.sql
# or
rsync -av --progress /root/fulldump.sql centos@192.168.0.147:~/fulldump.sql
Prepare Log Directory:
mkdir /var/log/mariadb
chown -R mysql:mysql /var/log/mariadb
Edit Configuration File:
nano /etc/my.cnf.d/server.cnf
Add the following lines:
[mariadb]
read_only
server_id=2
log_basename=slave1
log_bin=/var/log/mariadb/slave1-bin
log-bin-index=/var/log/mariadb/slave1-bin.index
Restart MariaDB:
systemctl restart mariadb
Import Dump File:
ls -l
mariadb < fulldump.sql
Verify Databases:
show databases;
exit
Set GTID Position:
SET GLOBAL gtid_slave_pos = '0-1-1';
Configure Slave:
CHANGE MASTER TO
MASTER_HOST='192.168.0.146',
MASTER_USER='repuser',
MASTER_PASSWORD='aBcd123%',
MASTER_USE_GTID = slave_pos;
Start Slave:
START SLAVE;
SHOW SLAVE STATUS\G
Verification and Testing
Create Database on Master:
CREATE DATABASE mordor;
show databases;
Check Databases on Slave:
show databases;
Check Process List:
SHOW PROCESSLIST;
Verify Slave Status:
SHOW SLAVE STATUS\G
Managing Binary Logs Advanced
Show Binary Logs:
SHOW BINARY LOGS;
Flush Logs:
FLUSH LOGS;
Verify Binary Logs:
SHOW BINARY LOGS;
Database Operations
Select Data:
SELECT Name, Population FROM City WHERE ID=456;
Drop Table:
DROP TABLE City;
Update Data:
UPDATE Country SET Population=82000000 WHERE Code='DEU';
Verify Update:
SELECT Name, Population FROM City WHERE ID=456;
Log Analysis and Recovery
View Dump File:
cat /root/dump.sql | less
Stop MariaDB:
systemctl stop mariadb
Navigate to Log Directory:
cd /var/log/mariadb/
Search for DROP Statements:
mariadb-binlog master1-bin.000002 | grep -i DROP
mariadb-binlog master1-bin.000003 | grep -i DROP
mariadb-binlog master1-bin.000004 | grep -i DROP
mariadb-binlog master1-bin.000004 | grep -B 12 -A 4 -i DROP
Recovery Steps:
nano RECOVER
Add the following steps:
1. Recover from backup
2. Replay master1-bin.000001 from position 330
3. Stop replaying master1-bin.000004 at position 431
4. Start replaying master1-bin.000004 from position 551
Edit Configuration File:
nano /etc/my.cnf.d/server.cnf
Add the following line:
[mariadb]
read-only
Start MariaDB:
systemctl start mariadb
Check MariaDB Status:
journalctl -u mariadb
# or
systemctl status mariadb.service -l
Import Dump File:
mariadb < /root/dump.sql
Replay Binary Logs:
mariadb-binlog --start-position=330 /var/log/mariadb/master1-bin.000001 | mariadb
mariadb-binlog --stop-position=431 /var/log/mariadb/master1-bin.000004 | mariadb
mariadb-binlog --start-position=551 /var/log/mariadb/master1-bin.000004 | mariadb
Verification and Testing
Select Data from City:
SELECT Name, Population FROM City WHERE ID=456;
Select Data from Country:
SELECT Name, Population FROM Country WHERE Code='DEU';
Edit Configuration File:
nano /etc/my.cnf.d/server.cnf
# or
vi /etc/my.cnf.d/server.cnf
Comment out the read-only line:
[mariadb]
# read-only
Restart MariaDB:
systemctl restart mariadb
Check MariaDB Status:
journalctl -u mariadb
# or
systemctl status mariadb.service -l
Patching MariaDB Database on Linux
Prerequisites
Root or sudo access to the Linux server.
Backup of the current MariaDB database.
MariaDB repository configured on your system.
Step-by-Step Guide
Update System Packages
sudo apt update && sudo apt upgrade -y
Backup MariaDB Database
mysqldump -u root -p --all-databases > all_databases_backup.sql
Stop MariaDB Service
sudo systemctl stop mariadb
Install MariaDB Patch
Download the patch from the official MariaDB repository.
Apply the patch:
sudo apt install mariadb-server
Restart MariaDB Service
sudo systemctl start mariadb
Verify MariaDB Version
mysql -V
Create a New User
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
Grant Privileges to the New User
GRANT ALL PRIVILEGES ON . TO 'newuser'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Test the New User
mysql -u newuser -p
Additional Tips
Regular Backups: Schedule regular backups to prevent data loss.
Security: Regularly update passwords and review user privileges.
This guide ensures a smooth patching process for your MariaDB database on a Linux system, maintaining both functionality and security.