Command-Line Interface

It’s not necessary to open a MySQL monitor to enter SQL statements into the MySQL server. In fact, sometimes you may have only a quick query to make in MySQL, and you’d rather just do it from the shell or command line. For instance, suppose we have a table called vendors in our database, and we want to get a quick list of vendors in Louisiana and their telephone numbers. We could enter the following from the shell in Linux (or an equivalent operating system) to get this list:

mysql --user='tina' --password='muller' \
-e "SELECT vendor, telephone FROM vendors \
    WHERE state='LA'" bookstore

The mysql command or interface is called on, although we’re not entering monitor mode. Next, we provide the username tina and the password muller. This line ends with a backslash to let the Unix shell know that there are more parameters to come. Otherwise, we would need to put all of this on one line. On the second line we use the -e switch to indicate that what follows it in quotes is to be executed by the mysql client. Notice that what’s in double quotes is the same SQL statement in the same syntax as we would enter it in monitor mode. Finally, we provide the name of the database to be used.

Other command-line options and command-line utilities are available. You can use some of these utilities for backing up the database or for performing server maintenance and tuning. These are covered in Chapter 11.

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.