Выбрать главу

FIGURE 18.7 The user accesses the database through the World Wide Web. The front end is the user's web browser, the client is running on leopard, and the server is running on simba.

In another possible web access scenario, it could be said that the client is a two-piece application in which part of it is running on the user's workstation and the other part is running on the web server. For example, the database programmer can use JavaScript in the web form to ensure that the user has entered a valid query. In this case, the user's query is partially processed on her own workstation and partially on the web server. Error checking is done on the user's own workstation, which helps reduce the load on the server and also helps reduce network traffic because the query is checked for errors before being sent across the network to the server.

The MySQL Command-Line Client

The MySQL command-line client is mysql, and it has the following syntax:

mysql [options] [database]

Some of the available options for mysql are discussed in Table 18.1. database is optional, and if given, it should be the name of the database to which you want to connect.

TABLE 18.1 Command-Line Options to Use When Invoking mysql

Option Action
-h hostname Connects to the remote host hostname (if the database server isn't located on the local system).
-u username Connects to the database as the user username.
-p Prompts for a password. This option is required if the user as whom you are connecting needs a password to access the database. Note that this is a lowercase p.
-P n Specifies n as the number of the port to which the client should connect. Note that this is an uppercase P.
-? Displays a help message.

More options are available than are listed in Table 18.1, but these are the most common options. See the man page for mysql for more information on the available options.

CAUTION

Although mysql enables you to specify the password on the command line after the -p option, and thus enables you to avoid having to type the password at the prompt, you should never invoke the client this way. Doing so causes your password to display in the process list, and the process list can be accessed by any user on the system. This is a major security hole, so you should never give your password on the mysql command line.

You can access the MySQL server without specifying a database to use. After you log in, you use the help command to get a list of available commands, like this:

mysql> help

MySQL commands:

Note that all text commands must be first on line and end with ';'

help (\h) Display this help.

? (\?) Synonym for `help'.

clear (\c) Clear command.

connect (\r) Reconnect to the server. Optional arguments are db and host.

edit (\e) Edit command with $EDITOR.

ego (\G) Send command to mysql server, display result vertically.

exit (\q) Exit mysql. Same as quit.

go (\g) Send command to mysql server.

nopager (\n) Disable pager, print to stdout.

notee (\t) Don't write into outfile.

pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.

print (\p) Print current command.

quit (\q) Quit mysql.

rehash (\#) Rebuild completion hash.

source (\.) Execute a SQL script file. Takes a file name as an argument.

status (\s) Get status information from the server.

tee (\T) Set outfile [to_outfile]. Append everything into given outfile.

use (\u) Use another database. Takes database name as argument.

You can then access a database by using the use command and the name of a database that has been created (such as animals) and to which you are authorized to connect, like this:

mysql> use animals

Database changed

mysql>

The PostgreSQL Command-Line Client

You invoke the PostgreSQL command-line client with the command psql. Like mysql, psql can be invoked with the name of the database to which you would like to connect. Also like mysql, psql can take several options. These options are listed in Table 18.2.

TABLE 18.2 Command-Line Options to Use When Invoking psql

Option Action
-h hostname Connects to the remote host hostname (if the database server isn't located on the local system).
-p n Specifies n as the number of the port to which the client should connect. Note that this is a lowercase p.
-U username Connects to the database as the user username.
-W Prompts for a password after connecting to the database. In PostgreSQL 7 and later, password prompting is automatic if the server requests a password after a connection has been established.
-? Displays a help message.

Several more options are available in addition to those listed in Table 18.2. See the psql's man page for details on all the available options.