Bug #78591 mysqld --initialize fails on empty data directory
Submitted: 27 Sep 2015 17:05 Modified: 4 May 2016 8:39
Reporter: Normann Koldrack Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Packaging Severity:S2 (Serious)
Version:5.7.8 OS:Ubuntu (14.04 LTS)
Assigned to: Lars Tangvald CPU Architecture:Any
Tags: data directory, initialize, ubuntu

[27 Sep 2015 17:05] Normann Koldrack
Description:
After installing via the mysql apt repository I stopped the server, exchanged the my.cnf, deleted the data directory content and tried to initialize mysql again:

mysqld --initialize
2015-09-27T16:46:22.238924Z 0 [ERROR] Can't change data directory owner to mysql
2015-09-27T16:46:22.239029Z 0 [ERROR] Aborting

error log is empty.

How to repeat:
Delete the content of the data directory and try to initialize under Ubuntu 14.04 LTS

#sudo apt-get install mysql-server
#sudo service mysql stop
#sudo nano /etc/mysql/my.cnf
#sudo rm -fr /var/lib/mysql/*

# mysqld --initialize
2015-09-27T16:46:22.238924Z 0 [ERROR] Can't change data directory owner to mysql
2015-09-27T16:46:22.239029Z 0 [ERROR] Aborting
[27 Sep 2015 17:27] Terje Røsten
Hi!

Thanks for report.

Can you try to add sudo to the "mysqld --initialize", like this:

sudo mysqld --initialize

Reason to use sudo is that mysqld will try to follow my.cnf where
you most likely has user = mysql

However, as a normal user you can't switch to mysql user, 
only root can to that.
[27 Sep 2015 17:30] Normann Koldrack
oh sorry.
I did tried sudo and using the root user itself.
both the same error
[27 Sep 2015 17:35] Terje Røsten
Ok, something different then.

Is AppArmor active?

Anything useful in dmesg or related to mysqld in e.g. daemon.log?

Can you also post output from:

# getent passwd mysql
[27 Sep 2015 18:29] Normann Koldrack
ok I did it all again on a new vm.

