I’ve been building utilities with PHP and MySQL command-line tools to clone databases. I ran into an issue when exporting data from multi-gigabyte tables using the mysql client program.

mysql: Out of memory (Needed 4179968 bytes)
ERROR 2008 (HY000) at line 1: MySQL client ran out of memory

The fix for this is easy; just use the –quick option in your call to the mysql command. This prevents the client from caching the resultset forcing it to write each row as it is received.

mysql -q -h localhost -P 3306 -u user -psecret -D db -e 'SELECT * FROM table' > '/tmp/table.csv'