{"id":4605,"date":"2015-04-26T19:10:23","date_gmt":"2015-04-26T11:10:23","guid":{"rendered":"http:\/\/rmohan.com\/?p=4605"},"modified":"2015-04-26T19:12:48","modified_gmt":"2015-04-26T11:12:48","slug":"change-the-default-mysql-data-directory-with-selinux-enabled","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=4605","title":{"rendered":"Change the default MySQL data directory with SELinux enabled"},"content":{"rendered":"<p>This is a short article that explains how you change the default MySQL data directory and adjust SELinux to account for the changes. The article assumes that you\u2019re running either RHEL, CentOS, Scientific Linux or Fedora with SELinux enabled. This works with the most recent EL (6.2) version.<\/p>\n<p>We\u2019ll be doing this in the following order.<\/p>\n<ul>\n<li>Stopping the MySQL server<\/li>\n<li>Create a new data directory and move the content from the old data directory<\/li>\n<li>Correct the MySQL configuration file<\/li>\n<li>Adjust SELinux parameters to accept our new change<\/li>\n<li>Starting the MySQL server<\/li>\n<\/ul>\n<h2>Stopping the MySQL server<\/h2>\n<pre># service mysqld stop<\/pre>\n<h2>Create a new data diretory and move the content from the old one<\/h2>\n<h3>Creating a new data directory<\/h3>\n<pre># mkdir \/srv\/mysql\/<\/pre>\n<pre># chown mysql:mysql \/srv\/mysql<\/pre>\n<h3>Moving the original data files<\/h3>\n<pre> # mv \/var\/lib\/mysql\/* \/srv\/mysql\/<\/pre>\n<h2>Correct the MySQL configuration file<\/h2>\n<p>Edit the my.cnf file for your distribution. In my example it\u2019s located in the \/etc\/mysql\/ directory. RHEL\/CentOS\/Scientific Linux put the my.cnf file directly in \/etc by default.<\/p>\n<pre># nano \/etc\/mysql\/my.cnf<\/pre>\n<p>Change<\/p>\n<pre>datadir=\/var\/lib\/mysql<\/pre>\n<p>to<\/p>\n<pre>datadir=\/srv\/mysql<\/pre>\n<p>and<\/p>\n<pre>socket=\/var\/lib\/mysql\/mysql.sock<\/pre>\n<p>to<\/p>\n<pre>socket=\/srv\/mysql\/mysql.sock<\/pre>\n<p>and save the file.<\/p>\n<h2>Adjust SELinux parameters to accept our new change<\/h2>\n<p>Should the following command output \u201cPermissive\u201d or \u201cDisabled\u201d then you may skip the details for SELinux.<\/p>\n<pre># getenforce<\/pre>\n<p>Run the semanage command to add a context mapping for \/srv\/mysql.<\/p>\n<pre># semanage fcontext -a -t mysqld_db_t \"\/srv\/mysql(\/.*)?\"<\/pre>\n<p>Now use the restorecon command to apply this context mapping to the running system.<\/p>\n<pre># restorecon -Rv \/srv\/mysql<\/pre>\n<h2>Starting the MySQL server<\/h2>\n<pre># service mysqld start<\/pre>\n<h2>Verifying access and connectivity<\/h2>\n<pre>$ mysql -u root -p<\/pre>\n<pre>mysql&gt; show databases;<\/pre>\n<p>If this is working, you\u2019re up and running. Should you get a message that says<\/p>\n<blockquote><p>ERROR 2002 (HY000): Can\u2019t connect to local MySQL server through socket \u2018\/var\/lib\/mysql\/mysql.sock\u2019<\/p><\/blockquote>\n<p>then add the following to your \/etc\/my.cnf<\/p>\n<pre>[client]\r\nsocket = \/srv\/mysql\/mysql.sock\r\n<\/pre>\n<p>Optionally you can just use<\/p>\n<pre>$ mysql -u root -p --protocol tcp<\/pre>\n<p>to avoid connecting via the socket.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a short article that explains how you change the default MySQL data directory and adjust SELinux to account for the changes. The article assumes that you\u2019re running either RHEL, CentOS, Scientific Linux or Fedora with SELinux enabled. This works with the most recent EL (6.2) version.<\/p>\n<p>We\u2019ll be doing this in the following [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4,16],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4605"}],"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=4605"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4605\/revisions"}],"predecessor-version":[{"id":4606,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4605\/revisions\/4606"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}