I intend to add to this as I have time.

To display the schema of a table:

mysql> desc tablename;

To restore a database from backup:

mysql -u root -p
mysql> create database databasename;
mysql> use databasename;
mysql> source databasebackupfile;
Note: I back my databases up to a file name of databasename.sql.

To update all records in a column of a table:

mysql> update tablename set columnname=’value‘;

To update one record in a column of a table. Note: othercolumnname needs to be unique to the record.:

mysql> update tablename set columnname=’value‘ where othercolumnname=`somevalue‘;

To create an account:

GRANT ALL PRIVILEGES ON databasename.* TO ‘username‘@’hostname‘ IDENTIFIED BY ‘password‘;
GRANT ALL PRIVILEGES ON databasename.* TO ‘username‘@’localhost’ IDENTIFIED BY ‘password‘;

To make mysqld listen only localhost:

vi /etc/my.cnf


To count the number of rows in a table:

SELECT COUNT(*) FROM tablename;

To remove formatting and heading from a SELECT statement and delimit using a TAB character:

mysql mydatabase -N -B -u myuser -p -e “myselect statement;”

How to convert timestamp to date from a SELECT statement:

SELECT column1,DATE_FORMAT(FROM_UNIXTIME(`column2`), ‘%b %e %Y’) AS ‘date_formatted’ FROM tablename;

Here is a simpler example, where timestampcol is the name of date column you want to convert:

SELECT FROM_UNIXTIME(timestampcol) FROM tablename;

How to get the time an hour ago:


To access a MySql database from a script without having the password stored in the script:
Add the information to the .my.cny file in the user’s home directory that the script will run under.

Edit or create the file:

# vi ~/.my.cny

Protect the file:

# chmod 600 ~/.my.cny

Sample file contents:

# cat ~/.my.cny

Then, to use it:

# mysql < mysqlscript > my.output

Display table information:

show table status from databasename;