{"id":8321,"date":"2024-11-12T17:19:38","date_gmt":"2024-11-12T11:49:38","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=8321"},"modified":"2024-11-30T12:50:57","modified_gmt":"2024-11-30T07:20:57","slug":"how-to-move-a-mysql-data-directory-to-a-new-location-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-move-a-mysql-data-directory-to-a-new-location-on-ubuntu-20-04\/","title":{"rendered":"How To Move a MySQL Data Directory to a New Location on Ubuntu 20.04?"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Introduction:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Moving a MySQL data directory to a new location can be necessary when the original storage is limited, or we want to optimize database performance by using faster storage.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Precautions:<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Before making any changes, back up MySQL data to avoid accidental loss or corruption.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To prevent data corruption, stop the MySQL service before moving files<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>sudo systemctl stop mysql<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Assume the current MySQL data directory is located at <code>\/var\/lib\/mysql<\/code>, and want to move it to <code>\/new\/mysql_data<\/code>. Use the following command to copy all data to the new location.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>rsync -av \/var\/lib\/mysql \/new\/mysql_data<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, configure MySQL to use the new data directory by editing the MySQL configuration file.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>sudo nano \/etc\/mysql\/mysql.conf.d\/mysqld.cnf<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Locate the <code>datadir<\/code> parameter, which by default points to <code>\/var\/lib\/mysql<\/code><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>datadir = \/var\/lib\/mysql<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Change it to new data directory path<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>datadir = \/new\/mysql_data<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 4:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we are on a system with AppArmor (common on Debian and Ubuntu), need to update AppArmor\u2019s settings to allow MySQL access to the new directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Edit the AppArmor configuration file for MySQL<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>sudo vi \/etc\/apparmor.d\/usr.sbin.mysqld<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Add the new path so AppArmor permits MySQL to read\/write there<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\/new\/mysql_data\/ r,<br>\/new\/mysql_data\/** rwk,<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Save and reload AppArmor to apply the changes<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>sudo systemctl restart apparmor<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 5:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ensure that the new MySQL directory has the right ownership and permissions so that MySQL can access it properly<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>sudo chown -R mysql:mysql \/new\/mysql_data<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 6:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the above steps are completed, restart the MySQL service to apply the changes<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>sudo systemctl start mysql<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Step 7:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Log into MySQL to confirm that it is using the new data directory. Run the following command<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>mysql -u root -p<br>SHOW VARIABLES LIKE &#8216;datadir&#8217;;<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">If the output displays a new data directory path, then the changes were successfully applied<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Conclusion:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Relocating the MySQL data directory on Ubuntu 20.04 can improve performance and free up space in the system&#8217;s root directory.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Moving a MySQL data directory to a new location can be necessary when the original storage is limited, or [&hellip;]<\/p>\n","protected":false},"author":496,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-8321","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-2ad","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/496"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8321"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8321\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}