TweetFollow Us on Twitter

Advanced MySQL Administration

Volume Number: 25
Issue Number: 11
Column Tag: MySQL

Advanced MySQL Administration

How to use some more advanced MySQL features

by Mihalis Tsoukalos


MySQL is a very popular database. When first introduced, MySQL was considered to be a simplistic database but now things have changed! A lot of advanced features have been added to MySQL, making it everything but a simplistic database.

This article is going to present some advanced MySQL features along with their relevant commands.

This article assumes that you already have basic MySQL knowledge. Nevertheless, the commands presented here can be used by every MySQL DBA or programmer as they are practical and easy to follow.

Advanced Administration commands

The first part of this article is going to present some advanced commands for MySQL administration.

Check if the MySQL server process is already running

The simplest way to check if MySQL is already running is to use the following UNIX command from (or a similar application):

$ ps -axww | grep -i mysqld

I tried the above command on a PowerMac running Mac OS X 10.4 and MySQL 5.0.16. If you get an output that looks like the following, then MySQL is already running:

6040  p1  S      0:00.03 /bin/sh /sw/bin/mysqld_safe -datadir=/sw/var/mysql -pid-file=/sw/var/mysql/
 6060  p1  S      0:02.15 /sw/sbin/mysqld -basedir=/sw -datadir=/sw/var/mysql -user=mysql -pid-file=/sw/var/mysql/

On a Leopard machine running MySQL 5.0.67, I got the following output:

92942 ttys001    0:00.02 /bin/sh /opt/local/lib/mysql5/bin/mysqld_safe -datadir=/opt/local/var/db/mysql5 -pid-file=/opt/local/var/db/mysql5/
92970 ttys001    
0:52.26 /opt/local/libexec/mysqld -basedir=/opt/local -datadir=/opt/local/var/db/mysql5 -user=mysql
-pid-file=/opt/local/var/db/mysql5/ -port=3306 -socket=/opt/local/var/run/mysql5/mysqld.sock

As you may understand, the biggest difference between the two outputs is that the first is from a MySQL database that was installed using fink, whereas the second was installed using macports.

Launching MySQL at startup

It would be very nice if MySQL could automatically run at startup J Well, macports (my preferred MySQL installation–yours may vary a little) has an easy way of doing this, which only requires running the following command once:

sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

Now, every time your reboot your Mac OS X, MySQL will automatically start!

Where MySQL saves its actual table data

Again, on my default macports installation, the actual table data is stored inside /opt/local/var/db/mysql5.

Where MySQL saves its logs

As it happens with every respectable database, MySQL has log files. Log files are the perfect place to find information about MySQL startup, shutdown, troubleshooting and error messages. The default place for finding the default MySQL log file is inside your data directory. The log file is called <server_name>.err and you can change its location using the --log-error=<filename> command line option during MySQL startup.

Using the --log command line option, you can make MySQL create an additional log file (again inside the data directory) that saves data about both every connection made and every statement received. For example, giving the "use SA" command as the root MySQL user adds the following lines in the general query log file:

070505 20:55:26       1 Connect     root@localhost on 
070505 20:55:28       1 Query       SELECT DATABASE()
                      1 Init DB     sa
                      1 Query       show databases
                      1 Query       show tables
                      1 Field List  NetData 
                      1 Field List  NetData_backup 
                      1 Field List  NetData_identical 
                      1 Field List  Populate 
                      1 Field List  deadlock 
                      1 Field List  delete_me 
                      1 Field List  t1 
                      1 Field List  t2 
                      1 Field List  test 

The default filename for the general query log file is <hostname>.log.

There is another log file called "Slow Query Log" that will be created when using the -log-slow-query command line option when starting up MySQL. Its default filename is <hostname>-slow.log, its default directory is the data directory, and it saves information about queries that are slower than a given threshold. The threshold can be set using the -long-query-time=<seconds> command line option.

MySQL Deadlocks

First of all, let me introduce you to deadlocks. A deadlock happens when two or more transactions want to change the same resource at the same time, ending up waiting for each other to commit so that they can complete their own change. Deadlocks are pretty common to busy database servers.

