52

when i try to view the databases in mysql i get this error:

ERROR 1018 (HY000): Can't read dir of '.' (errno: 13)

And that stops my app from displaying...

My django debugger says:

(2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/my_database' (13)")

Here is my settings file :

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'my_database',                      # Or path to database file if using sqlite3.
    'USER': 'root',                      # Not used with sqlite3.
    'PASSWORD': '****',                  # Not used with sqlite3.
    'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '3306',                      # Set to empty string for default. Not used with sqlite3.

What can cause the problem?

Thanks in advance

2
  • Have you been connected previously? Can you edit in your settings file so we can get a better idea of what is happening please? Jun 16, 2012 at 19:37
  • Maybe this could be of use, in case your system uses AppArmor:ar.zu.my/… Mar 10, 2014 at 21:06

6 Answers 6

85

You need to set ownership and permissions for directory:

chown -R mysql:mysql /var/lib/mysql/ #your mysql user may have different name
chmod -R 755 /var/lib/mysql/

Note: -R makes commands recursive - you may omit it, if there is no subdirs in /var/lib/mysql/.

7
  • 1
    should i replace the first mysql with my mysql user?
    – Tony
    Jun 16, 2012 at 19:44
  • 1
    the user under which your mysqld daemon is run - see in /etc/passwd. I believe it is mysql by default
    – scriptin
    Jun 16, 2012 at 20:26
  • ive changed it and it works but now it works but if i reload it doesnt... and if i reload again it does
    – Tony
    Jun 16, 2012 at 20:34
  • 6
    @TonyKyriakidis have you restarted mysqld? (service mysqld restart)
    – scriptin
    Jun 16, 2012 at 20:36
  • You will also need to change apparmor settings and reload it. This is a 2012 answer and I am not sure if apparmor was enforced then, but I had the same problem and this answer alone did not help. Fixing apparmor profile did the job. Edit /etc/apparmor.d/usr.sbin.mysqld and sudo service apparmor reload.
    – Lord Loh.
    Jun 2, 2016 at 0:01
37

This should work for Mac users:

sudo chown -R mysql:mysql /usr/local/mysql/
sudo chmod -R 755 /usr/local/mysql/

If this doesn't work, try running which mysql to see where your mysql installation is located, and then replace /usr/local/mysql/ in the command above with whatever is before the 'bin' directory.

For example, on my system which mysql produces the following output:

/usr/local/mysql/bin/mysql

so my path is /usr/local/mysql/

4
  • 1
    I found this with Google and while I was able to use the answer to fix my problem, I had different paths. So I posted this for the benefit of other mac users that might end up here. Dec 14, 2015 at 19:41
  • 1
    It seems to be relocated in El Capitan under macports. I had to use sudo chown -R _mysql:_mysql /opt/local/var/db/mysql56/ sudo chown -R _mysql:_mysql /opt/local/var/run/mysql56/ sudo chown -R _mysql:_mysql /opt/local/var/log/mysql56/
    – DDay
    Apr 1, 2016 at 13:40
  • 1
    Thanks. This helped me after upgrading to El Capitan.
    – Adeola
    Apr 19, 2016 at 17:45
  • 1
    also on OS X here (Mavericks 10.9) - my mysql user was _mysql. you can find out your user (and mysql install dir) with ps aux |grep mysql.
    – gl03
    Nov 16, 2017 at 20:08
3

On CentOS/RedHat, you should do the same thing on a different path:

chown -R mysql:mysql /data/mysql/
chmod -R 755 /data/mysql/
1
  • my /data/ is empty on centos 7
    – jiggunjer
    Mar 31, 2020 at 17:32
1
chown -R mysql:mysql /var/lib/mysql/

chmod -R 755 /var/lib/mysql/

I can confirm that these two chmod statements worked for me (Webmin didn't see the databases nor did show tables) but I'm not sure why I had to do this after setting up perhaps two dozen servers (Centos) with MySQL in that past few years.

1
  • Why have you added the same answer as @scriptin only to say that it works but you didn't know what they actually do. Better put a comment in the other answer and upvote it (if you can). It's a bad idea to suggest to others things that you don't understand.
    – PhoneixS
    Mar 3, 2017 at 12:24
0

osx high sierra use the following command solves the issue:

chown -R mysql:mysql /usr/local/mysql
0

if you installed mariadb using homebrew you can run the following the command for OS X

sudo chown -R mysql:mysql /var/lib/var/mysql/
sudo chmod -R 777 /usr/local/var/mysql/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.