0

PostgreSQL

In PostgreSQL it is possible to execute commands without entering the postgres=# console by executing sudo -u postgres psql -c "\l" instead of entering the console first and executing \l. By doing this psql commands can be directly executed in one step instead of two steps.

MySQL

In MySQL, sudo -u root mysql -c "" is not accepted as in PSQL. So the console has to be entered first by executing sudo mysql -u root before further commands can be issued.

Question

Is it possible to execute MySQL commands directly (one step) instead of entering the console first and issuing commands (two steps) like in PostgreSQL?

030
  • 1,557

1 Answers1

1

It looks like you want to use the -e option, short for --execte, right?

From man mysql:

    --execute=statement, -e statement

        Execute the statement and quit. The default output format is like
        that produced with --batch. See Section 4.2.3.1, “Using Options
        on the Command Line”, for some examples. With this option, mysql
        does not use the history file.

Different from PSQL, -c is short for --comments here:

    --comments, -c

        Whether to preserve comments in statements sent to the server.
        The default is --skip-comments (discard comments), enable with
        --comments (preserve comments).
Volker Siegel
  • 17,283
  • 1
    @utretch it looks like you want to use the --help option and read there too. What if the -c option meant --clear-database? – pqnet Aug 17 '14 at 17:46
  • @pqnet For a nice example of this problem, see http://unix.stackexchange.com/questions/150628/why-does-find-delete-delete-all-files-in-a-directory-recursively – Volker Siegel Aug 17 '14 at 17:59
  • i was commenting on that question while you wrote this :-) – pqnet Aug 17 '14 at 18:01