Database Reference
In-Depth Information
1.4. Specifying mysql Command Options
Problem
When you invoke the
mysql
program without command options, it exits immediately
with an “access denied” message.
Solution
You must specify connection parameters. Do this on the command line, in an option
file, or using a mix of the two.
Discussion
If you invoke
mysql
with no command options, the result may be an “access denied”
error. To avoid that, connect to the MySQL server as shown in
Recipe 1.1
, using
mysql
like this:
%
mysql -h localhost -u cbuser -p
Enter password:
cbpass
Each option is the single-dash “short” form:
-h
and
-u
to specify the hostname and
username, and
-p
to be prompted for the password. There are also corresponding
double-dash “long” forms:
--host
,
--user
, and
--password
. Use them like this:
%
mysql --host=localhost --user=cbuser --password
Enter password:
cbpass
To see all options that
mysql
supports, use this command:
%
mysql --help
The way you specify command options for
mysql
also applies to other MySQL programs
such as
mysqldump
and
mysqladmin
. For example, to generate a dump file named
cookbook.sql
that contains a backup of the tables in the
cookbook
database, execute
mysqldump
like this:
%
mysqldump -h localhost -u cbuser -p cookbook > cookbook.sql
Enter password:
cbpass
Some operations require an administrative MySQL account. The
mysqladmin
program
can perform operations that are available only to the MySQL
root
account. For example,
to stop the server, invoke
mysqladmin
as follows:
%
mysqladmin -h localhost -u root -p shutdown
Enter password:
← enter MySQL root account password here
If the value that you use for an option is the same as its default value, you can omit the
option. However, there is no default password. If you like, you can specify the password
directly on the command line by using
-p
password
(with
no space
between the option