Saturday, May 16, 2009

Drop user in mysql

In order to drop user in mysql a user issuing drop command must have one of the following two privileges.
-You have the global CREATE USER privilege.
-The DELETE privilege for the mysql database.

1)Before issuing dropping command be sure the user invokes drop command has the privileges by,
mysql> show grants;
+-----------------------------------------------------------------------------------------------------------------------
-----------------+
| Grants for root@localhost
|
+-----------------------------------------------------------------------------------------------------------------------
-----------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29' WITH GRANT OPTION |
+-----------------------------------------------------------------------------------------------------------------------
-----------------+
1 row in set (0.00 sec)

2)
Note that, you might be concerned about REVOKE privilege of the user that you are going to drop specially if your mysql database is before 5.0.2. Before mysql 5.0.2, the granted privileges are not automatically revoked and you must revoke them yourself.

Suppose we want to DROP a user named arju2 in mysql before 5.0.2. Then before dropping arju2 user check the privilege by,
mysql> show grants for arju2@localhost;
+-----------------------------------------------------------------------------------------------------------------------
+
| Grants for arju2@localhost
|
+-----------------------------------------------------------------------------------------------------------------------
+
| GRANT ALL PRIVILEGES ON *.* TO 'arju2'@'localhost' IDENTIFIED BY PASSWORD '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29'
|
+-----------------------------------------------------------------------------------------------------------------------
+
1 row in set (0.00 sec)

Revoke privileges by,
mysql> REVOKE ALL PRIVILEGES ON *.* FROM arju2@localhost;
Query OK, 0 rows affected (0.00 sec)

Check again,
mysql> show grants for arju2@localhost;
+--------------------------------------------------------------------------------------------------------------+
| Grants for arju2@localhost |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'arju2'@'localhost' IDENTIFIED BY PASSWORD '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29' |
+--------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


However if your mysql database version is 5.0.2 onwards then the granted privileges are automatically revoked.

3) Now drop the user.
Note that dropping user by simply drop user username statement will be failed if user's host is assigned to localhost. If you specify only the user name part of the account name, a host name part of '%' is used.

So following statement will be failed as it interpreted as 'arju2'@'%'.
mysql> drop user arju2;
ERROR 1396 (HY000): Operation DROP USER failed for 'arju2'@'%'

Check the host of the user querying from,
mysql> select user, host from user;
+---------+-----------+
| user    | host      |
+---------+-----------+
| magento | %         |
| root    | 127.0.0.1 |
|         | localhost |
| arju2   | localhost |
| magento | localhost |
| momin   | localhost |
| pma     | localhost |
| richard | localhost |
| rober4t | localhost |
| robert  | localhost |
| root    | localhost |
+---------+-----------+
11 rows in set (0.00 sec)

Then drop it by,
mysql> drop user 'arju2'@'localhost';
Query OK, 0 rows affected (0.00 sec)

Note that if user host is defined as '%' then no need to add host name while dropping user. Below is an example.
mysql> create user test;
Query OK, 0 rows affected (0.00 sec)
mysql> select user, host from user where user='test';
+------+------+
| user | host |
+------+------+
| test | %    |
+------+------+
1 row in set (0.00 sec)

mysql> drop user test;
Query OK, 0 rows affected (0.00 sec)

mysql> select user, host from user where user='test';
Empty set (0.00 sec)

Related Documents
http://arjudba.blogspot.com/2009/05/creating-and-using-database-in-mysql.html
http://arjudba.blogspot.com/2009/05/issuing-mysql-commands.html
http://arjudba.blogspot.com/2009/05/connecting-to-and-disconnecting-from.html

Friday, May 15, 2009

Create user in mysql

The mysql user information is kept inside USER table under mysql database. So creation of new user will affect the USER table of mysql database. In order to know a list of mysql users according with user table's description issue,

Here test is the root account password.

