how to move a directory from current partition/drive to a new partition/drive

General support questions
StariBrko
Posts: 27
Joined: 2012/08/24 12:35:50

how to move a directory from current partition/drive to a new partition/drive

Post by StariBrko » 2019/10/31 15:18:03

This is a situation with disks on my server.

Code: Select all

[root@MyServer /]# lsblk -i
NAME                         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0                            2:0    1    4K  0 disk
sda                            8:0    0  100G  0 disk
|-sda1                         8:1    0    1G  0 part /boot
`-sda2                         8:2    0   99G  0 part
  |-cl_myserver00-root       253:0    0   91G  0 lvm  /
  `-cl_myserver00-swap       253:1    0    8G  0 lvm  [SWAP]
sdb                            8:16   0  100G  0 disk
`-sdb1                         8:17   0  100G  0 part
  `-cl_myserver01-home       253:2    0  100G  0 lvm  /home
sr0                           11:0    1 1024M  0 rom
and

Code: Select all

[root@MyServer /]# df -hT
Filesystem                           Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl_myserver00-root       xfs        91G   79G   13G  86% /
devtmpfs                             devtmpfs  7.8G     0  7.8G   0% /dev
tmpfs                                tmpfs     7.8G     0  7.8G   0% /dev/shm
tmpfs                                tmpfs     7.8G  153M  7.7G   2% /run
tmpfs                                tmpfs     7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/sda1                            xfs      1014M  184M  831M  19% /boot
/dev/mapper/cl_myserver01-home       xfs       100G  2.5G   98G   3% /home
tmpfs                                tmpfs     1.6G     0  1.6G   0% /run/user/1000
tmpfs                                tmpfs     1.6G     0  1.6G   0% /run/user/1001


Ocasionally I have problem with space on root partition since mysql databases reside in /var/lib/mysql that is under root partition and mysql service gets stucked.
I could take some space from /home partition and assign it to root partition, but I was thinking of adding one more disk, and physically moving /var/lib/mysql there. New disk will be single partition and of lvm type.

This is what I suppose I should do:
• make sdc1 partition on sdc drive
• make lvm named ie. cl_myserver02-mysql

How do I make sure that my new lvm (cl_myserver02-mysql) is assigned to /var/lib/mysql mounting point?

I am still not sure if I described everything correctly, but I hope you got the picture.

thanx

User avatar
TrevorH
Site Admin
Posts: 33202
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: how to move a directory from current partition/drive to a new partition/drive

Post by TrevorH » 2019/10/31 16:05:05

To do this you would need to (not necessarily a complete list of everything):

make a new partition on the new disk and tag it as type 0x8e (LVM) and save that
possibly reboot if it says that the in-memory partition table has not been updated
pvcreate the new partition
vgcreate a new VG containing the new PV
lvcreate a new LV in the new VG
mkfs that new LV to put a filesystem on it
mount that on a different directory - say /mnt/mysql
stop mysql
cd /var/lib/mysql
cp -ax . /mnt/mysql
umount /mnt/mysql
cd ..
mv mysql oldmysql
mkdir -m 755 mysql
chown mysql:mysql mysql
edit /etc/fstab and add a line to that to mount your new LV on /var/lib/mysql at boot time
mount -a to check if that entry works and /var/lib/mysql is now mounted
restorecon -RFv /var/lib/mysql
start mysql
check it's all working how you think it should
if ok then rm /var/lib/oldmysql (backups never hurt!)

Please check before you run any of this as it's just a list and I may have forgotten something vital. Backups are good, make some.
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: how to move a directory from current partition/drive to a new partition/drive

Post by jlehtone » 2019/10/31 16:50:53

For "move": rsync, check, rm original

There are actually multiple options and subdilemmas.

To locate space:
* find some in existing filesystems
* expand existing filesystem
* add filesystems

To add a physical disk can:
* add PV to existing VG
* add PV to new VG
* add partition without LVM

To "move" mysql datadir:
* tell mysql to use new path (where you move the data)
* mount new location to /var/lib/mysql (after moving the data)

