Set up Linux Software RAID10 to Store MySQL Data Files and Databases

We’ll be setting up a Linux software RAID10 and re-configuring MySQL to use it for data files and databases. 

Installation

We’re using Ubuntu 13.10 (Saucy Salamander):

# uname -rv
3.11.0-15-generic #23-Ubuntu SMP Mon Dec 9 18:17:04 UTC 2013

Install mdadm and mysql:

# apt-get update && apt-get install mdadm mysql-server

Create Software RAID10

We have 4x2GB disks pre-configured for Linux raid:

# fdisk -l | grep /dev/xv
Disk /dev/xvda1 doesn't contain a valid partition table
Disk /dev/xvda1: 8589 MB, 8589934592 bytes
Disk /dev/xvdf: 2147 MB, 2147483648 bytes
/dev/xvdf1 2048 4194303 2096128 fd Linux raid autodetect
Disk /dev/xvdg: 2147 MB, 2147483648 bytes
/dev/xvdg1 2048 4194303 2096128 fd Linux raid autodetect
Disk /dev/xvdh: 2147 MB, 2147483648 bytes
/dev/xvdh1 2048 4194303 2096128 fd Linux raid autodetect
Disk /dev/xvdi: 2147 MB, 2147483648 bytes
/dev/xvdi1 2048 4194303 2096128 fd Linux raid autodetect

Create the RAID10 array:

# mdadm -C /dev/md0 -l 10 -n 4 /dev/xvdf1 /dev/xvdg1 /dev/xvdh1 /dev/xvdi1 -v
mdadm: layout defaults to n2
mdadm: layout defaults to n2
mdadm: chunk size defaults to 512K
mdadm: size set to 2094592K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

Scan and activate the array automatically on startup:

# mdadm --detail --scan --verbose > /etc/mdadm/mdadm.conf

Wait for the array to finish synchronising disks:

# watch cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
md0 : active raid10 xvdi1[3] xvdh1[2] xvdg1[1] xvdf1[0]
4189184 blocks super 1.2 512K chunks 2 near-copies [4/4] [UUUU]
[=========>...........] resync = 49.2% (2064912/4189184) finish=0.9min speed=38738K/sec

unused devices: <none>

Synchronisation is done:

# cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
md0 : active raid10 xvdi1[3] xvdh1[2] xvdg1[1] xvdf1[0]
4189184 blocks super 1.2 512K chunks 2 near-copies [4/4] [UUUU]

unused devices: <none>

Note that it’s good practice to regularly run data scrubbing to check for and fix errors. To initiate a data scrub manually:

# echo check > /sys/block/md0/md/sync_action

To stop a currently running data scrub safely:

# echo idle > /sys/block/md0/md/sync_action

Create an ext4 filesystem:

# mkfs.ext4 /dev/md0

Create a mountpoint:

# mkdir /sql_data

Mount the raid drive:

# mount /dev/md0 /sql_data

Create a lost+found directory:

# cd /sql_data
# mklost+found

Don’t forget to add a record to fstab:

# echo "/dev/md0 /sql_data ext4 defaults 0 0" >> /etc/fstab

Optionally, configure email alerts for daily checks to ensure that any degraded MD devices don’t go unnoticed:

# echo "MAILADDR [email protected]" >> /etc/mdadm/mdadm.conf
# echo "MAILFROM [email protected]" >> /etc/mdadm/mdadm.conf

Configure MySQL

By default, MySQL stores its data files and databases under /var/lib/mysql on Ubuntu. We want to change it to our newly created raid drive. Stop the daemon first:

# service mysql stop

Open the configuration file and change datadir appropriatelly:

# vim /etc/mysql/my.cnf
[...]
#datadir = /var/lib/mysql
datadir = /sql_data
[...]

Create an alias for the new path:

# echo "alias /var/lib/mysql/ -> /sql_data/," >> /etc/apparmor.d/tunables/alias

Copy all content form the default directory to the new location and change ownership of the folder:

# cp -Rp /var/lib/mysql/* /sql_data
# chown mysql:mysql /sql_data
# chmod 0700 /sql_data

Reload apparmor daemon and start mysql server:

# service apparmor reload
# service mysql start

Check mysql logs in case you run into problems:

# tail -f /var/log/mysql/error.log

Leave a Reply

Your e-mail address will not be published. Required fields are marked *