Bug #981 .mysql_history is lost if mysql clinent exit when out of space
Submitted: 2 Aug 2003 9:30 Modified: 25 Jan 2005 18:16
Reporter: [ name withheld ] Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Command-line Clients Severity:S1 (Critical)
Version: OS:Linux (Linux)
Assigned to: Jani Tolonen CPU Architecture:Any

[2 Aug 2003 9:30] [ name withheld ]
Description:
mysql client isn't saving files atomically so there could be data loss on disk full or crashing in the process of saving.
If something, while mysql client is saving, hangs the system like a crash or a
power failure all the data is lost.

This is what mysql client currently does (simplified Linux strace):
open($HOME"/.mysql_history", O_WRONLY|O_CREAT|O_TRUNC, 0600) = file
write(file,...
close(file)

This is an atomic save:
open($HOME"/.mysql_history..TMP", O_WRONLY|O_CREAT|O_EXCL, 0666) = file
write(file,...
close(file)
rename($HOME"/.mysql_history..TMP", $HOME"/.mysql_history")

This way you always keep a good copy if something fails.

How to repeat:
FILL THE HOME PARTITION

mysql -u user -p database
select * FROM mytable;
exit

YOUR HISTORY FILE IS LOST!

Suggested fix:
Save the file atomically as explained in the description.
[25 Jan 2005 16:41] Jani Tolonen
Patch under review.
[25 Jan 2005 18:16] Jani Tolonen
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html