test@testvm:~$ wget https://dev.mysql.com/get/mysql-apt-config_0.3.7-1ubuntu14.0                                               4_all.deb
test@testvm:~$ sudo dpkg -i mysql-apt-config_0.3.7-1ubuntu14.04_all.deb
test@testvm:~$ sudo apt-get update
test@testvm:~$ sudo apt-get upgrade
test@testvm:~$ sudo apt-get install mysql-server 
test@testvm:~$ sudo service mysql stop
test@testvm:~$ sudo nano /etc/mysql/my.cnf
+innodb_data_file_path=ibdata1:100M:autoextend
test@testvm:~$ sudo passwd
test@testvm:~$ su -
root@testvm:~$ rm -fr /var/lib/mysql/*
root@testvm:~$ su test
test@testvm:~$ sudo mysqld --initialize
2015-09-27T18:18:26.119790Z 0 [ERROR] Can't change data directory owner to mysql
2015-09-27T18:18:26.119870Z 0 [ERROR] Aborting

test@testvm:/etc/apparmor.d$ sudo aa-status
apparmor module is loaded.
5 profiles are loaded.
5 profiles are in enforce mode.
   /sbin/dhclient
   /usr/lib/NetworkManager/nm-dhcp-client.action
   /usr/lib/connman/scripts/dhclient-script
   /usr/sbin/mysqld
   /usr/sbin/tcpdump
0 profiles are in complain mode.
1 processes have profiles defined.
1 processes are in enforce mode.
   /sbin/dhclient (651)
0 processes are in complain mode.
0 processes are unconfined but have a profile defined.

but it should have the proper permission.
test@testvm:/etc/apparmor.d$ sudo nano usr.sbin.mysqld

# Allow data dir access
  /var/lib/mysql/ r,
  /var/lib/mysql/** rwk,

error.log and syslog are empty.
I have no daemon.log
[27 Sep 2015 19:43] Terje Røsten
Thanks for clean reproducer.
[27 Sep 2015 19:46] Normann Koldrack
test@testvm:~$ getent passwd mysql
mysql:x:105:112:MySQL Server,,,:/var/lib/mysql:/bin/false
[5 Oct 2015 5:44] Lars Tangvald
Seems the issue is a combination of missing apparmor privileges and the chown command being run regardless of what the current directory owner is.
[16 Oct 2015 6:31] MySQL Verification Team
Thank you for the feedback.
Verified as described using provided steps.

Thanks,
Umesh
[30 Oct 2015 13:07] Fabrício Raphael
I found this same error on change datadir from mysql.
I changed these files, as recommended:

/etc/mysql/my.cnf
    #datadir		= /var/lib/mysql
    datadir		= /media/sata3/data/mysql

/etc/apparmor.d/usr.sbin.mysqld
    #/var/lib/mysql/ r,
    #/var/lib/mysql/** rwk,
    /media/sata3/data/mysql/ r,
    /media/sata3/data/mysql/** rwk,

/etc/apparmor.d/tunables/alias
    alias /var/lib/mysql/ -> /media/sata3/data/mysql/,

Follow others details:

~$ sudo ls -lha /media/sata3/data/mysql/
total 8,0K
drwxrwx--- 2 mysql mysql 4,0K Out 30 10:45 .
drwxr-xr-x 3 root  root  4,0K Out 28 19:00 ..

~$ sudo ls -lha /var/lib/mysql
lrwxrwxrwx 1 root root 23 Out 28 18:03 /var/lib/mysql -> /media/sata3/data/mysql

# getent passwd mysql
mysql:x:119:129:MySQL Server,,,:/var/lib/mysql:/bin/false

~$ sudo aa-status
apparmor module is loaded.
19 profiles are loaded.
19 profiles are in enforce mode.
   /sbin/dhclient
   /usr/bin/evince
   /usr/bin/evince-previewer
   /usr/bin/evince-previewer//sanitized_helper
   /usr/bin/evince-thumbnailer
   /usr/bin/evince-thumbnailer//sanitized_helper
   /usr/bin/evince//sanitized_helper
   /usr/lib/NetworkManager/nm-dhcp-client.action
   /usr/lib/connman/scripts/dhclient-script
   /usr/lib/cups/backend/cups-pdf
   /usr/lib/telepathy/mission-control-5
   /usr/lib/telepathy/telepathy-*
   /usr/lib/telepathy/telepathy-*//pxgsettings
   /usr/lib/telepathy/telepathy-*//sanitized_helper
   /usr/lib/telepathy/telepathy-ofono
   /usr/sbin/cups-browsed
   /usr/sbin/cupsd
   /usr/sbin/mysqld
   /usr/sbin/tcpdump
0 profiles are in complain mode.
4 processes have profiles defined.
4 processes are in enforce mode.
   /sbin/dhclient (1435) 
   /usr/lib/telepathy/mission-control-5 (2344) 
   /usr/sbin/cups-browsed (1110) 
   /usr/sbin/cupsd (2564) 
0 processes are in complain mode.
0 processes are unconfined but have a profile defined.

~$ sudo mysqld --initialize --defaults-file=/etc/mysql/my.cnf --user=mysql
2015-10-30T13:04:18.684641Z 0 [ERROR] Can't change data directory owner to mysql
2015-10-30T13:04:18.684673Z 0 [ERROR] Aborting

~$ sudo cat /var/log/mysql/error.log
cat: /var/log/mysql/error.log: No such file or directory
[30 Oct 2015 13:40] Fabrício Raphael
My environment:
- Ubuntu 14.04 LTS
- MySQL Community Server 5.7.9
[4 May 2016 8:39] Lars Tangvald
Posted by developer:
 
This bug was fixed in 5.7.10, as per bug #22041387.
[4 May 2016 8:40] Lars Tangvald
The cause was that mysqld would attempt to set ownership on an empty datadir whether or not it was already correct, and the server's apparmor profile will not allow this operation.

So this will still fail if the permissions _are_ incorrect on systems with apparmor running, but this is by design.