MySQL Notes

The following is a list of commands for setting up and using a MySQL database.

Add MySQL to your PATH

PATH=$PATH:/c/Program\ Files/MySQL/bin; export PATH
        

Logging Into The Database

To log in as the mySQL administrator do (by default 'root' has no password):
mysql -h localhost -u root
        

Changing the root password

mysql> set password for root@localhost=PASSWORD('new_password');
        
or from a shell prompt run:
mysqladmin -u root password new_password
        

Logging In as a Regular User

mysql -h localhost -u username -p (you'll be prompted for the password)

mysql -h localhost -u username -p[password] (no space before password)

mysql -h localhost -u username -p[password] [db_name]
        

Determine MySQL Version

mysql> select version(), current_date;
        

Showing Current User

This displays the name of the user that is currently logged in.
mysql> select user(), current_date;
        

Showing all users

mysql> select host, user from mysql.user;
        

Listing the databases

mysql> show databases;
        

Selecting a Database

mysql> use [db_name]
        
Note that no semi-colon is needed at the end of the statement.

Creating a Database

mysql> create database dbName;
        

List All Tables

mysql> show tables;
        

Describe a Table

mysql> describe [table_name];
        

Add New Users To MySQL

mysql> grant all privileges
    -> on dbName.*
    -> to username@localhost
    -> identified by 'password';

mysql> flush privileges;
        

View Privileges For a User

mysql> show grants for userid;
        

Login As a User

mysql -h localhost -u username -p dbName
        
or
mysql -h localhost -u username -ppassword dbName
        

Run a SQL Script

To run a SQL script from the command line:
mysql -h localhost -u platform -p dbName < CreateAllTables.mysql.sql
        

Out Of Space Problems

Modify c:\my.ini (or my.cnf). According to the MySQL documentation, the solution to this problem is to add the following line to your \Windows\my.ini file:
innodb_data_file_path = ibdata1:20M:autoextend 
        

Delete The Anonymous User

mysql -u root -ppassword
use mysql
delete from User where User = "";