Database Reference
In-Depth Information
Getting parameters from the command line
The convention used by standard clients such as mysql and mysqladmin for command-
line arguments is to permit parameters to be specified using either a short option or a
long option. For example, the username cbuser can be specified either as -u cbuser (or
-ucbuser ) or --user=cbuser . In addition, for either of the password options ( -p or --
password ), the password value may be omitted after the option name to cause the pro‐
gram to prompt for the password interactively.
The standard flags for these command options are -h or --host , -u or --user , and -p
or --password . You could write your own code to iterate through the argument list, but
it's much easier to use existing option-processing modules written for that purpose.
Under the api directory of the recipes distribution, you'll find example programs that
show how to process command arguments to obtain the hostname, username, and
password for Perl, Ruby, Python, and Java. An accompanying PDF file explains how
each one works.
Insofar as possible, the programs mimic option-handling behavior of
the standard MySQL clients. An exception is that option-processing
libraries may not permit making the password value optional, and
they provide no way of prompting the user for a password interac‐
tively if a password option is specified without a password value.
Consequently, the programs are written so that if you use -p or --
password , you must provide the password value following the option.
Getting parameters from option files
If your API supports it, you can specify connection parameters in a MySQL option file
and let the API read the parameters from the file for you. For APIs that do not support
option files directly, you may be able to arrange to read other types of files in which
parameters are stored or to write your own functions that read option files.
Recipe 1.4 describes the format of MySQL option files. I assume that you've read the
discussion there and concentrate here on how to use option files from within programs.
You can find files containing the code discussed here under the api directory of the
recipes distribution.
Under Unix, user-specific options are specified by convention in ~/.my.cnf (that is, in
the .my.cnf file in your home directory). However, the MySQL option-file mechanism
can look in several different files if they exist, although no option file is required to exist.
(For the list of standard locations in which MySQL programs look for them, see
Recipe 1.4 .) If multiple option files exist and a given parameter is specified in several of
them, the last value found takes precedence.
Programs you write do not use MySQL option files unless you tell them to:
Search WWH ::




Custom Search