The following MySQL commands (using two users, that is creating two transactions) create a deadlock:

USER 1 (MySQL admin)


(this is just an example - usually deadlocks do not happen that way)

USER 2 (starts typing after USER 1 finishes typing her command)

mysql> UPDATE NetData
   -> SET sourcePort = "80"
   -> WHERE sourcePort = "http";

The last command will not return. If you close the connection of USER 1, you will get an output that looks similar to:

Query OK, 2472 rows affected (36 min 11.25 sec)
Rows matched: 2472  Changed: 2472  Warnings: 0

Generating sample data

In order to be able to follow this section of the article you need to install the Super Smack package ( After installing it, you will be able to run the gen-data executable that is used for generating sample data.

Sample data can be useful for the following purposes:

Testing your database

Debugging your database

Evaluating the performance of your database

Testing the import process of "real" data

Now that you will also agree that sample data is useful, imagine that you have the following table (called Populate) that you want to populate with sample data:

mysql> desc Populate;
| Field     | Type        | Null | Key | Default | Extra |
| Name      | varchar(50) | YES  |     | NULL    |       |
| SurName   | varchar(50) | NO   |     |         |       |
| Telephone | varchar(20) | YES  |     | NULL    |       |
| Id        | int(11)     | YES  |     | NULL    |       |
4 rows in set (0.03 sec)

The following UNIX shell command will create 1000 rows of sample data for it:

$ gen-data -n 1000 -f %20-12s,%15-25s,%5-s-%d,%n | head

Note: You have to write a small script in your favorite scripting language to insert the sample data in your MySQL database.

As you can easily understand, the information found in this section can be used not only in MySQL but everywhere you may need "realistic" data for testing purposes.


The SHOW TABLE STATUS command is very informative because it displays useful data per table. A part of its output is the following:

mysql> show table status \G

*************************** 1. row *************************
           Name: NetData
         Engine: MyISAM
        Version: 10
     Row_format: Dynamic
           Rows: 4912
 Avg_row_length: 28
    Data_length: 138520
Max_data_length: 281474976710655
   Index_length: 1024
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2007-03-14 10:02:39
    Update_time: 2007-04-12 10:06:14
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
*************************** 2. row ************************
           Name: NetData_backup

Use this command when you want to have a general overview of the tables found in a MySQL database.


The SHOW FULL PROCESSLIST command is very useful for finding out what is running in your database server. The following output is from the deadlock example:

*************************** 1. row *************************
     Id: 7
   User: root
   Host: localhost
     db: sa
Command: Query
   Time: 97
  State: Locked
   Info: UPDATE NetData SET sourcePort = "http" WHERE sourcePort = "80"
*************************** 2. row *************************
     Id: 8
   User: root
   Host: localhost
     db: sa
Command: Query
   Time: 0
  State: NULL
   Info: show full processlist
2 rows in set (0.00 sec)

Finding the privileges of a user

If you want to find out the privileges of a user ("mtsouk" in this example), you can use the SHOW GRANTS command:

mysql> mysql> SHOW GRANTS for 'mtsouk'@'localhost' \G
*************************** 1. row *************************
Grants for mtsouk@localhost: GRANT USAGE ON *.* TO 'mtsouk'@'localhost' IDENTIFIED BY PASSWORD '663c5dd53dae4ed0'
*************************** 2. row *************************
*************************** 3. row *************************
3 rows in set (0.01 sec)

Notice the \G string (two characters) at the end of the command. When you have long rows, \G improves the output making it easier to read and understand. Try the same command without the \G and you will understand what I am talking about.

The EXPLAIN command

Those of you who are interested in or familiar with database theory and/or performance issues will find the EXPLAIN command particularly interesting and handy. First, let me show you an example:

mysql> EXPLAIN select count(*) from NetData \G
************************** 1. row **************************
           id: 1
  select_type: SIMPLE
        table: NULL
         type: NULL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: NULL
        Extra: Select tables optimized away
1 row in set (0.00 sec)

The output of the EXPLAIN command provides extremely useful information such as how MySQL thinks it is best to execute the given SQL statement, the PRIMARY and SECONDARY keys used, the indices used, etc. Put simply, a database has many ways of executing a query; the more complicated the query is, the more (equivalent) ways there are to execute it. The EXPLAIN command helps you determine if MySQL has chosen an optimal method for accessing its data.

If you think that your query is slow, try the EXPLAIN command as it may help you explain some of the reasons of its slow execution.

Creating an empty table that is identical to an existing table but without the table data

The following command will create a new table called NEW_TABLE that looks like the original OLD_TABLE table without its actual data:

select * from OLD_TABLE
   where 0 = 1;

Creating a backup copy of an existing table

The following output illustrates how to create a backup copy of the NetData table:

mysql> desc NetData;
| Field      | Type        | Null | Key | Default | Extra|
| Date       | datetime    | NO   |     |         |       |
| dt         | float       | YES  |     | NULL    |       |
| sourcePort | varchar(12) | YES  |     | NULL    |       |
| destPort   | varchar(12) | YES  |     | NULL    |       |
4 rows in set (0.04 sec)
mysql> create table NetData_backup as select * from NetData;
Query OK, 4912 rows affected (0.02 sec)
Records: 4912  Duplicates: 0  Warnings: 0

Returning random rows

The MySQL rand() function can be used to return rows randomly. The following command returns 5 random rows:

select * from NetData
   order by rand()
   limit 3;

Two possible outputs are:

| Date               | dt       | sourcePort | destPort |
| 2007-01-27 15:12:11 | 0.000369 | 3144       | http     |
| 2007-01-27 15:15:02 | 0.000323 | http       | 4223     |
| 2007-01-27 15:09:04 | 0.000371 | 2610       | http     |
3 rows in set (0.14 sec)
| Date                | dt      | sourcePort | destPort |
| 2007-01-27 15:04:19  | 0.000513 | smtp       | 1108     |
| 2007-01-27 15:06:08  | 0.003117 | 2178       | http     |
| 2007-01-27 15:07:13  | 0.000183 | http       | 2590     |
3 rows in set (0.01 sec)

Running multiple MySQL server processes on a single machine

A lesser-known feature of MySQL is the possibility of running multiple server processes on a single machine. It is very handy when you want to run an experimental MySQL installation or you do not have a second machine to try things. The difficult part is that you must manually specify certain parameters on the command line.

The following example (using a fink MySQL installation) starts a second instance of MySQL that uses port 3305 (the default port of MySQL server is 3306–you can verify it by typing "telnet localhost 3306"), stores its data inside the /tmp/mysql directory and is a console application:

sudo /sw/sbin/mysqld -port 3305 -datadir="/tmp/mysql" -console -user=mysql

Using this method, you can start as many MySQL server processes as you want. Keep in mind that the datadir directory must be both readable and writable by the MySQL user.

In order to connect to the above MySQL instance you have to run the MySQL client using the -port 3305 parameter.

Getting the MySQL source code

MySQL has a great advantage over databases such as IBM DB2 or Oracle; you can get, view or change its source code according to your needs, provided that you know what you are doing.

If you have OS X Server, MySQL binaries are installed as part of the OS. OS X needs to have MySQL installed, however. Even on OS X Server, you may want to run a different version than that supplied or be able to tweak the code yourself. There are three main ways of getting the MySQL source code:

a. Using the BitKeeper revision control application (the modern way)

b. Downloading the source code of a specific version release (the traditional way)

c. By telling fink or macports to get the code for you (the Mac way!)

In the Web Links and Bibliography section you will find more information about getting the MySQL source code.

Notice: it is truly useful to try to understand the MySQL source code by reading it; especially if you are familiar with the C programming language. People that have research interests in the database field will also benefit from reading the MySQL source code in order to learn more about database internals, execution plans, optimization techniques, cost estimation, etc.

Backup and Recovery

Two essential tasks of every database are backup and recovery.

Backing up a MySQL database

Let us say that you want to backup all the tables in the SA database. The following UNIX shell command will do what you want:

$ mysqldump SA -u root -p > sa_backup.sql
Enter password: xxxxx
$ ll sa_backup.sql 
-rw-r-r-    1 mtsouk   mtsouk     455563 Mar 14 09:54 sa_backup.sql
$ head -n 5 sa_backup.sql 
- MySQL dump 10.10
- Host: localhost    Database: SA
- ---------------------------
- Server version       5.0.16

SA is the name of the database that we want to backup, whereas sa_backup_sql is a text file that holds the necessary SQL commands that can restore the SA database.

Restoring a MySQL database

In order to restore a MySQL database, you can use the following way (again, from a UNIX shell):

$ mysql SA -u root -p < sa_backup.sql

It is highly advisable to check your backup strategy from time to time in order to make sure that everything works as expected. If you do not have a reliable backup strategy, you may get into trouble if you ever need to make a restore of you database.

Replication in MySQL

Replication is the creation of an identical database (as the one you have), in an alternative place.

Replication, a feature used to be found in advanced databases, is also available in MySQL. There are two types of replication: synchronous and asynchronous. MySQL implements asynchronous replication: data is transferred to the replicated systems with a small delay (lag).

Binary logs are utilized for the replication process to work. Everything that is changing the data in the master database is being written in a binary log. A process in the master database reads this binary log and transfers queries to a special log file in the slave database, called relay log. Another process in the slave database reads the relay log and executes the queries found.

The MySQL Data Dictionary

The tables that hold data about the database itself compose the Data Dictionary of the database. This part of the article will present some of them.

The mysql.user table

This table holds important information about the users of the database. It is a very critical MySQL table that DBAs are using all the time. To understand the contents of this table, use the desc (describe) command:

mysql> use mysql
mysql> desc mysql.user;


The INFORMATION_SCHEMA.TABLES table holds metadata about the database tables on the server.



The INFORMATION_SCHEMA.COLUMNS contains information about the columns found in the tables of the MySQL server.


Every database contains lots of tables and views in its data dictionary. This part of the article presented a tiny part of the MySQL data dictionary in order to give you an idea of how those tables or views look.

Benchmarking MySQL

Benchmarking is the process of creating performance results for some given testing scenarios. The results are then used for the following purposes:

a. To compare your system configuration with other systems

b. To help you understand the performance impact of changes you made to the database

c. To find the limits of your system

d. To find the problematic parts of your system

MySQL offers a benchmarking suite. Super Smack can also help you benchmark your MySQL database (See below).

The following is the output of the MySQL benchmarking suite using a normal MySQL user:

root# ./run-all-tests -pass="xyzw" -user="mtsouk"
Benchmark DBD suite: 2.15
Date of test:        2007-05-07 22:58:41
Running tests on:    Darwin 8.9.0 Power Macintosh
Limits from:         
Server version:      MySQL 5.0.16/
Optimization:        None
alter-table:    Total time: 12 wallclock secs    ( 0.08 usr  0.03 sys +  0.00 cusr  0.00 csys =  0.11 CPU)
ATIS:          Total time: 31 wallclock secs    ( 8.65 usr  6.06 sys +  0.00 cusr  0.00 csys = 14.71 CPU)
big-tables:    Total time: 34 wallclock secs    ( 7.09 usr 13.08 sys +  0.00 cusr  0.00 csys = 20.17 CPU)
connect:       Total time: 185 wallclock secs    (46.70 usr 44.82 sys +  0.00 cusr  0.00 csys = 91.52 CPU)
create:       Total time: 190 wallclock secs    ( 9.58 usr  3.06 sys +  0.00 cusr  0.00 csys = 12.64 CPU)
insert:       Total time: 1668 wallclock secs    (467.41 usr 262.61 sys +  0.00 cusr  0.00 csys = 730.02 CPU)
select:       Total time: 634 wallclock secs    (46.81 usr 23.97 sys +  0.00 cusr  0.00 csys = 70.78 CPU)
transactions:  Test skipped because the database doesn't support transactions
wisconsin:       Total time: 13 wallclock secs    ( 3.11 usr  1.91 sys +  0.00 cusr  0.00 csys =  5.02 CPU)
All 9 test executed successfully
Totals per operation:
Operation           seconds     usr     sys     cpu   tests
alter_table_add      5.00    0.03    0.01    0.04     100 
alter_table_drop     5.00    0.02    0.00    0.02      91 
connect                               13.00    6.13    1.73    7.86   10000 
[many benchmarks omitted]
wisc_benchmark       4.00    1.87    1.11    2.98     114 
TOTALS           2755.00  579.87  349.13   929.00  3425950 

The output may look complicated at first sight, but should become more meaningful to you if you run the tests in different platforms, using various operating systems or if you change the amount of memory in your system.

Note: On my PowerMac G5 machine using a fink MySQL installation, the location of the run-all-tests script is the /sw/share/mysql/sql-bench directory. Yours may vary.


You can surely understand that this article could not cover everything about advanced MySQL administration. Its main purpose is to give you information about the capabilities of MySQL that you should further search and experiment with. Despite that, you learned many advanced MySQL commands in this article that you can use in every day tasks and make your DBA and programming life easier.

Web Links and Bibliography

Super Smack package:


Available MySQL source trees:

MySQL source code:

Charles A. Bell, Expert MySQL, APress, 2007

Michael Kruckenberd and Jay Pipes, Pro MySQL, Apress, 2005

Russell Dyer, MySQL in a Nutshell, O'Reilly, 2005.

MySQL Reference Manual:

Chad Russell and Jon Stephens. Beginning MySQL Database Design and Optimization: From Novice to Professional. Apress, 2004.


Control MySQL server via launchd (manually creating the required files):

Mihalis Tsoukalos enjoys digital photography, writing articles and programming iPhone apps. He is the author of the "Programming Dashboard Widgets" eBook. You can reach him at


Community Search:
MacTech Search:

Software Updates via MacUpdate

World of Tanks Generals guide - Tips and...
World of Tanks Generals is a brand new card game by the developer behind the World of Tanks shooter franchise. It plays like a cross between chess and your typical card game. You have to keep in consideration where you place your tanks on the board... | Read more »
TruckSimulation 16 guide: How to succeed...
Remember those strangely enjoyable truck missions in Grand Theft Auto V whereit was a disturbing amount of fun to deliver cargo? TruckSimulation 16 is reminiscent of that, and has you play the role of a truck driver who has to deliver various... | Read more »
The best GIF making apps
Animated GIFs have exploded in popularity recently which is likely thanks to a combination of Tumblr, our shorter attention spans, and the simple fact they’re a lot of fun. [Read more] | Read more »
The best remote desktop apps for iOS
We've been sifting through the App Store to find the best ways to do computer tasks on a tablet. That gave us a thought - what if we could just do computer tasks from our tablets? Here's a list of the best remote desktop apps to help you use your... | Read more »
Warhammer 40,000: Freeblade guide - How...
Warhammer 40,000: Freebladejust launched in the App Store and it lets you live your childhood dream of blowing up and slashing a bunch of enemies as a massive, hulking Space Marine. It's not easy being a Space Marine though - and particularly if... | Read more »
Gopogo guide - How to bounce like the be...
Nitrome just launched a new game and, as to be expected, it's a lot of addictive fun. It's called Gopogo, and it challenges you to hoparound a bunch of platforms, avoiding enemies and picking up shiny stuff. It's not easy though - just like the... | Read more »
Sago Mini Superhero (Education)
Sago Mini Superhero 1.0 Device: iOS Universal Category: Education Price: $2.99, Version: 1.0 (iTunes) Description: KAPOW! Jack the rabbit bursts into the sky as the Sago Mini Superhero! Fly with Jack as he lifts impossible weights,... | Read more »
Star Wars: Galaxy of Heroes guide - How...
Star Wars: Galaxy of Heroes is all about collecting heroes, powering them up, and using them together to defeat your foes. It's pretty straightforward stuff for the most part, but increasing your characters' stats can be a bit confusing because it... | Read more »
The best cooking apps (just in time for...
It’s that time of year again, where you’ll be gathering around the dinner table with your family and a huge feast in front of you. [Read more] | Read more »
Square Rave guide - How to grab those te...
Square Rave is an awesome little music-oriented puzzle game that smacks of games like Lumines, but with its own unique sense of gameplay. To help wrap your head around the game, keep the following tips and tricks in mind. [Read more] | Read more »

Price Scanner via

Cyber Monday: Target offers 15% discount on A...
Target has discounted Apple Watches by 15% for Cyber Monday. Choose free shipping or free local store pickup (if available). Sale prices for online orders only, in-store prices may vary: - Apple... Read more
Sunday roundup of Holiday weekend Mac sales:...
Take up to $500 off MSRP on the price of a new Mac at B&H Photo today as part of their Black Friday/Holiday weekend sale. Shipping is free, and B&H charges NY tax only. These prices are... Read more
Holiday weekend: Apple Watch on sale for $50-...
B&H Photo has the Apple Watch on sale today for $50-$100 off MSRP. Shipping is free, and B&H charges NY sales tax only: - Apple Watch Sport: $50 off - Apple Watch: $50-$100 off Read more
Holiday weekend: iPad Air 2s on sale for up t...
B&H Photo has iPad Air 2s on sale for up to $80 off MSRP including free shipping plus NY sales tax only: - 16GB iPad Air 2 WiFi: $459 $40 off - 64GB iPad Air 2 WiFi: $569 $30 off - 128GB iPad Air... Read more
Holiday weekend Mac sales roundup: B&H Ph...
B&H Photo continues to have all new Macs on sale for up to $500 off MSRP as part of their Black Friday/Holiday weekend sale. Shipping is free, and B&H charges NY tax only: - 15″ 2.2GHz Retina... Read more
iMobie Releases its Ace iOS Cleaner PhoneClea...
iMobie Inc. has announced the new update of PhoneClean 4, its iOS cleaner designed to reclaim wasted space on iPhone/iPad for use and keep the device fast. Alongside, iMobie hosts a 3-day giveaway of... Read more
U.S. Cellular Offering iPad Pro
U.S. Cellular today announced that it is offering the new iPad Pro with Wi-Fi + Cellular, featuring a 12.9-inch Retina display with 5.6 million pixels — the most ever in an iOS device. U.S. Cellular... Read more
Newegg Canada Unveils Black Friday Deals for...
Newegg Canada is offering more than 1,000 deep discounts to Canadian customers this Black Friday, available now through Cyber Monday, with new deals posted throughout the week. “Black Friday is... Read more
Black Friday: Macs on sale for up to $500 off...
BLACK FRIDAY B&H Photo has all new Macs on sale for up to $500 off MSRP as part of their early Black Friday sale including free shipping plus NY sales tax only: - 15″ 2.2GHz Retina MacBook Pro: $... Read more
Black Friday: Up to $125 off iPad Air 2s at B...
BLACK FRIDAY Walmart has the 16GB iPad Air 2 WiFi on sale for $100 off MSRP on their online store. Choose free shipping or free local store pickup (if available): - 16GB iPad Air 2 WiFi: $399, save $... Read more

Jobs Board

Technical Program Manager, Strategic Merchant...
# Technical Program Manager, Strategic Merchants - Apple Pay Job Number: 44001177 Santa Clara Valley, Califo ia, United States Posted: Oct. 30, 2015 Weekly Hours: 40.00 Read more
Frameworks Engineer, *Apple* Watch - Apple...
# Frameworks Engineer, Apple Watch Job Number: 41403122 Santa Clara Valley, Califo ia, United States Posted: Jul. 1, 2015 Weekly Hours: 40.00 **Job Summary** Join the Read more
Software Engineer - *Apple* Pay - Apple (Un...
# Software Engineer - Apple Pay Job Number: 44003246 Santa Clara Valley, Califo ia, United States Posted: Nov. 16, 2015 Weekly Hours: 40.00 **Job Summary** Apple Pay Read more
Merchant Operations Manager: *Apple* Pay -...
# Merchant Operations Manager: Apple Pay Job Number: 43593822 Santa Clara Valley, Califo ia, United States Posted: Nov. 10, 2015 Weekly Hours: 40.00 **Job Summary** The Read more
Product Design Engineer - *Apple* Watch - A...
# Product Design Engineer - Apple Watch Job Number: 41727161 Santa Clara Valley, Califo ia, United States Posted: Jul. 22, 2015 Weekly Hours: 40.00 **Job Summary** Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.