Thursday, May 23, 2013

sql:mysql:change default mysql directory

The default mysql directory is "/var/lib/mysql"

This can be changed by the following steps -

1. Change the default directory path in the mysql configuration file.

sudo vi /etc/mysql/my.cnf
datadir        = /mynewdir/mydefaultdir

2. Since mysql is now going to be using a new resource (/mynewdir/mydefaultdir), use apparmor to notify the system so.

sudo vi /etc/apparmor.d/usr.sbin.mysqld
/usr/sbin/mysqld {
  #include <abstractions/base>
  /etc/mysql/conf.d/ r,
  /etc/mysql/conf.d/* r,
  /etc/mysql/*.cnf r,
  /usr/lib/mysql/plugin/ r,
  /usr/lib/mysql/plugin/*.so* mr,
  /usr/sbin/mysqld mr,
  /usr/share/mysql/** r,
  /var/log/mysql.log rw,
  /var/log/mysql.err rw,
  /var/lib/mysql/ r,
  /var/lib/mysql/** rwk,
  /var/log/mysql/ r,
  /var/log/mysql/* rw,
  /mynewdir/ r,           # add this line to give read permission to mynewdir
  /mydefaultdir/ r,      # add this line to give read permission to mydefaultdir
  /mydefaultdir/* rw,  # add this line to give read,write permissions to all the
                                  # files in mydefaultdir

3. sudo /etc/init.d/apparmor reload
4. sudo /etc/init.d/mysql restart

Note: For step 4, some systems have "mysqld" instead of "mysql"

No comments:

Post a Comment