D:\xampp\mysql\bin>mysql -u root -ptest
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.30-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use mysql;
Database changed
mysql> desc user;
+-----------------------+-----------------------------------+------+-----+---------+-------+
| Field                 | Type                              | Null | Key | Default | Extra |
+-----------------------+-----------------------------------+------+-----+---------+-------+
| Host                  | char(60)                          | NO   | PRI |         |       |
| User                  | char(16)                          | NO   | PRI |         |       |
| Password              | char(41)                          | NO   |     |         |       |
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |
| Event_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Trigger_priv          | enum('N','Y')                     | NO   |     | N       |       |
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |
| ssl_cipher            | blob                              | NO   |     | NULL    |       |
| x509_issuer           | blob                              | NO   |     | NULL    |       |
| x509_subject          | blob                              | NO   |     | NULL    |       |
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |
+-----------------------+-----------------------------------+------+-----+---------+-------+
39 rows in set (0.14 sec)

mysql> select host, user from user;
+-----------+---------+
| host      | user    |
+-----------+---------+
| %         | magento |
| 127.0.0.1 | root    |
| localhost |         |
| localhost | magento |
| localhost | pma     |
| localhost | root    |
+-----------+---------+
6 rows in set (0.00 sec)

By query the Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv, Shutdown_priv, Process_priv etc you can check whether user has certain types of privilege or not.

There are several ways to create user in mysql which is described below.

Way 01: With the CREATE USER statement
1)Connect to mysql database as a user who must have the global CREATE USER privilege or the INSERT privilege for the mysql database. You can use root because by default root has privilege to create user.

2)In fact CREATE USER statement creates a new record in the mysql.user table that has no privileges assigned by default.

mysql> create user arju;
Query OK, 0 rows affected (0.13 sec)
mysql> select  user,password, host, select_priv, insert_priv from mysql.user where user='arju';
+------+----------+------+-------------+-------------+
| user | password | host | select_priv | insert_priv |
+------+----------+------+-------------+-------------+
| arju |          | %    | N           | N           |
+------+----------+------+-------------+-------------+
1 row in set (0.00 sec)

Notice that if you specify only the user name part of the account name while creating user, a host name part of '%' is used.

User arju is not assigned any password in this way as you see password is null. To assign password to user arju issue,

mysql> set password for 'arju'=password('test');
Query OK, 0 rows affected (0.03 sec)
mysql> select  user,password, host, select_priv, insert_priv from mysql.user where user='arju';
+------+-------------------------------------------+------+-------------+-------------+
| user | password                                  | host | select_priv | insert_priv |
+------+-------------------------------------------+------+-------------+-------------+
| arju | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 | %    | N           | N           |
+------+-------------------------------------------+------+-------------+-------------+
1 row in set (0.00 sec)

In one command you can set password while creating user. The following command will crate a user arju2 and password is test in the host localhost. Both password, username and hostname should be within single quote.

mysql> create user 'arju2'@'localhost' identified by 'test';
Query OK, 0 rows affected (0.00 sec)
mysql> select  user,password, host, select_priv, insert_priv from mysql.user where user='arju2';
+-------+-------------------------------------------+-----------+-------------+-------------+
| user  | password                                  | host      | select_priv | insert_priv |
+-------+-------------------------------------------+-----------+-------------+-------------+
| arju2 | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 | localhost | N           | N           |
+-------+-------------------------------------------+-----------+-------------+-------------+
1 row in set (0.00 sec)

If you don't put username, host name and password within single quote syntax error will be resulted.

mysql> create user 'arju2'@'localhost' identified by test;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test' at line 1

An equivalent statement of setting password is issuing update statement.

mysql> UPDATE mysql.user SET Password=PASSWORD('test')
-> WHERE User='arju' AND Host='%';

Query OK, 0 rows affected (0.05 sec)
Rows matched: 1 Changed: 0 Warnings: 0

Changing via update and insert of the user table, it is necessary to use FLUSH PRIVILEGES to tell the server to reload the grant tables. Otherwise, the changes go unnoticed until you restart the server.

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.08 sec)
mysql> select  user,password, host, select_priv, insert_priv from mysql.user where user='arju';
+------+-------------------------------------------+------+-------------+-------------+
| user | password                                  | host | select_priv | insert_priv |
+------+-------------------------------------------+------+-------------+-------------+
| arju | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 | %    | N           | N           |
+------+-------------------------------------------+------+-------------+-------------+
1 row in set (0.00 sec)

Now, have a look that connecting to mysql database as user arju will fail but arju2 will be successful.
D:\xampp\mysql\bin>mysql -u arju -ptest
ERROR 1045 (28000): Access denied for user 'arju'@'localhost' (using password: YES)

