Move MySQL database storage location

It’s always a good idea to keep storage away from the boot device. If you run out of space on the boot device the system will halt. If you make a new install it’s easy enough to move your storage and you can do it from a cloud-init script like this:

[shell]
– mkdir /var/db
– chown -R mysql:mysql /var/db
– sed -i ‘s:datadir=/var/lib/mysql:datadir=/var/db:g’ /etc/my.cnf
– service mysqld start
[/shell]

If the installation is all ready up and running you have to add steps for stopping the MySQL server and copy the database files:

[shell]
mkdir /var/www/db
service mysqld stop
mv /var/lib/mysql/* /var/db
chown -R mysql:mysql /var/db
sed -i ‘s:datadir=/var/lib/mysql:datadir=/var/db:g’ /etc/my.cnf
service mysqld start
[/shell]

In these examples I have user /var/db where I mounted the second storage device. You can however use any location you see fit. Points of interest in the command sequence.

[shell]chown -R mysql:mysql /var/db[/shell]

Make sure that the mysql deamon have access to the storage location.

[shell]sed -i ‘s:datadir=/var/lib/mysql:datadir=/var/db:g’ /etc/my.cnf[/shell]

sed is a simple tool for search and replace inside text/config files directly from the command line. Here it searches for the line specifying the MySQL datadir location and replaces it with the new value.

1 Comments on “Move MySQL database storage location”

  1. Pingback: Amazon EC2 Linux – Add additional volumes « Hackviking.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: