{"id":6509,"date":"2017-02-27T09:13:25","date_gmt":"2017-02-27T01:13:25","guid":{"rendered":"http:\/\/rmohan.com\/?p=6509"},"modified":"2017-02-27T09:13:25","modified_gmt":"2017-02-27T01:13:25","slug":"mysql-databases-to-separate-disks","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6509","title":{"rendered":"MySQL Databases to Separate Disks"},"content":{"rendered":"<p>You may have a database server which started out small, with all its databases stored on the same disks, that is now experiencing severe storage I\/O bottlenecks. With so many heavily accessed databases on the same storage device your queries are timing out while waiting for response from disk. And despite all your efforts in optimizing the databases and queries, there has come a time where the disks just can\u2019t keep up. For this type of scenario, you need to spread your load across more storage devices.<\/p>\n<p>Sadly, MySQL doesn\u2019t have an option to configure separate storage paths for each database like more enterprise database servers do. The solution is to symbolicly link your databases from the new storage device to the MySQL data home directory<\/p>\n<p>&nbsp;<\/p>\n<p>As long as the new location has the proper ownership and SELINUX context, this fools MySQL into believing your migrated databases still exist in the data home directory.<\/p>\n<h3>Objectives<\/h3>\n<ol>\n<li>Prepare new storage for databases.<\/li>\n<li>Moving I\/O heavy databases to separate storage.<\/li>\n<\/ol>\n<h3>Scenario<\/h3>\n<p>We have a MySQL 5.1 server hosting five databases on a single disk. One of the five databases is flooding the disk with I\/O due to its work profile and needs to be moved to separate storage. The databases information is shown below.<\/p>\n<table>\n<tbody>\n<tr>\n<th>Database<\/th>\n<th>Old Data Location<\/th>\n<th>New Data Location<\/th>\n<\/tr>\n<tr>\n<td>webapp02<\/td>\n<td>\/var\/lib\/mysql\/webapp02<\/td>\n<td>\/Databases\/webapp02<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The storage for the the database.<\/p>\n<table>\n<tbody>\n<tr>\n<th>Device Name<\/th>\n<th>Type<\/th>\n<th>Configuration<\/th>\n<\/tr>\n<tr>\n<td>sdb<\/td>\n<td>SCSI<\/td>\n<td>4 physical disks in RAID 10<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><a name=\"toc2\"><\/a><\/p>\n<h2>Preparing Your new Storage<\/h2>\n<ol>\n<li>Attach the new storage devices to the server.<\/li>\n<li>Create a single partition and format it with a filesystem.Create a root directory which will be used to contain mount points for your new storage.\n<pre class=\"bash\">mkdir \/Databases<\/pre>\n<\/li>\n<li>For each database being migrated, create a folder for its storage device to mount to.\n<pre class=\"bash\">mkdir \/Databases\/webapp02<\/pre>\n<\/li>\n<li>Set the SELINUX context type of the new directories to\u00a0<strong>mysqld_db_t<\/strong>\u00a0to allow MySQL access to them.\n<pre class=\"bash\">chcon -r -t mysqld_db_t \/Database<\/pre>\n<\/li>\n<li>Modify\u00a0<strong>fstab<\/strong>\u00a0so that<\/li>\n<\/ol>\n<p><a name=\"toc3\"><\/a><\/p>\n<h2>Copying Databases to New Storage<\/h2>\n<ol>\n<li>Copy your database\u2019s files to the new location, using\u00a0<strong>cp<\/strong>\u00a0and\u00a0<strong>-preserve=all<\/strong>\u00a0to maintain ownership and SELINUX contexts.\n<pre class=\"bash\">cp -r -preserve=all \/var\/lib\/mysql\/mydb1 \/new-mydb1-location<\/pre>\n<\/li>\n<li>Verify the SELINUX context is applied correctly to the directories and files.\n<pre class=\"bash\">ls -lZ \/Databases &amp;&amp; ls -lZ \/Databases\/*<\/pre>\n<\/li>\n<\/ol>\n<p><a name=\"toc4\"><\/a><\/p>\n<h2>Point MySQL to New Database Locations<\/h2>\n<ol>\n<li>Stop the MySQL daemon.\n<pre class=\"bash\">service mysqld stop<\/pre>\n<\/li>\n<li>Navigate to the MySQL data home directory, which is\u00a0<strong>\/var\/lib\/mysql<\/strong>\u00a0by default.<\/li>\n<li>Delete the databases directories for the databases being migrated, making note of the directory names. They\u2019ll be needed in the next step.<\/li>\n<li>Create soft links to the storage of each database being migrated. The link file names must must the name of the database\u2019s directory name.\n<pre class=\"bash\">ln -s \/Databases\/myappdb1 myappdb1<\/pre>\n<\/li>\n<li>Repeat the process for every databases being moved.<\/li>\n<li>After all databases have been migrated, restart the MySQL daemon.<\/li>\n<li>If all goes well, MySQL should start correctly. If it does not, check the system logs for Selinux context errors.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>You may have a database server which started out small, with all its databases stored on the same disks, that is now experiencing severe storage I\/O bottlenecks. With so many heavily accessed databases on the same storage device your queries are timing out while waiting for response from disk. And despite all your efforts in [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72,16],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6509"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6509"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6509\/revisions"}],"predecessor-version":[{"id":6510,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6509\/revisions\/6510"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}