Name

mysql

Synopsis

mysql options [database]

The mysql client can be used to interact with MySQL in terminal or monitor mode. To enter monitor mode, enter something like the following from the command line:

mysql -u russell -p

If the MySQL server is running, the client will prompt the user for a password (thanks to the -p option). Once in monitor mode, you can enter SQL statements to view or to change data as well as the status of the server.

As an alternative to monitor mode, when performing straightforward tasks in MySQL, you can still use the mysql client from the command line. For instance, to execute a batch file that contains several SQL statements that will insert data into a database, you could do something like this:

mysql -u russell -pmy_pwd db1 < stuff.sql

In this example, the password is given so that the user isn’t prompted. It’s entered immediately after the -p option without a space in between. Although including the password on the command line poses a security risk for interactive use, it’s a valuable feature for using mysql in scripts.

Next, the database name db1 is given. The Unix redirect (the less-than sign) tells the shell to input the test file stuff.sql to the command. When the client has finished processing the text file, the user is returned to the command prompt.

To handle even smaller tasks, you can execute a single SQL command against the database by running mysql with the --execute or -e option.

Several options may be given when calling the mysql client at the ...

Get MySQL in a Nutshell, 2nd Edition 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.