Several Ways to Run Queries from Bash

In some of these examples, particularly the remote server I’ve included usernames and passwords, but you might not need it locally, depending on your mysql permissions setup.

Run and save results of sql statement against a remote mysql server

mysql -h remote_server_ip -u your_username --password=your_password database_name -N -e 'select * from your_table' > results.txt

Run file of sql commands against a remote mysql server

mysql -u your_username --password=your_password -h server_host_name_or_ip database_name < /tmp/your-file-with-sql-statements.sql

Run file of sql commands against a local mysql server

mysql database_name < /tmp/your-file-with-sql-statements.sql