How to Move the Maria DB Data Directory In AWS EC2 Instance

Typical MariaDB Server installations have the MySQL data directory located in /var/lib/mysql. The data directory contains the data of the databases and the size increases depending on the usage. In this post, you will learn how to move the MariaDB Server data directory to a new mounted EBS Volume. This is useful to separate the data disk from the root volume

~$ ls /var/lib/mysql/
aria_log.00000001  debian-10.3.flag  ibdata1      ib_logfile1  multi-master.info  performance_schema
aria_log_control   ib_buffer_pool    ib_logfile0  ibtmp1       mysql

Pre-requisite

  • AWS Account
  • EC2 instance
  • New EBS Volume
  • MariaDB Server installed
  • rsync

Read more here about installing the MariaDB Server

After setting up the MariaDB Server, Install rsync

~$ sudo apt-get install rsync

Create a EBS Volume and Attach to the EC2 Instance where MariaDB is installed

To read more about attach and mounting the EBS Volume to EC2 Instance, click here

Here I mounted the disk with the device name /dev/sdf and the df command

:/datadir$ df -h .
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvdf        12G   30M   12G   1% /datadir

Stop the MariaDB Service

$ sudo service mariadb stop

Move the Data to the mounted disk

$ sudo rsync -av /var/lib/mysql/ /datadir/
$ sudo rm -rf /var/lib/mysql

Configure the new Data Directory in the MariaDB Config

sudo nano /etc/mysql/my.cnf

Change the datadir to /datadir ( the new mountpoint )

Start the service

sudo service mariadb start

Verification

Connect to the database and create a new schema

MariaDB [(none)]> create schema vignesh;
Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> show databases;
+---------------------+
| Database            |
+---------------------+
| #mysql50#lost+found |
| information_schema  |
| mysql               |
| performance_schema  |
| vignesh             |
+---------------------+
5 rows in set (0.001 sec)

Then check the datadir , you can see the new folder with the schema name created


Also published on Medium.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.