I tried to change the database directory, datadir, I did the following steps. My machine: Laptop Ubuntu 11.10 64-bit


sudo apt-get install mysql-server mysql-client

All Ok, I can create databases, tables, all ok. Change the datadir:

1) $ /etc/init.d/mysql stop
2) $ cp -R -p /var/lib/mysql /new_path
3) $ rm /new_path                      (this only remove files unnecessary)
4) $ gedit /etc/mysql/my.cnf

Change the "datadir = var/lib/mysql" to the new path.

5) $ gedit /etc/apparmor.d/usr.sbin.mysqld

Change the "/var/lib/mysql" to the new lines with "/new_path/mysql"

6) $ /etc/init.d/apparmor reload
7) $ /etc/init.d/mysql restart
8) $ service mysql status     (is Ok)

The error is the following.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

I tried to change the permissions in /new_path/mysql and didn't work.

I tried to copy only the databases (not all files in mysql dir) and it didn't work

I saw in other forum that I have to change the /etc/apparmor.d/usr.sbin.mysqld the following

/var/run/mysqld/mysqld.pid w,
/var/run/mysqld/mysqld.sock w,

Replace "/var/run/mysqld/mysqld" by "/{,var/}run/mysqld/mysqld" but in my case it was with "/{,var/}run/mysqld/mysqld" by default.

Please I need help, I have 2 weeks with this problem.


  • 1
    Have you tried the steps here: askubuntu.com/questions/224/… ? – cosmorogers Jan 28 '12 at 3:42
  • 1
    Can you look in /var/log/mysql/* and see if there is anything relevant in there, and maybe add it to your question? – SpamapS Feb 8 '12 at 22:26

you can do this:

cd /var/lib/

tar -cf - mysql | (cd /new_path ; tar -xvf -)

then (because it's always a good idea) make a back up.

 mv /var/lib/mysql /var/lib/mysql.old

then a symlink

 ln -s /new_path/mysql/ /var/lib

in that case you dont need to touch the /etc/mysql/my.cnf.


In order for this to work for me I needed to modify the mysql files in /etc/apparmor.d/usr.sbin.mysqld. The symbolic link was not good enough for some reason. Perhaps the link did not exist before apparmor was ran. So I did the following:

nano /etc/apparmor.d/usr.sbin.mysqld

and changed

  #/var/lib/mysql/ r,
  #/var/lib/mysql/** rwk,
  /mysql/mysql/ r,
  /mysql/mysql/** rwk,

where /mysql was my mount point for all of my mysql data repository (an SSD drive).

I got the clue from a post by walli in: Re: MySQL ERROR 2002 (HY000): Can't connect to local

I was lucky to find the solution to this problem. It was an all-nighter for me to find the solution. I hope this helps you. It would be a good idea to post a note directly in the my.cnf file about this issue. I just dont know who to send it to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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