MySQL: A Grab Bag of Useful Bits

Database management tools are nice to have, but they're not essential (I'm talking about you, MySQL Workbench). When the tools aren't working, for whatever reason, you can always go back to basics and create the objects you need from the simple, mysql command-line utility. Here's a grab bag of things you can do...

What User Accounts Exist?

Login to the mysql command-line utility as root. Execute the following commands:

   mysql> use mysql

   Database changed

   mysql> desc user;

   +------------------------+---------------+------+-----+---------+
   | Field                  | Type          | Null | Key | Default |
   +------------------------+---------------+------+-----+---------+
   | 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       |
   | Create_tablespace_priv | enum('N','Y') | NO   |     | N       |
   | ssl_type               | enum('',      | NO   |     | NULL    |
   |                        |  'ANY',       |      |     |         |
   |                        |  'X509',      |      |     |         |
   |                        |  'SPECIFIED') |      |     |         |
   | ssl_cipher             | blob          | NO   |     | NULL    |
   | x509_issuer            | blob          | NO   |     | NULL    |
   | x509_subject           | blob          | NO   |     | NULL    |
   | max_questions          | int(11)       | NO   |     | 0       |
   |                        |   unsigned    |      |     |         |
   | max_updates            | int(11)       | NO   |     | 0       |
   |                        |   unsigned    |      |     |         |
   | max_connections        | int(11)       | NO   |     | 0       |
   |                        |   unsigned    |      |     |         |
   | max_user_connections   | int(11)       | NO   |     | 0       |
   |                        |   unsigned    |      |     |         |
   | plugin                 | char(64)      | YES  |     |         |
   | authentication_string  | text          | YES  |     | NULL    |
   | password_expired       | enum('N','Y') | NO   |     | N       |
   +------------------------+---------------+------+-----+---------+
   43 rows in set (0.01 sec)

   mysql> select host, user from user;

   +--------------------------+------------+
   | host                     | user       |
   +--------------------------+------------+
   | %                        | davekeener |
   | 127.0.0.1                | root       |
   | ::1                      | root       |
   | Davids-MacBook-Pro.local |            |
   | Davids-MacBook-Pro.local | root       |
   | localhost                | root       |
   +--------------------------+------------+
   6 rows in set (0.00 sec)

The mysql schema contains system information, including user accounts, privileges, etc. Once you've switched so that it's your active schema, you can do a "desc" on the USER table to see what columns are available.

The only columns of general interest for our purposes are HOST and USER. That's what the final SQL statement shows for us, revealing that there are 6 user accounts defined.

What Schemas Exist?

In MySQL, a schema, or database, is a unit that can have its own privileges, and which can contain other database objects such as tables, views, etc. You typically can't see schemas that you don't have permissions on, for security reasons. In order to see all of the available schemas, you'll have to login as root.

Login to the mysql command-line utility as root and run the following command:

   mysql> show schemas;

   +--------------------+
   | Database           |
   +--------------------+
   | information_schema |
   | ktech_dev          |
   | mysql              |
   | performance_schema |
   | test               |
   +--------------------+
   5 rows in set (0.00 sec)

Changing a User's Password

Login to the mysql command-line utility as root and run the following command:

   mysql> SET PASSWORD FOR 'dave'@'localhost' = PASSWORD('mvemjsun');
   Query OK, 0 rows affected (0.28 sec)

The change can also be done directly by updating the USER table in the mysql schema, but...really...I don't advise manual updates of system tables.



Comments

No comments yet. Be the first.



Leave a Comment

Comments are moderated and will not appear on the site until reviewed.

(not displayed)