{"id":2288,"date":"2015-07-20T14:57:29","date_gmt":"2015-07-20T14:57:29","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=2288"},"modified":"2024-01-27T17:35:24","modified_gmt":"2024-01-27T17:35:24","slug":"mysql-1018-hy000-cant-read-dir-errno-13-permission-denied","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/mysql-1018-hy000-cant-read-dir-errno-13-permission-denied","title":{"rendered":"MySQL Permission denied error 13 and solution"},"content":{"rendered":"\n<p>In this post we will see the resolution of MySQL &#8220;permission denied&#8221; error due to movement of database to a different partition on CentOS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span style=\"color: #ff6600;\"> ERROR 1018 (HY000): Can&#8217;t read dir of &#8216;.\/sakila\/&#8217; (errno: 13 &#8211; Permission denied)<\/span><\/h2>\n\n\n\n<p>Recently, a friend was stuck on following issue and he was not able to access the tables under &#8216;sakila&#8217; database. Though he was able to read all other databases!<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR 1018 (HY000): Can't read dir of '.\/sakila\/' (errno: 13 - Permission denied)<\/pre>\n\n\n\n<p>This is what happens when he try accessing the tables from sakila database:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;use sakila;\nReading table information for completion of table and column names\nYou can turn off this feature to get a quicker startup with -A\n\nDatabase changed\nmysql&gt; show tables;\nERROR 1018 (HY000): Can't read dir of '.\/sakila\/' (errno: 13 - Permission denied)\n<\/pre>\n\n\n\n<!--more-->\n\n\n\n<p>We reviewed the &#8216;sakila&#8217; database, it was well owned by mysql user and group with proper permissions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@localhost mysql]# ls -lrth | grep sakila\nlrwxrwxrwx. 1 mysql mysql   15 Jul  6 07:51 sakila -&gt; \/database\/sakila\n<\/pre>\n\n\n\n<p>Note that &#8220;sakila&#8221; is pointing (\/soft-linked) to a different partition which was the recent change he made! We confirmed that the partition and sakila database \/ datafiles were again having correct permissions too.<\/p>\n\n\n\n<p>Usually such error is <span style=\"text-decoration: underline;\">caused by wrong permission<\/span> due to which MySQL isn&#8217;t able to read the specifics. Correcting the file\/folder permissions can fix them quickly!<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$] chown mysql:mysql \/database -R  # Making sure \/database and everything under is mysql owned.\n$] chown mysql:mysql \/var\/lib\/mysql -R # Making sure mysql $] datadir and everything under is mysql owned.\n<\/pre>\n\n\n\n<p>We can actually connect as mysql user and confirm that the table files are readable by mysql!<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@localhost mysql]# su - mysql\n[mysql@localhost ~] cd \/var\/lib\/mysql\/sakila\n[mysql@localhost sakila]# cat actor.frm\n...\n<\/pre>\n\n\n\n<p>Though ours was not the permission issue and without softlink it was working fine!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution to the Permission Denied Error<\/strong><\/h2>\n\n\n\n<p>Considering, all but sakila database&nbsp; tables were generating &#8220;permission denied&#8221; errors and mysql error log remained silent about this, we&#8217;re surely dealing with some &#8220;external&#8221; power!<\/p>\n\n\n\n<p>We are on CentOS and we have something called <strong>SELinux<\/strong>!<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>Security-Enhanced Linux (SELinux) is a Linux kernel security module that provides a mechanism for supporting access control security policies, including ... mandatory access controls (MAC).<\/em>\n- Wikipedia\n<\/pre>\n\n\n\n<p>SELinux logs at <strong>\/var\/log\/audit\/audit.log<\/strong> and following is a snippet from audit log:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">type=AVC msg=audit(1436192732.305:119): avc:  <span style=\"color: #ff0000;\"><strong>denied  { read } for  pid=7063 comm=\"mysqld\" name=\"sakila\"<\/strong><\/span> dev=sda3 ino=655362 scontext=unconfined_u:system_r:mysqld_t:s0 tcontext=unconfined_u:object_r:default_t:s0 tclass=dir\n<\/pre>\n\n\n\n<p>This confirms that SELinux is obstructing MySQL to read from location other than datadir and fixing that will make MySQL happy.<\/p>\n\n\n\n<p>Command <b>&#8216;sestatus&#8217;<\/b> can show the SELinux Status:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@localhost mysql]# sestatus\nSELinux status:                 enabled\nSELinuxfs mount:                \/selinux\nCurrent mode:                   enforcing\nMode from config file:          <span style=\"color: #ff0000;\"><strong>enforcing<\/strong><\/span>\nPolicy version:                 24\nPolicy from config file:        targeted\n<\/pre>\n\n\n\n<p>Here we can note SELinux is enabled and the mode is enforcing.<\/p>\n\n\n\n<p>Here comes 2 more commands: <b>setenforce &amp; getenforce.<\/b><\/p>\n\n\n\n<p>The &#8220;setenforce 0&#8221; command switches off SELinux enforcing, and getenforce shows you the current status. Though setenforce is a &#8220;temporary&#8221; solution and it will auto-reset to configuration option upon system reboot.<\/p>\n\n\n\n<p>To make it persistent over reboots, we&#8217;ll need to change a configuration file, \/etc\/sysconfig\/selinux:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@localhost mysql]# cat \/etc\/sysconfig\/selinux\n# This file controls the state of SELinux on the system.\n# SELINUX= can take one of these three values:\n#     enforcing - SELinux security policy is enforced.\n#     permissive - SELinux prints warnings instead of enforcing.\n#     disabled - No SELinux policy is loaded.\n<span style=\"color: #99cc00;\">#SELINUX=enforcing # Removed this line to disable SELinux completely.\nSELINUX=disabled<\/span>\n# SELINUXTYPE= can take one of these two values:\n#     targeted - Targeted processes are protected,\n#     mls - Multi Level Security protection.\nSELINUXTYPE=targeted\n<\/pre>\n\n\n\n<p>We added &#8220;SELINUX=disabled&#8221; to <span style=\"text-decoration: underline;\">disable SELinux completely<\/span>. (You might want to keep it &#8220;<span style=\"text-decoration: underline;\">permissive<\/span>&#8221; or even better, manage SELinux policies.)<\/p>\n\n\n\n<p>Upon disabling SELinux the issue went away and &#8216;sakila&#8217; database&nbsp; tables were accessible.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; use sakila\nReading table information for completion of table and column names\nYou can turn off this feature to get a quicker startup with -A\n\nDatabase changed\nmysql&gt; show tables;\n+----------------------------+\n| Tables_in_sakila           |\n+----------------------------+\n| actor                      |\n| actor_info                 |\n| address                    |\n| category                   |\n| city                       |\n| country                    |\n| customer                   |\n| customer_list              |\n| film                       |\n| film_actor                 |\n| film_category              |\n| film_list                  |\n| film_text                  |\n| inventory                  |\n| language                   |\n| nicer_but_slower_film_list |\n| payment                    |\n| rental                     |\n| sales_by_film_category     |\n| sales_by_store             |\n| staff                      |\n| staff_list                 |\n| store                      |\n+----------------------------+\n23 rows in set (0.00 sec)\n<\/pre>\n\n\n\n<p>Just to note that <strong>\/etc\/sysconfig\/selinux<\/strong> configuration file for SELinux is actually pointing to \/etc\/selinux\/config.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@localhost mysql]# ls -l \/etc\/sysconfig\/selinux\nlrwxrwxrwx. 1 root root 17 Oct  8  2014 \/etc\/sysconfig\/selinux -&gt; ..\/selinux\/config\n<\/pre>\n\n\n\n<p>Later I found plenty of posts just talking about the same issue! So we walked a common path, but not without learning!<\/p>\n\n\n\n<p>On CentOS, Redhat or Fedora we have SELinux while on Ubuntu, Debian or SuSE you should find AppArmor! Refer a good post from Jeremy @Oracle <a href=\"https:\/\/blogs.oracle.com\/jsmyth\/entry\/selinux_and_mysql\" target=\"_blank\" rel=\"nofollow noopener\">here.<\/a><\/p>\n\n\n\n<p>Hope this helps (or refreshes).<\/p>\n","protected":false},"excerpt":{"rendered":"MySQL ERROR 1018 (HY000): Can&#8217;t read dir of &#8216;.\/sakila\/&#8217; (errno: 13 &#8211; Permission denied) caused due to moving database to a different partition and using softlink on CentOS and it&#8217;s fix for SELinux.\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,377],"tags":[872,873,874,871,533],"class_list":{"0":"post-2288","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-mysql","7":"category-mysql-articles","8":"tag-errno-13-permission-denied","9":"tag-fix-mysql-permission-denied-error","10":"tag-mysql-and-selinux","11":"tag-mysql-permission-denied","12":"tag-selinux"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=2288"}],"version-history":[{"count":6,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2288\/revisions"}],"predecessor-version":[{"id":2865,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2288\/revisions\/2865"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=2288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=2288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=2288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}