MySQL execute statements directly from the (linux) console

Executing a query quickly from the console is nice to be able to do (without having to enter the MySQL console).

  • This will simply output the result to the console where you are.
     user@server:~$ mysql -uUSERNAME -p DATABASENAME --execute "REPAIR TABLE position";

    Replace USERNAME and DATABASENAME with your vars

  • If you would like the output to a file
     mysql -uUSERNAME -p DATABASENAME --execute "SELECT * FROM test" > test.sql;

    Replace USERNAME and DATABASENAME with your vars

  • If you would like the output to be sent as a email
     mysql -uUSERNAME -p DATABASENAME --execute "SELECT * FROM test" | mail -s 'SUBJECT' example@mail.com

    Replace USERNAME, DATABASENAME , SUBJECT and example@mail.com with your vars