There are pros and cons in most of those.


Semi-reasonable: https://linuxhint.com/change_mysql_data ... ry_ubuntu/
(Ubuntu has AppArmor, while CentOS has SELinux.)

User avatar
TrevorH
Site Admin
Posts: 33202
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: how to move a directory from current partition/drive to a new partition/drive

Post by TrevorH » 2019/10/31 16:54:03

* tell mysql to use new path (where you move the data)
Which also means "add new path to selinux so that it knows it's a mysql data store and the daemon is allowed to access it"
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: how to move a directory from current partition/drive to a new partition/drive

Post by jlehtone » 2019/10/31 18:50:23

Yes. Should have stressed that part.

Note: one does not have to mount whole filesystem to /var/lib/mysql.
If you had /home/mysql you could bind-mount /home/mysql to /var/lib/mysql, even though /home/mysql is "just a subdirectory" and filesystem is officially mounted to /home.
(The selinux might provide "entertainment" there too.)

desertcat
Posts: 843
Joined: 2014/08/07 02:17:29
Location: Tucson, AZ

Re: how to move a directory from current partition/drive to a new partition/drive

Post by desertcat » 2019/11/01 02:10:54

TrevorH wrote:
2019/10/31 16:54:03
* tell mysql to use new path (where you move the data)
Which also means "add new path to selinux so that it knows it's a mysql data store and the daemond is allowed to access it"
How appropriate for HALLOWEEN!!! GHOSTS in the machine, GREMLINS, and how can we forget DAEMONDS!!!! Hahahahaha. HAPPY HALLOWEEN everyone!!!!

D'Cat

StariBrko
Posts: 27
Joined: 2012/08/24 12:35:50

Re: how to move a directory from current partition/drive to a new partition/drive

Post by StariBrko » 2019/11/01 07:38:01

Trevor,
at the first glance, your respond seems like exactly what I needed.

I was quite certain that my problem should be a task (relatively easy) possible to solve, but I just wasn't sure the way to do it.

THANX A LOT !!!

jlehtone,
thank you for your suggestions as well.
I was considering expanding root partition taking some 50G from /home partition, but since everything (OS, applications, ...) apart from users data is on the root partition, I was not sure (actually I am still NOT sure) whether such an action would affect proper functioning of my OS itself. Probably not, but nevertheless...
Moving /var/lib/mysql alone is pretty much easy task itself. Just transfer everything somewhere else and change path toward it in my.cnf
But still, proper mapping toward /var/lib/mysql remains a tricky bit in this solution.

Anyhow, both of you helped me a lot.

Of course, I will let you know of final result as soon as i finish everything

cheers everyone

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: how to move a directory from current partition/drive to a new partition/drive

Post by jlehtone » 2019/11/01 08:19:03

StariBrko wrote:
2019/11/01 07:38:01
Moving /var/lib/mysql alone is pretty much easy task itself. Just transfer everything somewhere else and change path toward it in my.cnf
But still, proper mapping toward /var/lib/mysql remains a tricky bit in this solution.
If the new filesystem is already mounted somewhere else, then you can append to /etc/fstab:

Code: Select all

/my/data/location /var/lib/mysql none bind 0 0
You don't need to edit my.cnf and don't need to give selinux instructions. The mysql (and selinux) will see the data in /var/lib/mysql, just like before.

StariBrko
Posts: 27
Joined: 2012/08/24 12:35:50

Re: how to move a directory from current partition/drive to a new partition/drive

Post by StariBrko » 2019/11/01 09:20:05

Well, that sounds interesting indeed!
Basically, if I understood wel:
I make a partition on a new drive, put file system on it, copy /var/lib/mysql there and update fstab.
after reboot, it should work.
correct?

StariBrko
Posts: 27
Joined: 2012/08/24 12:35:50

Re: how to move a directory from current partition/drive to a new partition/drive

Post by StariBrko » 2019/11/01 09:22:53

and it is basically exactly what trevor said.
;)

Post Reply