Monday, June 4, 2012

MySql at Prompt- I Account Management

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.8-log MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> help;
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
notee     (\t) Don't write into outfile.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an 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.
charset   (\C) Switch to another charset. Might be needed for processing binlog
with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'

mysql> help contents
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
   Account Management
   Administration
   Compound Statements
   Data Definition
   Data Manipulation
   Data Types
   Functions
   Functions and Modifiers for Use with GROUP BY
   Geographic Features
   Help Metadata
   Language Structure
   Plugins
   Table Maintenance
   Transactions
   User-Defined Functions
   Utility

mysql> help account management

You asked for help about help category: "Account Management"
For more information, type 'help <item>', where <item> is one of the following
topics:
   CREATE USER
   DROP USER
   GRANT
   RENAME USER
   REVOKE
   SET PASSWORD

mysql> help create user
Name: 'CREATE USER'
Description:
Syntax:
CREATE USER user_specification
    [, user_specification] ...

user_specification:
    user
    [
        IDENTIFIED BY [PASSWORD] 'password'
      | IDENTIFIED WITH auth_plugin [AS 'auth_string']
    ]

The CREATE USER statement creates new MySQL accounts. To use it, you
must have the global CREATE USER privilege or the INSERT privilege for
the mysql database. For each account, CREATE USER creates a new row in
the mysql.user table and assigns the account no privileges. An error
occurs if the account already exists.

CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass'

If you specify only the user name part of the account name, a host name
part of '%' is used.

The user specification may indicate how the user should authenticate
when connecting to the server:

o To enable the user to connect with no password (which is insecure),
  include no IDENTIFIED BY clause:
Sample 1.1
mysql> create user 'dreamy'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> select user, password from mysql.user;
+--------+----------+
| user   | password |
+--------+----------+
| root   |          |
| root   |          |
| root   |          |
|        |          |
| root   |          |
| dreamy |          |
+--------+----------+
6 rows in set (0.00 sec)

  In this case, the server uses built-in authentication and clients
  must provide no password.

o To assign a password, use IDENTIFIED BY with the literal plaintext
  password value:

Sample 1.2
mysql> create user 'dreamy'@'localhost' identified by 'arun';
Query OK, 0 rows affected (0.00 sec)
mysql> select user, password from mysql.user;
+--------+-------------------------------------------+
| user   | password                                  |
+--------+-------------------------------------------+
| root   |                                           |
| root   |                                           |
| root   |                                           |
|          |                                           |
| dreamy | *F70361020567813A819C38085D2B08A8748C5FC8 |
| root   |                                           |
+--------+-------------------------------------------+
6 rows in set (0.00 sec)

  The server uses built-in authentication and clients must match the
  given password.

o To avoid specifying the plaintext password if you know its hash value
  (the value that PASSWORD() would return for the password), specify
  the hash value preceded by the keyword PASSWORD:

CREATE USER 'jeffrey'@'localhost'
IDENTIFIED BY PASSWORD '*90E462C37378CED12064BB3388827D2BA3A9B689'

 Sample 1.3
mysql> select password('arun');
+-------------------------------------------+
| password('arun')                          |
+-------------------------------------------+
| *F70361020567813A819C38085D2B08A8748C5FC8 |
+-------------------------------------------+
1 row in set (0.00 sec)

mysql> create user 'dreamy'@'localhost' identified by password '*F70361020567813A819C38085D2B08A8748C5FC8';
Query OK, 0 rows affected (0.00 sec)
mysql> select user, password from mysql.user;

+--------+-------------------------------------------+
| user   | password                                  |
+--------+-------------------------------------------+
| root   |                                           |
| root   |                                           |
| root   |                                           |
|        |                                           |
| root   |                                           |
| dreamy | *F70361020567813A819C38085D2B08A8748C5FC8 |
+--------+-------------------------------------------+
6 rows in set (0.00 sec)

  The server uses built-in authentication and clients must match the
  given password.
