I had a bug in a backup script running as a cron job which I found and corrected, but I'm still puzzled by the fact that this bug did not show up when the script was started from the command line. The script itself is started as a root and at some moment it starts another script as another user (actually as 'mysql')
su another_user -c "/some/path/another_script"
the another_script in turn does the db dump at some point:
mysqldump db_name > file.sql
This instance of mysql has no root password for localhost, so running this command as a root always works. What I don't understand is that it still works if I start this script from the command line, although this command is called not by 'root' but by 'mysql' user. The db logs show that the connecting user is still 'root'. Whereas when the script is started as a cron job it fails as expected with mysql logs showing that the connecting user was 'mysql'.
The problem actually can be reproduced as simple as that:
su mysql -c "mysqldump db_name > file.sql"
unexpectedly connects as a root db user if started from command line as a root and expectedly connects as mysql db user if started as a root cron job.