Using the mysql Client to Export Data

The synt ax for t his st at em ent com bines a regular SELECT w it h INTO OUTFILE filename at t he end. The default out put form at is t he sam e as for LOAD DATA , so t he following st at em ent export s t he passwd t able int o t m p passwd.t xt as a t ab-delim it ed, linefeed- t erm inat ed file: mysql SELECT FROM passwd INTO OUTFILE tmppasswd.txt; You can change t he out put form at using opt ions sim ilar t o t hose used wit h LOAD DATA t hat indicat e how t o quot e and delim it colum ns and records. To export t he passwd t able in CSV form at wit h CRLF- t erm inat ed lines, use t his st at em ent : mysql SELECT FROM passwd INTO OUTFILE tmppasswd.txt - FIELDS TERMINATED BY , ENCLOSED BY - LINES TERMINATED BY \r\n; SELECT ... INTO OUTFILE has t he following propert ies: • The out put file is creat ed direct ly by t he MySQL server, so t he filenam e should indicat e where you want t he file t o be writ t en on t he server host . There is no LOCAL version of t he st at em ent analogous t o t he LOCAL version of LOAD DATA . • You m ust have t he MySQL FILE privilege t o execut e t he SELECT ... INTO st at em ent . • The out put file m ust not already exist . This prevent s MySQL from clobbering files t hat m ay be im port ant . • You should have a login account on t he server host or som e way t o ret rieve t he file from t hat host . Ot herwise, SELECT ... INTO OUTFILE likely will be of no value t o you. • Under Unix, t he file is creat ed world readable and is owned by t he MySQL server. This m eans t hat alt hough youll be able t o read t he file, you m ay not be able t o delet e it .

10.14.5 Using the mysql Client to Export Data

Because SELECT ... INTO OUTFILE writ es t he dat afile on t he server host , you cannot use it unless your MySQL account has t he FILE privilege. To export dat a int o a local file, you m ust use som e ot her st rat egy. I f all you require is t ab- delim it ed out put , you can do a poor- m ans export by execut ing a SELECT st at em ent wit h t he m ysql program and redirect ing t he out put t o a file. That way you can writ e query result s int o a file on your local host wit hout t he FILE privilege. Heres an exam ple t hat export s t he login nam e and com m and int erpret er colum ns from t he passwd t able creat ed earlier in t his chapt er: mysql -e SELECT account, shell FROM passwd -N cookbook shells.txt The - e opt ion specifies t he query t o execut e, and - N t ells MySQL not t o writ e t he row of colum n nam es t hat norm ally precedes query out put . The lat t er opt ion was added in MySQL 3.22.20; if your version is older t han t hat , you can achieve t he sam e end by t elling m ysql t o be really silent wit h t he -ss opt ion inst ead: mysql -e SELECT account, shell FROM passwd -ss cookbook shells.txt Not e t hat NULL values are writ t en as t he st ring NULL . Som e sort of post processing m ay be necessary t o convert t hem , depending on what you want t o do wit h t he out put file. I t s possible t o produce out put in form at s ot her t han t ab-delim it ed by sending t he query result int o a post -processing filt er t hat convert s t abs t o som et hing else. For exam ple, t o use hash m arks as delim it ers, convert all t abs t o charact ers TAB indicat es w here you t ype a t ab charact er in t he com m and : mysql -N -e your query here db_name | sed -e s TAB g output_file You can also use t r for t his purpose, t hough t he synt ax m ay vary for different im plem ent at ions of t his ut ilit y. The com m and looks like t his for Mac OS X or RedHat Linux: mysql -N -e your query here db_name | tr \t output_file The m ysql com m ands j ust shown use - N or - ss t o suppress colum n labels from appearing in t he out put . Under som e circum st ances, it m ay be useful t o include t he labels. For exam ple, t hey m ay be useful when im port ing t he file lat er. I f so, om it t he label-suppression opt ion from t he com m and. I n t his respect , export ing query result s wit h m ysql is m ore flexible t han SELECT ... INTO OUTFILE because t he lat t er cannot produce out put t hat includes colum n labels.

10.14.6 See Also