o If the account should authenticate using a specific authentication
  plugin, use IDENTIFIED WITH. auth_plugin is an authentication plugin
  name. It can be an unquoted name or a quoted string literal.
  'auth_string' is an optional quoted string literal to pass to the
  plugin. The plugin interprets the meaning of the string.

CREATE USER 'jeffrey'@'localhost'
IDENTIFIED WITH my_auth_plugin

  The server uses the named plugin and clients must provide credentials
  as required for the authentication method that the plugin implements.
  If the server cannot find the plugin, an error occurs. IDENTIFIED
  WITH can be given as of MySQL 5.5.7.

The IDENTIFIED BY and IDENTIFIED WITH clauses are mutually exclusive,
so at most one of them can be specified for a given user.


mysql> help drop user
Name: 'DROP USER'
Description:
Syntax:
DROP USER user [, user] ...

The DROP USER statement removes one or more MySQL accounts and their
privileges. It removes privilege rows for the account from all grant
tables. To use this statement, you must have the global CREATE USER
privilege or the DELETE privilege for the mysql database.

DROP USER 'jeffrey'@'localhost'

If you specify only the user name part of the account name, a host name
part of '%' is used.

Sample 1.4
mysql> drop user 'dreamy'@'localhost'; 
Query OK, 0 rows affected (0.01 sec)

mysql> help rename userName: 'RENAME USER'
Description:
Syntax:
RENAME USER old_user TO new_user
    [, old_user TO new_user] ...

The RENAME USER statement renames existing MySQL accounts. To use it,
you must have the global CREATE USER privilege or the UPDATE privilege
for the mysql database. An error occurs if any old account does not
exist or any new account exists. Each account name uses the format
described in http://dev.mysql.com/doc/refman/5.5/en/account-names.html.
For example:

RENAME USER 'jeffrey'@'localhost' TO 'jeff'@'127.0.0.1'

If you specify only the user name part of the account name, a host name
part of '%' is used.

RENAME USER causes the privileges held by the old user to be those held
by the new user. However, RENAME USER does not automatically drop or
invalidate databases or objects within them that the old user created.
This includes stored programs or views for which the DEFINER attribute
names the old user. Attempts to access such objects may produce an
error if they execute in definer security context.

Sample 1.5
mysql> rename  user 'dreamy'@'localhost' to 'kamali'@'localhost';
Query OK, 0 rows affected (0.01 sec)
mysql> select user from mysql.user;
+--------+
| user   |
+--------+
| root   |
| root   |
| root   |
|        |
| kamali |
| root   |
+--------+
6 rows in set (0.00 sec)
mysql> help grant
Name: 'GRANT'
Description:
Syntax:
GRANT
    priv_type [(column_list)]
      [, priv_type [(column_list)]] ...
    ON [object_type] priv_level
    TO user_specification [, user_specification] ...
    [REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]
    [WITH with_option ...]

GRANT PROXY ON user_specification
    TO user_specification [, user_specification] ...
    [WITH GRANT OPTION]

object_type:
    TABLE
  | FUNCTION
  | PROCEDURE

priv_level:
    *
  | *.*
  | db_name.*
  | db_name.tbl_name
  | tbl_name
  | db_name.routine_name

user_specification:
    user
    [
        IDENTIFIED BY [PASSWORD] 'password'
      | IDENTIFIED WITH auth_plugin [AS 'auth_string']
    ]

ssl_option:
    SSL
  | X509
  | CIPHER 'cipher'
  | ISSUER 'issuer'
  | SUBJECT 'subject'

with_option:
    GRANT OPTION
  | MAX_QUERIES_PER_HOUR count
  | MAX_UPDATES_PER_HOUR count
  | MAX_CONNECTIONS_PER_HOUR count
  | MAX_USER_CONNECTIONS count

