Execute commands

suggest change

These examples run statements stored in strings or script files without the interactive prompt. That is useful when a shell script talks to the database. -p with the password attached, as in -proot, does not prompt. A space after -p would.

Execute command from a string

$ mysql -uroot -proot test -e'select * from people'

+----+-------+--------+
| id | name  | gender |
+----+-------+--------+
|  1 | Kathy | f      |
|  2 | John  | m      |
+----+-------+--------+

To format the output as a tab-separated grid, use the --silent parameter:

$ mysql -uroot -proot test -s -e'select * from people'

id      name    gender
1       Kathy   f
2       John    m

To omit the headers:

$ mysql -uroot -proot test -ss -e'select * from people'

1       Kathy   f
2       John    m

Execute from script file:

$ mysql -uroot -proot test < my_script.sql
$ mysql -uroot -proot test -e'source my_script.sql'

Write the output on a file

$ mysql -uroot -proot test < my_script.sql > out.txt

$ mysql -uroot -proot test -s -e'select * from people' > out.txt

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents