Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminal mysql queries

Tags:

terminal

mysql

I'm looking to use terminal to execute mysql queries. I currently connect to a sql db via the sql workbench but would like to do this via terminal. Is this possible?

I've installed mysql via homebrew

when I hit mysql in terminal it says command not found, maybe I need to do something else besides the homebrew setup?

like image 367
Tony Avatar asked Dec 27 '22 22:12

Tony


2 Answers

Go to the directory:

mysql/bin

To execute the query from command line:

mysql -u [username] -p [dbname] -e [query]

example:

mysql -u root -p database -e "select * from user"

like image 109
Atri Avatar answered Jan 13 '23 02:01

Atri


mysql --help

You can do also:

cat dump.sql | mysql -u user -p password database

Or:

echo "select * from abc;" | mysql -u user -p password database
like image 29
pamil Avatar answered Jan 13 '23 01:01

pamil