5

I have MySQL data files on a thumb drive that are used on two hosts where mysql user's UIDs differ. As a result, MySQL fails to start when it's files have 0700 perms and an unknown UID as an owner.

I failed to find how to change MySQL's umask (and actually I don't like the idea of sharing these files to everyone), therefore I want to change UID of mysql user on both hosts so the files belong to the same user.

I'm going to change the UID and to chown all files owned by old mysql UID to the new user:

usermod --uid 900 --gid 900 mysql # assign the new uid
olduid=67   find / -user $olduid -group $olduid -print0 | xargs -0 chown "mysql:mysql"

Is this sufficient for an application to work in general case? Maybe, I have better options?

kolypto
  • 11,058
  • 12
  • 54
  • 66

2 Answers2

3

I've made some research and noticed two things one should take into account when changing UIDs&GIDs:

  1. Numeric UID and GID do not always match: in my case id -u mysql=120 and id -g mysql=127
  2. Not all files are owned by user 'mysql' and group 'mysql' simultaneously: these files should be searched for separately.

Therefore, we first change UID and GID:

user=mysql new_uid=600 old_uid=$(id -u $user)
group=mysql new_gid=600 old_gid=$(id -g $user)
sudo usermod -u $new_uid $user
sudo groupmod -g $new_gid $group

Then we find for files owned by the late user and group separately: 'user=mysql' goes to one file, 'group=mysql' goes to another file. Also we exclude some directories from find traversing tree:

chownlist=$(tempfile) chgrplist=$(tempfile) sudo find / \
\( \( -path "/proc" -or -path "/sys" -or -path "/dev" \) -prune \) -or \
\( \( -user $old_uid -fprint0 "$chownlist" \) , \
\( -group $old_gid -fprint0 "$chgrplist" \) \)

And only now it's okay to change owners and groups for these files found:

cat "$chownlist" | xargs -0 sudo chown $user
cat "$chgrplist" | xargs -0 sudo chown :$group
sudo rm "$chownlist" "$chgrplist"

Finally, we check if everything went okay: find files owned by unknown UIDs of GIDs:

sudo find / \( \( -path "/proc" -or -path "/sys" -or -path "/dev" \) -prune \) -or \( -nouser -or -nogroup -print \)

Hope this helps someone.

kolypto
  • 11,058
  • 12
  • 54
  • 66
1

Yeah, that's pretty much all you need to do. The only files which should need changing are the logs and data files.

You might want to use find | xargs rather than a loop though.

James
  • 7,643
  • 2
  • 24
  • 33
  • I believe xargs is not acceptable here because there can be too many files for the shell to handle. Have you ever tried to `rm *` in a folder with thousands of files? :) – kolypto Jan 23 '10 at 01:02
  • You were right, xargs can split dozens of files so the argument line is not overflowed. Thanks! :) – kolypto Jan 23 '10 at 12:48