The following MySQL Commands were originally split into several smaller blog posts that I had built up over the years, I have now consolidated the articles into a single post (feel free to link to this resource from your site).
Please note this article contains commands & examples for the mysql command line client, it does not contain information for phpMyadmin or similar GUI based software.
MySQL Set Root Password
By default MySQL has no password set, this might be fine for a private development environment but unacceptable for production servers. You can set the mysql root password various ways but below is a nice simple method that works:
1
|
|
Set / Change MySQL Users Passwords from the Linux Shell
1
|
|
You should now be able to restart MySQL and login with your new root password.
How To Connect to MySQL
To connect to your local MySQL server from the command line enter:
1
|
|
If you need to login to a remote MySQL server, you cn either SSH to the server and login or use the following commnd (if the server allows external connections):
1
|
|
MySQL Create Database
The following command will create a new MySQL database:
1
|
|
Backup a MySQL Database using mysqldump
Backing up a MySQL database to a flat file is refered to as “dumping the database”, there are several ways to acomplish this taske here are a few of the methods I use.
Basic mysqldump to a .sql file:
1
|
|
You can also dump the database and compress on the fly by piping it through gzip:
1
|
|
Mysqldump a remote database & transfer over SSH using gzip compression
Note you should execute the following command on the remote server that is currently serving the database, so you are affectivly pushing the db to your local machine.
Mysqldump a remote mysql database to your local machine using SSH & gzip compression (a fast way of taking a backup of a remote database). :
1
|
|
Dump all MySQL Databases on a server
If you wish to dump all databses on a server to a single dump file enter:
1
|
|
Mysqldump & Skip Table(s)
While carrying out a nasty phpBB migration I was faced with the task of dumping a MyISAM databse with some broken tables, you will get an error “mysqldump: Error 1194” or something similar to:
1
|
|
The best option you have if you need to take a backup in it’s current state is to tell mysqldump to skip the tables with:
1
|
|
If you need to skip more than one table you can just add multiple, example below:
1
|
|
Once you have a backup I would recommend repairing the tables.
Dump a specific table from a mysql database
1
|
|
Import a MySQL Database
Simple mysql db import from a .sql file:
1
|
|
Import a mysql database from .sql.gz
1
|
|
Import a .sql file from the mysql command line (you can se the output on the console as it imports, handy if your getting an import error from mysql), first select the mysql database you wish to import into and run:
1
|
|
Select a Database in MySQL
How to select a database in mysql:
1
|
|
Show Tabels in a Database
First select the databse you wish to use and run the following to show tables in a mysql databse:
1
|
|
Create MySQL User
The following example creates a MySQL user called “jesus” with the password “jedimaster”:
1
|
|
Next you need to grant the user permission to access your database:
1
|
|
The above will allow permission for the user “jesus” on the database “heaven_db”.
If you want “jesus” to have access to all databases on the server you would enter:
1
|
|
Show MySQL Database Size
The simple way is to use the filesystem to show the mysql database size on the disk with:
1
|
|
If you need to find out the size of the mysql database from within mysql you could use:
1
|
|
List MySQL Databases
The following will list all mysql databases on a server:
1
|
|
This will give you an output similar to:
1 2 3 4 5 6 7 8 9 10 11 |
|
Drop A MySQL Database (deletes a db)
The following will drop a databases, when you drop a database you are deleting it. Be careful with this command…
1
|
|
Drop a MySQL Table
The following will delete (drop) a mysql table, you need to select the database you wish to use first.
1
|
|
How To Reset the MySQL root password
The following proccess will allow you to reset the mysql root password:
Stop mysql:
1
|
|
Start mysql in safe mode:
1
|
|
Login as root:
1
|
|
Set the mysql root password:
1 2 3 4 |
|
Restart the mysql service and you can login with your new password:
1
|
|
Create a MySQL table
Here is the basic create table syntax for mysql:
1 2 3 4 |
|
Here is a more complex example:
1
|
|
Create an INNODB Table in MySQL
The following will create an innodb table:
1 2 3 4 |
|
Convert MyISAM to INNODB
It goes without saying, backup up your db first before running such a task, but here is the mysql syntax to convert a MyISAM table to INNODB:
1
|
|
Repair Broken Table(s) in MySQL
If you have a a corrupt / broken table (pretty common with MyiSAM) then take a dump (see the skip broken table with mysqldump instructions above) and then run:
1
|
|
Show MySQL Database Fields & Field Formats
1
|
|
MySQL Show Table Data (Displays the contents of a table)
1
|
|
Show Columns in a MySQL Table
1
|
|
Add a new column in MySQL
The following is an example of how to add a new column in mysql:
1
|
|
Delete a Column in MySQL
The following is an example of how to delete (drop) a column in mysql:
1
|
|
Delete a Row from a field
How to delete a row:
1
|
|
Show How Many Rows in a MySQL Table
1
|
|
MySQL Join Tables
How to join tables in MySQL:
1
|
|
MySQL SUM Column Example
1
|
|
Show MySQL & List in Descending Order (DESC)
Show records from col6 and col5 and sort in a descending order using col6:
1
|
|
MySQL Show Records & List in Ascending Order (MySQL ASC)
Show records from col6 and col5 and sort in a ascending order using col6:
1
|
|
MySQL Show Unique Records
Shows all unique records from a mysql table:
1
|
|
Search MySQL Records using a Regular Expression
This regular expression example will show you how to search for MySQL records using regular expressions and the REGXP Binary, the following example will return all results beging with the lower case letter z.
1
|
|
Show Rows Containing a Value
This example will show all rows containing “jesus”:
1
|
|
MySQL Search for a Record Matching (Various Examples)
Search for records with the name “Jesus” born in “1984”:
1
|
|
Search for anyone called “Jesus” with the phone number “911”
1
|
|
Search MySQL for any records matching the name “Jesus” with the phone number “911” and sort by phone number:
1
|
|
Show all records starting with “Jesus” and the phone number “911”:
1
|
|
Do the same as about but only show records 1 to 10:
1
|
|
Feel free to link to this resource from your blog, if you have any suggestions for additional commands please drop me a comment below and I will amend the post.