The GRANT statement grants privileges to MySQL user accounts. GRANT
also serves to specify other account characteristics such as use of
secure connections and limits on access to server resources. To use
GRANT, you must have the GRANT OPTION privilege, and you must have the
privileges that you are granting.

Normally, a database administrator first uses CREATE USER to create an
account, then GRANT to define its privileges and characteristics. For
example:

CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass'
GRANT ALL ON db1.* TO 'jeffrey'@'localhost'
GRANT SELECT ON db2.invoice TO 'jeffrey'@'localhost'
GRANT USAGE ON *.* TO 'jeffrey'@'localhost' WITH MAX_QUERIES_PER_HOUR 90

However, if an account named in a GRANT statement does not already
exist, GRANT may create it under the conditions described later in the
discussion of the NO_AUTO_CREATE_USER SQL mode.

The REVOKE statement is related to GRANT and enables administrators to
remove account privileges. See [HELP REVOKE].

To determine what privileges an account has, use SHOW GRANTS. See [HELP
SHOW GRANTS].


Sample 1.6
mysql> show grants for 'kamali'@'localhost';
+---------------------------------------------------------------------------------------------------------------+
| Grants for kamali@localhost                                                                                   |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'kamali'@'localhost' IDENTIFIED BY PASSWORD '*F70361020567813A819C38085D2B08A8748C5FC8' |
+---------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


Sample 1.7
C:\wamp\bin\mysql\mysql5.5.8\bin>mysql  -h localhost -u kamali -p
Enter password: ****

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec) 
Sample 1.8
mysql> grant select on onlintest.* to 'kamali'@'localhost';
Query OK, 0 rows affected (0.00 sec) 

mysql> show grants for 'kamali'@'localhost';
+---------------------------------------------------------------------------------------------------------------+
| Grants for kamali@localhost                                                                                   |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'kamali'@'localhost' IDENTIFIED BY PASSWORD '*F70361020567813A819C38085D2B08A8748C5FC8' |
| GRANT SELECT ON `onlintest`.* TO 'kamali'@'localhost'                                                         |
+---------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> select db from mysql.db where user='kamali';
+-----------+
| db        |
+-----------+
| onlintest |
+-----------+
1 row in set (0.00 sec)

mysql> grant all on *.* to 'kamali'@'localhost';
Query OK, 0 rows affected (0.00 sec)

Sample 1.9:
mysql> revoke all privileges,grant option from 'kamali'@'localhost';
Query OK, 0 rows affected (0.01 sec)
mysql> show grants for 'kamali'@'localhost';
+---------------------------------------------------------------------------------------------------------------+
| Grants for kamali@localhost                                                                                   |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'kamali'@'localhost' IDENTIFIED BY PASSWORD '*F70361020567813A819C38085D2B08A8748C5FC8' |
+---------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select password('dreamy');
+-------------------------------------------+
| password('dreamy')                        |
+-------------------------------------------+
| *21935FCF3EA4C49338463622BB6F36A8855720FC |
+-------------------------------------------+
1 row in set (0.01 sec)

mysql> select user, password from mysql.user;

+--------+-------------------------------------------+
| user   | password                                  |
+--------+-------------------------------------------+
| root   |                                           |
| root   |                                           |
| root   |                                           |
|        |                                           |
| kamali | *F70361020567813A819C38085D2B08A8748C5FC8 |
| root   |                                           |
+--------+-------------------------------------------+
6 rows in set (0.03 sec)

mysql> use mysql;
Database changed
mysql> update user set password='*21935FCF3EA4C49338463622BB6F36A8855720FC' WHERE user='kamali';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select user, password from mysql.user;

+--------+-------------------------------------------+
| user   | password                                  |
+--------+-------------------------------------------+
| root   |                                           |
| root   |                                           |
| root   |                                           |
|        |                                           |
| kamali | *21935FCF3EA4C49338463622BB6F36A8855720FC |
| root   |                                           |
+--------+-------------------------------------------+
6 rows in set (0.00 sec)