O'Reilly logo

MySQL in a Nutshell, 2nd Edition by Russell J.T. Dyer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required