How to Use Other Disk in Dedicated Server to Store MySQL Data

You know, sometimes your main disk on a server starts to feel a bit crowded. You might be running out of space fast, especially if you host websites, use MySQL, or store lots of data. I’ve been there.

This guide is about using a second disk on your dedicated server to store your MySQL data. I’ll walk you through the process step by step, using simple words and real examples.

A dedicated server means you have a full machine just for you. No sharing with other people. It often comes with more than one disk (or hard drive). By default, MySQL stores its data on the main disk—usually somewhere like /var/lib/mysql. But if you have another disk that’s empty or not being used, why not move the MySQL data there?

It’s a bit like moving all your heavy books to a new shelf because the old one is bending. Let’s get started.


What You’ll Learn

  • How to prepare your second disk
  • How to move MySQL data safely
  • How to make MySQL use the new location every time it starts

I’ll also add some tips I learned from doing this myself. And yes, I made a few mistakes at first—so you don’t have to.


Step 1: Check Your Second Disk

First, we need to make sure your second disk is connected and working.

Run this command:

lsblk

You’ll see a list of drives. Your first disk is usually /dev/sda, and the second might be /dev/sdb. If you see sdb or something like it, cool—you’ve got a second drive.

Next, let’s see if it’s already formatted. You can check with:

sudo fdisk -l

If the disk looks blank or says “does not contain a recognized partition,” then we need to format it.


Step 2: Format and Mount the Disk

If the second disk is new or empty, we’ll create a file system on it. I’ll use ext4 because it’s common and works well.

Be super careful here. Make sure you’re working with the correct disk (like /dev/sdb). Messing with the wrong one can wipe your whole server.

Run:

sudo mkfs.ext4 /dev/sdb

Now we need to create a mount point. That’s just a folder where the disk will show up.

sudo mkdir /mnt/mysql-disk
sudo mount /dev/sdb /mnt/mysql-disk

You can check it’s mounted with:

df -h

You should see something like /dev/sdb listed with the new space.

To make it stay mounted after a reboot, you’ll want to add it to /etc/fstab. Use this:

sudo blkid /dev/sdb

It gives you something like this:

/dev/sdb: UUID="abc123-456" TYPE="ext4"

Now open the fstab file:

sudo nano /etc/fstab

Add this line at the bottom (replace UUID with the one you saw):

UUID=abc123-456 /mnt/mysql-disk ext4 defaults 0 2

Save and close. Try rebooting your server to make sure it mounts automatically.


Step 3: Stop MySQL and Copy the Data

Now we’ll tell MySQL to move out of its old home. First, we stop MySQL so it doesn’t get confused while we copy stuff.

sudo systemctl stop mysql

Then copy the entire MySQL data folder to the new disk:

sudo rsync -av /var/lib/mysql /mnt/mysql-disk/

This might take a while depending on how big your database is.

Rsync makes sure everything is copied properly, including file permissions and hidden files. Kind of like a careful librarian moving every book to the right spot.


Step 4: Point MySQL to the New Folder

Now we’ll change MySQL’s settings so it looks in the new spot when it starts.

Open the MySQL config file:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Find the line that says:

datadir = /var/lib/mysql

Change it to:

datadir = /mnt/mysql-disk/mysql

Also, make sure the socket path is okay. Usually it’s fine, but you can double check it later if there’s an error.


Step 5: Update AppArmor or SELinux (If Needed)

This part depends on your system. On Ubuntu, AppArmor might block MySQL from using the new folder. Let’s fix that.

Edit the AppArmor profile:

sudo nano /etc/apparmor.d/usr.sbin.mysqld

Find the lines that mention /var/lib/mysql and add new ones for /mnt/mysql-disk/mysql. Like this:

/mnt/mysql-disk/mysql/ r,
/mnt/mysql-disk/mysql/** rwk,

Then reload AppArmor:

sudo systemctl reload apparmor

If you’re using SELinux (on CentOS or RHEL), that’s a whole different story. But you’d likely need to use semanage fcontext and restorecon.


Step 6: Fix Permissions

MySQL needs to own its new home. Let’s set the right permissions:

sudo chown -R mysql:mysql /mnt/mysql-disk/mysql

This makes sure MySQL can read and write all the files, or it’ll throw tantrums (a.k.a. errors).


Step 7: Start MySQL Again

Let’s start the service and see if MySQL wakes up happy.

sudo systemctl start mysql

If it works, you can log in and check your data:

mysql -u root -p

If everything looks okay, then congrats! You’ve just moved your MySQL data to a new disk.


Why This Is Useful

Here are a few reasons why I like doing this:

  • More room: You free up your main disk, so your system runs smoother.
  • Better organization: You can keep MySQL data separate from other stuff.
  • Easy backups: It’s simpler to back up or clone the second disk if it’s just for databases.

Common Mistakes to Avoid

I’ve made a few goofs when trying this in the past. Here are some things to watch out for:

  • Forgetting to stop MySQL before copying the data.
  • Skipping permission changes—then MySQL can’t read its own files.
  • Not editing AppArmor or SELinux—then everything breaks.

Signs It Worked

Here’s how you know you did everything right:

  • MySQL starts without errors.
  • You can see your databases and tables.
  • You’ve got more free space on your main disk.

A Little Comparison

Using a second disk is like giving MySQL its own bedroom instead of sleeping in the living room with everyone else. It has space, peace, and its own closet (data folder). This setup also helps when your main disk is already busy with other tasks.


Quick Checklist

Here’s a recap of the steps:

  • Format and mount your second disk
  • Stop MySQL
  • Copy data to the new location
  • Update MySQL config
  • Fix permissions and security rules
  • Start MySQL and check everything

Final Thoughts

Moving MySQL to another disk on your dedicated server might sound scary at first. But once you do it, it just feels smart. You’re using your hardware better, your system has more space, and everything is more organized.

Think of it like moving your pet hamster to a bigger cage. Same hamster, just more room to run.

I hope this guide helped you. Did it work for you? Did something get weird? Let me know—I’d love to hear how your disk adventure goes.

Leave a Reply