How to Run a Mysql Query from Command Line and Write the Output into a File
search cancel

How to Run a Mysql Query from Command Line and Write the Output into a File


Article ID: 36871


Updated On:


CA Infrastructure Management CA Performance Management - Usage and Administration



Usually people run MySQL query from the MySQL prompt, but it's a common practice to run it from command line and write the output into a file. This article provides the steps for doing it.




CA Performance Management (all versions)

All supported platforms



  1. On the CAPC system, move into the mysql bin directory
    cd /opt/vertica/MySQL/bin.

  2. Run a MySQL query from the command line and write the sql output into a file.


Here is an example of a sql statement (between the double quotes) run from the command line; netqosportal.dst_device is the database name and table name.

mysql -e "select SourceID,ItemID,ItemName,Model from netqosportal.dst_device;" > /tmp/output.txt


Below is what the file content looks like

cat /tmp/output.txt

SourceID       ItemID       ItemName             Model
3       117
3       116     Sim24077:brlb-cmts-2    BSR64000
3       118     Sim24593:NetScaler      Citrix NetScaler
3       115     DataAggregator:    NULL
3       114     IMDC1RH65       NULL
5       305   NULL
6       357       NULL
6       355     cis2514-96.24   NULL
6       356     cis3640-96.25.153       NULL
6       350 NULL
6       353    NULL
6       361     rs3000-green-96.30      NULL
6       360     rs3000-green-96.29-1411911752   NULL
6       349   NULL
6       365    NULL
6       362       NULL
6       352    NULL
6       364     rs3000-PurpleCE1-96.39  NULL
6       351    NULL
6       354    NULL
6       366     rs3000-246.19   NULL
6       363     cis2811-96.32   NULL
6       348     NULL















Release: IMDAGG99000-2.5-Infrastructure Management-Data Aggregator