D:\xampp\mysql\bin>mysql -u arju2 -ptest
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.1.30-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> select user();
+-----------------+
| user()          |
+-----------------+
| arju2@localhost |
+-----------------+
1 row in set (0.00 sec)

Note that the 'arju'@'localhost' account can be used only when connecting from the local host. The 'arju'@'%' account uses the '%' wildcard for the host part, so it can be used to connect from any host.

You can check the privilege assigned for a user by issuing,
mysql> show grants for arju;
+-----------------------------------------------------------------------------------------------------+
| Grants for arju@%                                                                                   |
+-----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'arju'@'%' IDENTIFIED BY PASSWORD '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29' |
+-----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show grants for arju2;
ERROR 1141 (42000): There is no such grant defined for user 'arju2' on host '%'
mysql> show grants for arju2@localhost;
+--------------------------------------------------------------------------------------------------------------+
| Grants for arju2@localhost                                                                                   |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'arju2'@'localhost' IDENTIFIED BY PASSWORD '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29' |
+--------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

By default it is search for host '%'.

Way 02: With the grant option
To create a user named momin with password test2 and all privileges for a particular database called ecommerce issue,
mysql> grant ALL PRIVILEGES on ecommerce.* to 'momin'@'localhost' identified by 'test2';
Query OK, 0 rows affected (0.00 sec)

mysql> use mysql;
Database changed
mysql> select host, user from user where user='momin';
+-----------+-------+
| host      | user  |
+-----------+-------+
| localhost | momin |
+-----------+-------+
1 row in set (0.00 sec)

D:\xampp\mysql\bin>mysql -u momin -ptest2
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.1.30-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ecommerce          |
| test               |
+--------------------+
3 rows in set (0.00 sec)

Here ecommerce database is shown as we have given permission.

Way 03: Using INSERT statement into Table mysql.user
You can also create user in mysql by simply INSERT a new row into mysql.user table. Note that creating user through INSERT statement needs to use FLUSH PRIVILEGES to tell the server to reload the grant tables. If we don't do that the changes go unnoticed until you restart the server. This restriction is not applicable in CREATE USER statement.
Insert statement can be issued in two ways. One way to create user robert,

mysql> use mysql;
Database changed

mysql> INSERT INTO user SET Host='localhost',User='robert', Password=password('test');
Query OK, 1 row affected, 3 warnings (0.00 sec)

Alternative way of creating user richard is,

mysql> INSERT INTO user (Host,User,Password)
-> VALUES('localhost','richard',password('test'));

