Ubuntu – Can’t start MySQL server if the .sock file is changed in /etc/thesql/the.cnf

MySQLsockets

I have installed MySQL server 5.5 on Ubuntu 12.04.
I am trying to start MySQL server on a different sock file. By default MySQL runs on /var/run/mysqld/mysqld.sock.

I am trying to run the same server on /var/run/mysqld/mysqld1.sock.

For this I have made the following changes:

  1. Changes to /etc/mysql/my.cnf
[client]
    port        = 3306
    socket      = /var/run/mysqld/mysqld1.sock

[mysqld_safe]
    socket      = /var/run/mysqld/mysqld1.sock
    nice        = 0

[mysqld]
    user        = mysql
    pid-file    = /var/run/mysqld/mysqld.pid
    **socket        = /var/run/mysqld/mysqld1.sock**
    port        = 3306
    basedir     = /usr
    datadir     = /var/lib/mysql
    tmpdir      = /tmp
    lc-messages-dir = /usr/share/mysql
  1. I also added the following line to /etc/apparmor.d/usr/sbin.mysqld

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

    /var/run/mysqld/mysqld[1-9].sock w,

  2. I also changed the ownership for the directory /var/run/mysqld to mysql user.

    ls -lA /var/run/ | grep mysqld

    drwxrwxrwx 2 mysql mysql 40 Dec 31 17:24 mysqld

However when I try to start MySQL server I get the following error (As a root user)

    $ mysqld --user=mysql --verbose
    121231 18:40:56 [Note] Plugin 'FEDERATED' is disabled.
    121231 18:40:56 InnoDB: The InnoDB memory heap is disabled
    121231 18:40:56 InnoDB: Mutexes and rw_locks use GCC atomic builtins
    121231 18:40:56 InnoDB: Compressed tables use zlib 1.2.3.4
    121231 18:40:56 InnoDB: Initializing buffer pool, size = 128.0M
    121231 18:40:56 InnoDB: Completed initialization of buffer pool
    121231 18:40:56 InnoDB: highest supported file format is Barracuda.
    121231 18:40:57  InnoDB: Waiting for the background threads to start
    121231 18:40:58 InnoDB: 1.1.8 started; log sequence number 1595685
    121231 18:40:58 [Note] Server hostname (bind-address): '127.0.0.1'; port: 3306
    121231 18:40:58 [Note]   - '127.0.0.1' resolves to '127.0.0.1';
    121231 18:40:58 [Note] Server socket created on IP: '127.0.0.1'.
   **`121231 18:40:58 [ERROR] Can't start server : Bind on unix socket: Permission denied`**

    121231 18:40:58 [ERROR] Do you already have another mysqld server running on socket: 
    /var/run/mysqld/mysqld1.sock ?
    121231 18:40:58 [ERROR] Aborting**
    121231 18:40:58  InnoDB: Starting shutdown...
    121231 18:40:58  InnoDB: Shutdown completed; log sequence number 1595685
    121231 18:40:58 [Note] mysqld: Shutdown complete

If I start the server with the default socket file, I am able to start the server. I have googled about this issue but only found solutions suggesting it to be permissions issue. However the permissions seems fine. Some have suggested that AppArmor might be a cause, but I have checked that too – snippet is pasted above.

Can someone provide some clues?

[EDIT]

I see the following output in /var/log/syslog.

Jan  2 12:09:34 praveshp-lt mysqld: 130102 12:09:34 [Note] Plugin 'FEDERATED' is disabled.
Jan  2 12:09:34 praveshp-lt mysqld: 130102 12:09:34 InnoDB: The InnoDB memory heap is disabled
Jan  2 12:09:34 praveshp-lt mysqld: 130102 12:09:34 InnoDB: Mutexes and rw_locks use GCC atomic builtins
Jan  2 12:09:34 praveshp-lt mysqld: 130102 12:09:34 InnoDB: Compressed tables use zlib 1.2.3.4
Jan  2 12:09:34 praveshp-lt mysqld: 130102 12:09:34 InnoDB: Initializing buffer pool, size = 128.0M
Jan  2 12:09:34 praveshp-lt mysqld: 130102 12:09:34 InnoDB: Completed initialization of buffer pool
Jan  2 12:09:34 praveshp-lt mysqld: 130102 12:09:34 InnoDB: highest supported file format is Barracuda.
Jan  2 12:09:35 praveshp-lt mysqld: 130102 12:09:35  InnoDB: Waiting for the background threads to start
Jan  2 12:09:36 praveshp-lt mysqld: 130102 12:09:36 InnoDB: 1.1.8 started; log sequence number 1595685
Jan  2 12:09:36 praveshp-lt mysqld: 130102 12:09:36 [Note] Server hostname (bind-address): '127.0.0.1'; port: 3307
Jan  2 12:09:36 praveshp-lt mysqld: 130102 12:09:36 [Note]   - '127.0.0.1' resolves to '127.0.0.1';
Jan  2 12:09:36 praveshp-lt mysqld: 130102 12:09:36 [Note] Server socket created on IP: '127.0.0.1'.
Jan  2 12:09:36 praveshp-lt mysqld: 130102 12:09:36 [ERROR] Can't start server : Bind on unix socket: Permission denied
Jan  2 12:09:36 praveshp-lt mysqld: 130102 12:09:36 [ERROR] Do you already have another mysqld server running on socket: /var/run/mysqld/mysqld1.sock ?
Jan  2 12:09:36 praveshp-lt mysqld: 130102 12:09:36 [ERROR] Aborting
Jan  2 12:09:36 praveshp-lt mysqld: 
Jan  2 12:09:36 praveshp-lt mysqld: 130102 12:09:36  InnoDB: Starting shutdown...
Jan  2 12:09:36 praveshp-lt kernel: [ 7060.098580] type=1400 audit(1357108776.036:33): apparmor="DENIED" operation="mknod" parent=6702 profile="/usr/sbin/mysqld" name="/run/mysqld/mysqld1.sock" pid=7039 comm="mysqld" requested_mask="c" denied_mask="c" fsuid=116 ouid=116
Jan  2 12:09:36 praveshp-lt mysqld: 130102 12:09:36  InnoDB: Shutdown completed; log sequence number 1595685
Jan  2 12:09:36 praveshp-lt mysqld: 130102 12:09:36 [Note] /usr/sbin/mysqld: Shutdown complete

It says permission denied, however I seem to have set the right permissions.

Best Answer

By looking at this line in the following line in the log, it appears that Apparmor is denying access.

Jan  2 12:09:36 praveshp-lt kernel: [ 7060.098580] type=1400 audit(1357108776.036:33):
     apparmor="DENIED" operation="mknod" parent=6702 profile="/usr/sbin/mysqld" 
     name="/run/mysqld/mysqld1.sock" pid=7039 comm="mysqld" requested_mask="c"
     denied_mask="c" fsuid=116 ouid=116

To allow this, add a file /etc/apparmor.d/local/usr.sbin.mysqld with the entry below, and restart MySQL.

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

Thanks to Rinzwind for pointing this out.

Related Question