windows azure mysql data disk_Migrating MySQL database from OS disk to Data disk on Azure Linux VM

Migrating MySQL database from OS disk to Data disk on Azure Linux VM

04/25/2016

4 分钟可看完

本文内容

NOTE: Please take a backup of your MySQL databases before attempting migration. Taking backups is always a good process before attempting to make changes. Migrating without taking backups can lead to serious issues including data loss.

There are many cases where customer installs MySQL on OS disk without his knowledge. Most of MySQL installs put data directory in /var/lib/mysql. Putting MySQL data files on OS disk can have performance impact because of I/O contention. Particularly so if you are expecting higher performance from MySQL.

How do you install MySQL:

Ubuntu: sudo apt install mysql-server

CentOS: sudo yum install mariadb-server

How to migrate Data from OS disk to Data Disk.

First attach data disks to your VM. Based on IOPS performance and storage requirements, attach disks accordingly. In this blog we will attach two data disks.

Follow this article to attach 2 new empty disks. Procedure is same for both V1 (Classic)/V2 (ARM) VM. https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-attach-disk-portal/

c1d070d673962f4d5b0f14f3e5457beb.png

If you see an error when adding disk like this:

A disk named 'xyzabc-2016mmddetc' already uses the same LUN: 0. This is because you added the disks in succession too fast. Wait for some time and then retry. It will succeed.

Once configured you should see 2 disks like below:

b12aaa4b9d4e66d5074cf6604be96787.png

Create a software raid device using the disks attached. Please note that all commands are run logged in as root in this blog. You can do the same or run from individual id's with sudo.

Follow this article to achieve that: https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-configure-raid/. Below are a series of screenshots from my demo (on Ubuntu 14.04) to illustrate the steps performed to create a software raid device.

bacc381fe835b9d45419c5cdb629b450.png

39a26a62c91e424f62960abdde28d21e.png

You may be prompted for a window to choose mail settings. You can safely choose No Configuration and click ok.

962b704d1548850a0736651f802c58d4.png

12953d6c049c8ff2df61d2f1c69d3e77.png

5d11131b9ead6e122f2a178a342f2398.png

2149ab79a1134c446d03e59209a102b8.png

In below screenshot, i have used nobootwait. nobootwait works for ubuntu, but does not for CentOS. So you would need to replace nobootwait with nofail for CentOS.

8da5d50b8a0b7997397f6921c0231946.png

26f52853e44d17398bb879224c9e868b.png

Once you followed above steps, you should see a directory /data mounted with raid device md127. You can check last image in above step. /dev/md127 is mounted as /data.

Now we start the process of moving data for MySQL.

First Stop MySQL Service:

Ubuntu: service mysql stop

CentOS: service mariadb stop

Copy data from /var/lib/mysql to /data/mysql. For this cd to /data and run this command without doublequotes: "rsync -aux /var/lib/mysql .". If you don't find rsync, you can install rsync package. Then rename /var/lib/mysql folder to /var/lib/mysql_old. Then create a symlink to /data/mysql in /var/lib folder. This is the command from /var/lib directory: "ln -s /data/mysql mysql"

da58e09e5659913cf8ebe366f9089e8d.png

Now go ahead and start mysql. Let's see if it starts. MySQL does not startup. This is because we need to make some more changes for different flavors of Linux.

For Ubuntu 14.04 or lower (Tested with 14.04).

Change permissions on /var/lib/mysql. Command to run is: chown -R mysql:mysql /var/lib/mysql

If AppArmor is not enabled you do not need to do below. But it is recommended to have AppArmor enabled for security. Follow below steps to allow mysql access through AppArmor:

29ed1f2cbc3847570f7969fd74cc016f.png

Add below two lines to allow access for mysqld process and save the file:

ab6caf672790b28b1079ced7d49aeed3.png

Restart apparmor service issuing this command: service apparmor restart.

Now fire up mysql service and you should see it will start up.

10afbdb8b53a1bc5ce7f16617239624f.png

For Ubuntu 16.04

Ubuntu 16.04 comes with MySQL 5.7.x. This introduces new folders for MySQL to handle under /var/lib. These are /var/lib/mysql-files which limits import/export operations and /var/lib/mysql-keyring for secure storage.

So we need to create a folder under /data for mysql-files and mysql-keyring similar to mysql folder and create links similar to what we did in step 6 above.

15e03feee827565d95d6bb2c90b98db7.png

ced86c3f5625d590495b32aee3094d34.png

Then we add these folders for apparmor restrictions into /etc/apparmor.d/usr.sbin.mysqld file.

0001144de620337bc7d91a65a8aaf6a9.png

Now restart apparmor service and then restart mysql service.

For CentOS/Oracle Linux 7.x (Tested with CentOS 7.1 and Oracle Linux 7.0).

If selinux is disabled you do not need to do below. It is not recommended, but if you want to disable selinux, you can just run "setenforce 0" and this will set selinux to permissive mode and MariaDB should startup fine without below changes. But it is recommended to have selinux enabled for security. Follow below steps to allow mysql access through selinux:

First cd to /var/lib folder and change ownership on mysql symlink. It should be owned by mysql:mysql.

b7965cd74da5f3128980917dddaab797.png

Then run below commands to change selinux execution context for /var/lib/mysql and /data/mysql. If you see any error here, change directory to /root and try executing the semanage command again. Then run restore context, so that changes take immediate effect.

3a6decb19c0f24b04c76d3d6a5326085.png

933f138b81243fc8422d1a9361122f83.png

3af6d078d5c847f818e0cf3ec4ec5022.png

0a2c87cb3e48ce8fc221fd2d71fbf343.png

d87a6598af61d4667943cef9f4ed68c8.png

This completes the steps to move MySQL data from OS disk to Data Disks.

Once this has been completed, you can refer to my blog on how to performance tune your MySQL database.

NOTE: Please take a backup of your MySQL databases before attempting migration. Taking backups is always a good process before attempting to make changes. Migrating without taking backups can lead to serious issues including data loss.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值