Query OK, 1 row affected, 3 warnings (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

Check by,
mysql> select user, host, password from user where user in('robert','richard');
+---------+-----------+-------------------------------------------+
| user    | host      | password                                  |
+---------+-----------+-------------------------------------------+
| robert  | localhost | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 |
| richard | localhost | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 |
+---------+-----------+-------------------------------------------+
2 rows in set (0.01 sec)

See that while setting password we used the PASSWORD() function with INSERT in order to encrypt the password. But with the CREATE USER statement it was unnecessary as create user statement automatically encrypts the password.

Related Documents
http://arjudba.blogspot.com/2009/05/creating-and-using-database-in-mysql.html
http://arjudba.blogspot.com/2009/05/issuing-mysql-commands.html
http://arjudba.blogspot.com/2009/05/connecting-to-and-disconnecting-from.html

Monday, May 11, 2009

Creating and using database in mysql

In order to know a list of mysql database in the system issue following command where we are connecting as administrative root user with password test and into host localhost. Here we are working on console, not into mysql prompt.
D:\xampp\mysql\bin>mysql -h localhost -u root -ptest  -e "show databases"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| arju               |
| email_master_list  |
| magento            |
| mysql              |
| phpmyadmin         |
| test               |
| webauth            |
+--------------------+

We could do the same job into mysql prompt as,
D:\xampp\mysql\bin>mysql -h localhost -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.30-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| arju               |
| email_master_list  |
| magento            |
| mysql              |
| phpmyadmin         |
| test               |
| webauth            |
+--------------------+
8 rows in set (0.02 sec)

Note that you must login as administrative privilege to see all databases. If you don't do that you may be see all the databases. Like below.

D:\xampp\mysql\bin>mysql -h localhost -sN -e "show databases;"
information_schema
test

Only two databases are shown here.

Now let's try to create a new database named test2. Only administrative privileged user can create the database. So if you don't login as administrative user and issuing create database command the following error will happen.

D:\xampp\mysql\bin>mysql -h localhost -e "create database test2;"
ERROR 1044 (42000) at line 1: Access denied for user ''@'localhost' to database 'test2'

From console try to create database test2 by,
D:\xampp\mysql\bin>mysql -h localhost -u root -ptest -e "create database test2;"

Notice that with password should be specified as -pPassword, i.e adjacent with -p. There is no space between and p and password.

Now check the existing database by,
D:\xampp\mysql\bin>mysql -h localhost  -u root -ptest -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| arju               |
| email_master_list  |
| magento            |
| mysql              |
| phpmyadmin         |
| test               |
| test2              |
| webauth            |
+--------------------+

In the mysql prompt you can create mysql database by,
D:\xampp\mysql\bin>mysql -h localhost  -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.1.30-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database test3;
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| arju               |
| email_master_list  |
| magento            |
| mysql              |
| phpmyadmin         |
| test               |
| test2              |
| test3              |
| webauth            |
+--------------------+
10 rows in set (0.00 sec)

In order to connect to a database issue connect database_name or use database_name statement. Ensure the database name by command select database() as shown below.
mysql> connect test3;
Connection id:    17
Current database: test3

mysql> select database();
+------------+
| database() |
+------------+
| test3      |
+------------+
1 row in set (0.00 sec)

mysql> use test2;
Database changed

mysql> select database();
+------------+
| database() |
+------------+
| test2      |
+------------+
1 row in set (0.00 sec)

Related Documents
http://arjudba.blogspot.com/2009/05/issuing-mysql-commands.html
http://arjudba.blogspot.com/2009/05/connecting-to-and-disconnecting-from.html

Sunday, May 10, 2009

Issuing MySQL commands

After connecting to mysql as it is described in http://arjudba.blogspot.com/2009/05/connecting-to-and-disconnecting-from.html you can issue mysql commands within mysql> prompt.

Below is a simple example which will print mysql version number.

D:\xampp\mysql\bin>mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.30-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> select version();
+------------------+
| version()        |
+------------------+
| 5.1.30-community |
+------------------+
1 row in set (0.06 sec)

From the query and query about let's discuss about some default characters of issuing mysql commands and it's output.

1)MySQL commands end with semicolon. An exception is quit command.

2)After command is executed in server mysql shows mysql> prompt in order to indicate that it is suitable to take another input.

3)mysql displays query output in tabular form (rows and columns) by default.

4)After query is executed, it displays number of rows returned by the query as well as clock time needed for the query.

Let's now see another example.
mysql> select version()
-> ,
-> 'USER()
'> '
-> ,
-> "NOW()
"> "
-> ;
+------------------+---------+--------+
| version()        | USER()
| NOW()
|
+------------------+---------+--------+
| 5.1.30-community | USER()
| NOW()
|
+------------------+---------+--------+
1 row in set (0.00 sec)
mysql>

It displays version number information and then displays two strings 'USER()' and "NOW()". But look at the mysql prompt. It changes from -> to '>, -> to "> , -> to mysql> etc. That prompt indicates that it is waiting for a letters(s) that mysql expects.

Following is the list of mysql prompts with their meaning.

1)mysql> : Mysql is ready to accept commands.

2)-> : Mysql waits for next line of multiple-line command.

3)'> : Waiting for next line plus waiting for completion of a string that began with a single quote (').

4)"> : Waiting for next line plus waiting for completion of a string that began with a double quote (").

5)`> : Waiting for next line plus waiting for completion of an identifier that began with a backtick (`).

