How to compare schema of two databases in MySQL

If you are responsible for maintaining database objects structure and want to know what objects are added or dropped from previous application version to the next version, then you have come to the right place.

You may need to know the schema difference for any number of purposes like for Application upgrade, Product documentation etc.

In this tutorial we will mention how to compare schema of two databases in MySQL. You will need below mentioned tools to do this.

  • MySQL built-in utility: mysqldump
  • Text Editor supporting Regular Expression: Notepad++
  • File comparison tool: Chose as per your preference, I used Beyond Compare

1. Take the backup of schema for the databases

For the purpose of this tutorial we will assume that we need to compare DB schema for two databases appVer1 and appVer2. Take the backup as follows: (Provide password when prompted and press Enter key.)

For appVer1: mysqldump -h your_db_host -u your_db_user -p --single-transaction --skip-comments --no-data --routines --triggers --events appVer1 > appVer1_schema.sql

For appVer2: mysqldump -h your_db_host -u your_db_user -p --single-transaction --skip-comments --no-data --routines --triggers --events appVer2 > appVer2_schema.sql

NOTE:
  1. Replace your_db_host with actual HOST and your_db_user with actual USER of your MySQL server.
  2. Make sure that the USER has proper privilege on the database whose schema you are fetching in this activity.

2. Use Notepad++ to format the schema dump

The mysqldump output will also contain auto increment clause, if the tables are created with the same. It will show difference in DB schema when you will use a compare tool, where in actual it may not matter for you.

How to resolve this?

 
Use Notepad++

 
Open your mysqldump file in Notepad++. Then Search and Replace using Regular Expression as follows:

 
Regular Expression: AUTO_INCREMENT=[0-9]+<space>
 

Replace <space> with actual space character.

 

Find what: AUTO_INCREMENT=[0-9]+<space> (Add a actual space character after + character)
Replace with: Make sure there is no character inside this box as you need to replace the matching pattern with blank.
 

Choose Replace ALL.

Apply similar process for other clauses as well like DEFINER etc., if present and creating unnecessary difference.

3. Compare the two schema files

Use a file comparison tool to compare the two schema files after they have been formatted.

Comments

Back To Top

Popular posts from this blog

How to save video from Internet Explorer

error 18 at 0 depth lookup: self signed certificate

How to check fragmentation in MySQL tables