6)/*> : Waiting for next line plus waiting for completion of a comment that began with /*.

Related Documents
http://arjudba.blogspot.com/2009/05/connecting-to-and-disconnecting-from.html

Connecting to and disconnecting from MySQL

With the MySQL server installed and MySQL server daemon (Unix) or MySQL service (Windows) running you can connect to mysql database.

In order to know a list of mysql commands on your console, go to mysql installation directory and then into bin folder. Writing mysql --help you provide of a list of commands which you can use with mysql command. Output is shown below.

D:\xampp\mysql\bin>mysql --help
mysql  Ver 14.14 Distrib 5.1.30, for Win32 (ia32)
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
Usage: mysql [OPTIONS] [database]
-?, --help          Display this help and exit.
-I, --help          Synonym for -?
--auto-rehash       Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
and reconnecting may take a longer time. Disable with
--disable-auto-rehash.
-A, --no-auto-rehash
No automatic rehashing. One has to use 'rehash' to get
table and field completion. This gives a quicker start of
mysql and disables rehashing on reconnect. WARNING:
options deprecated; use --disable-auto-rehash instead.
-B, --batch         Don't use history file. Disable interactive behavior.
(Enables --silent)
--character-sets-dir=name
Directory where character sets are.
--column-type-info  Display column type information.
-c, --comments      Preserve comments. Send comments to the server. The
default is --skip-comments (discard comments), enable
with --comments
-C, --compress      Use compression in server/client protocol.
-#, --debug[=#]     This is a non-debug version. Catch this and exit
--debug-check       Check memory and open file usage at exit .
-T, --debug-info    Print some debug info at exit.
-D, --database=name Database to use.
--default-character-set=name
Set the default character set.
--delimiter=name    Delimiter to be used.
-e, --execute=name  Execute command and quit. (Disables --force and history
file)
-E, --vertical      Print the output of a query (rows) vertically.
-f, --force         Continue even if we get an sql error.
-G, --named-commands
Enable named commands. Named commands mean this program's
internal commands; see mysql> help . When enabled, the
named commands can be used from any line of the query,
otherwise only from the first line, before an enter.
Disable with --disable-named-commands. This option is
disabled by default.
-g, --no-named-commands
Named commands are disabled. Use \* form only, or use
named commands only in the beginning of a line ending
with a semicolon (;) Since version 10.9 the client now
starts with this option ENABLED by default! Disable with
'-G'. Long format commands still work from the first
line. WARNING: option deprecated; use
--disable-named-commands instead.
-i, --ignore-spaces Ignore space after function names.
--local-infile      Enable/disable LOAD DATA LOCAL INFILE.
-b, --no-beep       Turn off beep on error.
-h, --host=name     Connect to host.
-H, --html          Produce HTML output.
-X, --xml           Produce XML output
--line-numbers      Write line numbers for errors.
-L, --skip-line-numbers
Don't write line number for errors. WARNING: -L is
deprecated, use long version of this option instead.
-n, --unbuffered    Flush buffer after each query.
--column-names      Write column names in results.
-N, --skip-column-names
Don't write column names in results. WARNING: -N is
deprecated, use long version of this options instead.
-O, --set-variable=name
Change the value of a variable. Please note that this
option is deprecated; you can set variables directly with
--variable-name=value.
--sigint-ignore     Ignore SIGINT (CTRL-C)
-o, --one-database  Only update the default database. This is useful for
skipping updates to other database in the update log.
-p, --password[=name]
Password to use when connecting to server. If password is
not given it's asked from the tty.
-W, --pipe          Use named pipes to connect to server.
-P, --port=#        Port number to use for connection or 0 for default to, in
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/services, built-in default (3306).
--prompt=name       Set the mysql prompt to this value.
--protocol=name     The protocol of connection (tcp,socket,pipe,memory).
-q, --quick         Don't cache result, print it row by row. This may slow
down the server if the output is suspended. Doesn't use
history file.
-r, --raw           Write fields without conversion. Used with --batch.
--reconnect         Reconnect if the connection is lost. Disable with
--disable-reconnect. This option is enabled by default.
-s, --silent        Be more silent. Print results with a tab as separator,
each row on new line.
--shared-memory-base-name=name
Base name of shared memory.
-S, --socket=name   Socket file to use for connection.
--ssl               Enable SSL for connection (automatically enabled with
other flags). Disable with --skip-ssl.
--ssl-ca=name       CA file in PEM format (check OpenSSL docs, implies
--ssl).
--ssl-capath=name   CA directory (check OpenSSL docs, implies --ssl).
--ssl-cert=name     X509 cert in PEM format (implies --ssl).
--ssl-cipher=name   SSL cipher to use (implies --ssl).
--ssl-key=name      X509 key in PEM format (implies --ssl).
--ssl-verify-server-cert
Verify server's "Common Name" in its cert against
hostname used when connecting. This option is disabled by
default.
-t, --table         Output in table format.
--tee=name          Append everything into outfile. See interactive help (\h)
also. Does not work in batch mode. Disable with
--disable-tee. This option is disabled by default.
--no-tee            Disable outfile. See interactive help (\h) also. WARNING:
option deprecated; use --disable-tee instead
-u, --user=name     User for login if not current user.
-U, --safe-updates  Only allow UPDATE and DELETE that uses keys.
-U, --i-am-a-dummy  Synonym for option --safe-updates, -U.
-v, --verbose       Write more. (-v -v -v gives the table output format).
-V, --version       Output version information and exit.
-w, --wait          Wait and retry if connection is down.
--connect_timeout=# Number of seconds before connection timeout.
--max_allowed_packet=#
Max packet length to send to, or receive from server
--net_buffer_length=#
Buffer for TCP/IP and socket communication
--select_limit=#    Automatic limit for SELECT when using --safe-updates
--max_join_size=#   Automatic limit for rows in a join when using
--safe-updates
--secure-auth       Refuse client connecting to server if it uses old
(pre-4.1.1) protocol
--server-arg=name   Send embedded server this as a parameter.
--show-warnings     Show warnings after every statement.

Default options are read from the following files in the given order:
E:\WINDOWS\my.ini E:\WINDOWS\my.cnf C:\my.ini C:\my.cnf D:\xampp\mysql\my.ini D:\xampp\mysql\my.cnf
The following groups are read: mysql client
The following options may be given as the first argument:
--print-defaults        Print the program argument list and exit
--no-defaults           Don't read default options from any options file
--defaults-file=#       Only read default options from the given file #
--defaults-extra-file=# Read this file after the global files are read

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}  Value (after reading options)
--------------------------------- -----------------------------
auto-rehash                       TRUE
character-sets-dir                (No default value)
column-type-info                  FALSE
comments                          FALSE
compress                          FALSE
debug-check                       FALSE
debug-info                        FALSE
database                          (No default value)
default-character-set             latin1
delimiter                         ;
vertical                          FALSE
force                             FALSE
named-commands                    FALSE
local-infile                      FALSE
no-beep                           FALSE
host                              (No default value)
html                              FALSE
xml                               FALSE
line-numbers                      TRUE
unbuffered                        FALSE
column-names                      TRUE
sigint-ignore                     FALSE
port                              0
prompt                            mysql>
quick                             FALSE
raw                               FALSE
reconnect                         TRUE
shared-memory-base-name           (No default value)
socket                            (No default value)
ssl                               FALSE
ssl-ca                            (No default value)
ssl-capath                        (No default value)
ssl-cert                          (No default value)
ssl-cipher                        (No default value)
ssl-key                           (No default value)
ssl-verify-server-cert            FALSE
table                             FALSE
user                              (No default value)
safe-updates                      FALSE
i-am-a-dummy                      FALSE
connect_timeout                   0
max_allowed_packet                16777216
net_buffer_length                 16384
select_limit                      1000
max_join_size                     1000000
secure-auth                       FALSE
show-warnings                     FALSE

Note that if the MySQL server daemon (Unix) or service (Windows) is not running, then you would get the error below.

# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

If daemon or service is running then to connect into mysql database into host localhost as user root issue,
D:\xampp\mysql\bin>mysql -h localhost -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.30-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

If you see mysql> prompt as above you are connected to mysql database. And now you can query as you wish. Note that also from shell prompt or console you can query from mysql database.

For example to know the list of mysql database issue,
D:\xampp\mysql\bin>mysql -h localhost -u root -ptest -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| arju |
| email_master_list |
| magento |
| mysql |
| phpmyadmin |
| test |
| webauth |
+--------------------+

Sometimes you can want to remove the column heading. Then use as,

D:\xampp\mysql\bin>mysql -h localhost -u root -ptest -sN -e "show databases;"
information_schema
arju
email_master_list
magento
mysql
phpmyadmin
test
webauth

In order to disconnect from mysql prompt issue exit. Bye will be printed on the screen and it will go to console prompt.
mysql> exit
Bye

D:\xampp\mysql\bin>

On windows you can also exit by pressing CTRL+C in mysql prompt.
On unix press CTRL+D .