Monday, October 10, 2011

MySQL Utilities Release 1.0.3

The MySQL Utilities project continues to evolve with key new features for
replication and export. The latest release, 1.0.3, is no exception.

MySQL Utilities is included in the MySQL Workbench product which can be
downloaded from HTTP://dev.mysql.com/downloads/workbench/5.2.html

If you want the latest developments for MySQL Utilities, you can create a
bazaar branch using the following command:

bzr branch lp:~mysql/mysql-utilities/trunk

New Utility - mysqlrplshow

You can now view a list of the slaves attached to your master with
mysqlrplshow. The utility displays a graph of the master and its slaves y
default but you can also get a list of the slaves in GRID, CSV, TAB, or
VERTICAL format as follows.

  • GRID - Displays output formatted like that of the mysql monitor in a grid or table layout. 
  • CSV - Displays the output in a comma-separated list. 
  • TAB - Displays the output in a tab-separated list.
  • VERTICAL - Displays the output in a single column similar to the '\G' option for the mysql monitor commands.

You can use the GRID format to output the data and store it away for a later
run then compare or diff against the recent output to see what has changed. In
this case, what new slaves have been changed, added or removed from your
topology.

# Replication Topology Graph
localhost:3311 (MASTER)
   |
   +--- localhost:3310 - (SLAVE)
   |
   +--- localhost:3312 - (SLAVE + MASTER)
       |
       +--- localhost:3313 - (SLAVE)


If you want to see your replication topology, you can use the --recurse option
to instruct mysqlrplshow to traverse each slave found to locate downstream
slaves. Circular topologies are detected and displayed using a special
notation. You can even limit the depth of the recursive search if you want to
see only a portion of your topology.

The utility currently works by reading slaves with the SHOW SLAVE HOSTS
command. For most versions of MySQL, this requires the use of --report-host and
--report-port options when setting up your slaves. For example, version 5.5.3
and later always show the slaves regardless if --report-host is specified.
Versions older than 5.5.3 will not show the slaves unless --report-host is
specified.

You should specify the --report-host option when starting your slaves but take
care to ensure the correct host or IP is specified. If the slave is using a NAT
derived IP, be sure to use the IP address that can reach the slave from the
master.

You do not normally need to specify --report-port unless the slave is using a
custom port (something other than the default of 3306).

Improvements

There have been a number of minor enhancements and there are two major feature
additions that will improve usage in many scenarios.

Enhanced binlog control for mysqlreplicate - you can now start replication in
one of four ways:

  1. Start from the first event recorded in the binary log on the master. This is typically used to setup a new master and slave replication topology.
  2. Start from the first event in a specific binary log file This is used to restart replication from a specific point or to start replication after a slave has had data loaded (for example via a restore or file copy from the master).
  3. Start from a specific event (position) in a specific binary log file. This is a common operation for point-in-time recovery.
  4. Start from the current position of the master. This is typically used to start a slave following a recovery of the slave, fail-back to a recovered master, or restarting replication after an error event has been resolved.

We added --all option to mysqldbcopy, mysqldbexport. These utilities allow you
to include all of the databases on your server in the operation except the
system databases (mysql, information_schema, and performance_schema).

Try it out and let us know what you think!

Thursday, July 14, 2011

Comparing Databases with mysqldbcompare

If you have two or more database servers containing the same data, how do you know if the objects are identical. Furthermore, how can you be sure the data is the same on all of the servers? What is needed is a way to determine if the databases are in synch - all objects are present, the object definitions are the same, and the tables contain the same data. Synchronizing data can become a nightmare without the proper tools to quickly identify differences among objects and data in two databases. Perhaps a worst case (and more daunting) is trying find data that you suspect may be different but you don’t have any way of finding out.

This is where the new 'mysqldbcompare' utility comes in handy. The mysqldbcompare utility uses the mysqldiff functionality (mysqldiff allows you to find the differences in object definitions for two objects or a list of objects in two databases) and permits you to compare the object definitions and the data among two databases. Not only will it find the differences among database objects and their definitions, it will also find differences in the data!

The databases can reside on the same server or different servers. The utility performs a consistency check to ensure two databases are the same defined as having the same list of objects, identical object definitions (including object names), and for tables the same row counts and the same data.

Some scenarios where mysqldbcompare can be employed include:

  • checking master and slave for consistency
  • checking production and development databases for consistency
  • generating a difference report for expected differences among new and old data
  • comparing backups for differences
Running the Utility

Let us take a look at the utility in action. Below are two examples of the utility comparing what should be the same database on two servers. I am using a simple detail shop inventory database used to manage supplies. It consists of two tables (supplier, supplies) and three views (cleaning, finishing_up, and tools).

In the first example, we see an example where the databases are consistent. When you examine the output, you will see each object is inspected in three passes. First, the object definitions are compared. Any discrepancies would be displayed as a difference in their CREATE statements. If the object is a table, a row count test is performed followed by a comparison of the data. Surely, if the row count test fails we know the data check will fail.

Note: This is the same output (and indeed the same code) that is used for mysqldiff. The mysqldbcompare utility has all of the same features with respect to difference type presented (unified, differ, and context) which you can select with the same option named ‘--difftype’.

mysqldbcompare --server1=root@localhost --server2=root@backup_host:3310 inventory:inventory
# server1 on localhost: ... connected.
# server2 on localhost: ... connected.
# Checking databases inventory on server1 and inventory on server2


Defn Row Data
Type Object Name Diff Count Check
---------------------------------------------------------------------------
TABLE supplier pass pass pass
TABLE supplies pass pass pass
VIEW cleaning pass - -
VIEW finishing_up pass - -
VIEW tools pass - -


Databases are consistent.


# ...done

Normally, the mysqldbcompare utility will stop on the first failed test. This default means you can run the utility as a safeguard on data that you expect to be consistent. However, if you suspect or know there will be differences in the database objects or data and want to run all of the checks, you can use the ‘--run-all-tests’ option. This option will run the tests on all objects even if some tests fail. Note that this does not include system or access errors such as a down server or incorrect login - those errors will cause the utility to fail with an appropriate error message.

In the second example, we expect the databases to be different and we want to know which data is different. As you can see, the utility found differences in the object definitions as well as differences in the data. Both are reported.

mysqldbcompare --server1=root@localhost --server2=root@backup_host:3310 inventory:inventory --run-all-tests
# server1 on localhost: ... connected.
# server2 on localhost: ... connected.
# Checking databases inventory on server1 and inventory on server2


WARNING: Objects in server1:inventory but not in server2:inventory:
VIEW: finishing_up
VIEW: cleaning


Defn Row Data
Type Object Name Diff Count Check
---------------------------------------------------------------------------
TABLE supplier pass FAIL FAIL


Row counts are not the same among inventory.supplier and inventory.supplier.


Data differences found among rows:
--- inventory.supplier
+++ inventory.supplier
@@ -1,2 +1,2 @@
code,name
-2,Never Enough Inc.
+2,Wesayso Corporation


Rows in inventory.supplier not in inventory.supplier
code,name
3,Never Enough Inc.


TABLE supplies pass FAIL FAIL


Row counts are not the same among inventory.supplies and inventory.supplies.


Data differences found among rows:
--- inventory.supplies
+++ inventory.supplies
@@ -1,4 +1,4 @@
stock_number,description,qty,cost,type,notes,supplier
-11040,Leather care,1,9.99,other,,1
-11186,Plastic polish,1,9.99,polishing,,1
-11146,Speed shine,1,9.99,repair,,1
+11040,Leather care,1,10.00,other,,1
+11186,Plastic polish,1,10.00,polishing,,1
+11146,Speed shine,1,10.00,repair,,1


Rows in inventory.supplies not in inventory.supplies
stock_number,description,qty,cost,type,notes,supplier
11104,Interior cleaner,1,9.99,cleaning,,1
11056,Microfiber and foam pad cleaner,1,9.99,cleaning,,1
11136,Rubber cleaner,1,9.99,cleaning,,1
11173,Vinyl and rubber dressing,1,9.99,cleaning,,1
11106,Wheel cleaner,1,9.99,cleaning,,1
11270,Carpet cleaner,1,9.99,cleaning,,1


Rows in inventory.supplies not in inventory.supplies
stock_number,description,qty,cost,type,notes,supplier
11269,Microfiber spray on car wash towel,3,16.99,cleaning,,1
11116,Microfiber wax removal towel,3,16.99,waxing,,1
10665,Glass polish pad,3,10.00,polishing,,1


VIEW tools FAIL - -


--- inventory.tools
+++ inventory.tools
@@ -1,1 +1,1 @@
-CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `inventory`.`tools` AS select `inventory`.`supplies`.`stock_number` AS `stock_number`,`inventory`.`supplies`.`description` AS `description`,`inventory`.`supplies`.`qty` AS `qty`,`inventory`.`supplies`.`cost` AS `cost`,`inventory`.`supplies`.`type` AS `type`,`inventory`.`supplies`.`notes` AS `notes`,`inventory`.`supplies`.`supplier` AS `supplier` from `inventory`.`supplies` where (`inventory`.`supplies`.`type` = 'tool')
+CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `inventory`.`tools` AS select `inventory`.`supplies`.`stock_number` AS `stock_number`,`inventory`.`supplies`.`description` AS `description`,`inventory`.`supplies`.`qty` AS `qty`,`inventory`.`supplies`.`cost` AS `cost`,`inventory`.`supplies`.`type` AS `type`,`inventory`.`supplies`.`notes` AS `notes`,`inventory`.`supplies`.`supplier` AS `supplier` from `inventory`.`supplies` where (`inventory`.`supplies`.`type` in ('tool','other'))


Database consistency check failed.

Take a moment to read through the report above. At the top of the report (the first object tested), we see a critical error in the suppliers table. Here we can see that there are two different names for the same supplier_id. All relational database theory aside, that could spell trouble when it comes time to reorder supplies.

Notice the report for the supplies table. In this example, the utility identified three rows that were different among the two databases. It also identified rows that were missing from either table. Clearly, that could help you diagnose what went wrong where in your application (or your data entry).

Lastly, we see a possible issue with the tools view. Here the view definition differs slightly. Depending the use of the view this may be acceptable but it is nice to know nonetheless.

What Does This All Mean?

If data consistency is important to you or if you need a way to quickly determine the differences among data in two databases, the mysqldbcompare utility is a great addition to your toolset. But don’t take my word for it - try it out yourself.

Download and Try It Out!

The MySQL Utilities project is written in Python and is a component of the MySQL Workbench tool. You can download the latest release of the MySQL Workbench here:

http://dev.mysql.com/downloads/workbench/

There are some limitations in this first release. Currently, if the storage engines differ among the tables in the compare, the object definitions will show this difference – which is exactly what you would expect. However, the utility will stop and report the object definition test as a failure. You can still run the data consistency check by using the --force option which instructs the mysqldbcompare utility to run all tests unless they fail from an internal exception (for example, the table files are corrupt).

Got Some Ideas or Want to See New Features?

One really cool feature would be if the utility generate SQL statements for synchronizing the data and objects. Would this be something you would want to see incorporated?

If you’re intrigued by this new utility and in your course of use find new features or new uses that you would like to see incorporated in future revisions, please email me and let me know!

Related Material

Th latest MySQL Utilities development tree (including mysqldbcompare) can be found here:

https://launchpad.net/mysql-utilities

Wednesday, April 20, 2011

Wand Selection at Ollivander’s

Fans of J. K. Rowling’s Harry Potter books should plan a visit to Universal Studio’s Wizarding World of Harry Potter. There you will encounter scenes from the books and movies, a castle ride that may result in battle damage from a dragon (I got sprayed by its hot, fetid breath), butter beer by the barrel, a dragon ride not for the faint of heart, a tour of one of the most famous classrooms, and several of the wizardly shops open to muggles (they will even let you buy stuff).

Perhaps the most exciting opportunity for Harry Potter fans is the wand ceremony at Ollivander’s. There a lucky few can have a wand selected for them by Ollivander himself. Having witnessed the ceremony, I can say it was one of those times I wish I were a child again. The ceremony I saw involved two boys about the ages of 8 and 10. Ollivander had them come forward and go through a series of tests to select a wand. The looks on their faces when presented with wands chosen specifically for them was well worth the time spent waiting in line to see the ceremony. I can imagine that event will be a lasting memory for those boys and their parents.

If the lines are too long or you were not fortunate enough to be selected by Ollivander, you can still have your wand selected for you by inquiring among Ollivander’s helpful wizardly staff.

The process is very simple. Just select a date that is most important to you - one that holds special meaning. It is best if this date is tied directly to your core being (so sayeth Ollivander). There are thirteen wands that are available for selection and each is tuned to a specific date range. The following lists the wands and the dates that apply. As to what these wands look like, you’ll have to visit Ollivander’s to find out.

Wand Date Range
Birch December 24 - January 20
Rowan January 21 - February 17
Ash February 18 - March 17
Alder March 18 - April 14
Willow April 15 - May 12
Hawthorn May 13 - June 9
Oak June 10 - July 7
Holly July 8 - August 4
Hazel August 5 - September 1
Vine September 2 - September 29
Ivy September 30 - October 27
Reed October 28 - November 24
Elder November 25 - December 23


There are, of course, all of the major character wands available for you to choose or collect. All of the wands are made of resin with very nice detail.

If you cannot make the trip to Orlando, you were there but did not buy a wand, or just want to add to your collection, you can go to the URL below  and order one from one of Ollivander’s helpful house elves. These are the very same wands sold at the theme park and the gift shops at the Orlando airport but are contained in a more travel friendly box (it’s a bit bigger).

http://www.universalorlando.com/Merchandise/Shop/Ollivanders_Wand_Shop.html

Thursday, April 7, 2011

MySQL Workbench Utilities

Introducing MySQL Workbench Utilities

One of the many new things that is being introduced during the Collaborate 2011 and 2011 MySQL Users’ Conference are some great new additions to some of the external tools for managing MySQL servers. One of those tools receiving updates is the MySQL Workbench.

One of the jewels in a long list of new features is the addition of new command-line utilities to help you administer your servers. The new feature is called MySQL Workbench Utilities. It is a package of easy-to-use utilities for maintenance and administration of MySQL servers. These utilities incapsulate a set of primitive commands bundling them so that you can perform macro operations with a single command. Some of the key features in MySQL Workbench Utilities include:

  • Plugin for MySQL Workbench 5.2.31
  • Available under the GPLv2 license
  • Written in Python
  • Easily to extend using the supplied library
How Does It Work?

There are two ways to access the utilities from within the MySQL Workbench.

You can click on the drop down arrow icon to the right of the MySQL Workbench main window. This will display a list of the plugins available. You can scroll through the screens and find the MySQL Utilities icon. The image below shows what the window looks like.




Once you hover on the MySQL Utilities icon, you will see an Action button appear. Click Action then Start Plugin to launch the MySQL Utilities shell in a new window. The image below shows what the window would look like.



You can launch any of the utilities listed by typing the name of the command. To find out what options are available, use the --help option.

Note: you can also add the MySQL Utilities icon to your home screen. Highlight Add to Home Screen and then choose a position from the menu.

You can also launch the MySQL Utilities command window by clicking on the Plugins menu item and selecting Start Shell for MySQL Utilities.

What Utilities are Available?

The MySQL Workbench and MySQL Workbench Utilities developers are adding new features and utilities with every release. If you don’t see a utility you want, check back with each release of Workbench or send the developers an email and let them know what you want!

You should try the --help option for each tool to see what options it supports. Some utilities have lots of options for controlling the operation (for example the mysqldbexport can produce one of several formats with object definitions, data, or both).

There are several options that are common to the utilities available (for instance --version, --help, --verbose) but the one that is most important is the server specification option used to connect to a specific server. This manifests as --server, --source, --destination, or --server1, etc. and all require the same format as follows:

--server=user_name:password@hostname:port_num:socket_file.

The parameter includes the user name followed by the user’s password (if available) separated by a colon (colon is omitted if no password) followed by the @ symbol and the host machine name. Optionally, you can provide the port number and the socket file each preceded by a colon. That sounds like a lot of crazy typing but it is really simple. The following are some examples using this format.

--server=root@localhost:3306
--server=joe:pass@192.168.1.101
--server=dolly:sassyshoes@localhost:3310:/tmp/mysql.sock

The following list describes the utilities available as well as some examples of use. For more details, please see the manuals for each utility.

mysqldbcopy - Permits a database administrator to copy a database from one server either to another server as the same name or a different name or to the same server as the same or as a different name.

Example 1: Copy a database named ‘util_test’ to a new name ‘util_test_copy’ on the same server.

mysqldbcopy util_test:util_test_copy
--source=root:pass@host1:3306
--destination=root:pass@host1:3306

Example 2: Copy a database named ‘util_test’ to another server.

mysqldbcopy util_test:util_test
--source=root:pass@host1:3306
--destination=root:pwd@host2:33010

mysqldbexport - Permits a database administrator to export the metadata (object definitions, hence definitions) or data or both from one or more databases. By default, the utility will export only definitions.

Example 1: Export the definitions of the database ‘dev’ from a MySQL server on localhost via port 3306 producing CREATE statements.

mysqldbexport --skip=GRANTS
--server=root:pass@localhost
--export=DEFINITIONS util_test

Example 2: Export the data of the database ‘util_test’ producing bulk insert statements.

mysqldbexport --export=DATA
--bulk-insert util_test
--server=root:pass@localhost

mysqldbimport - Permits a database administrator to import the metadata (objects) or data for one or more databases from one or more files in either SQL or a text format such as CSV, TAB, GRID, or VERTICAL.

Example 1: Import the metadata of the database ‘util_test’ to server1 on port 3306 using a file in CSV format.

mysqldbimport --import=definitions
--server=root@localhost --format=csv
data.csv

Example 2: Import both the data and definitions of the database ‘util_test’ to server1 on port 3306 producing bulk insert statements from a file that contains SQL statements.

mysqldbimport data.sql --import=both
--bulk-insert --format=sql
--server=root@localhost

mysqldiff - Reads the definitions of objects and compares them using a diff-like method to determine if two objects are the same based on the definition of the object.

Example 1: Find the differences among objects in database employees on one server and emp1 on the same server.

mysqldiff --server1=root@localhost
employees:emp1

Example 2: Find differences between the salaries table on one server and the same table on another server.

mysqldiff --server1=root@localhost
--server2=root@host2:3306
employees.salaries:emp1.salaries
--differ

mysqldiskusage - Permits a database administrator to see the disk space usage for one or more databases in either CSV, TAB, GRID, or VERTICAL text formats. The utility will also allow the user to examine the disk usage for the binary logs, slow, error, and general log; and InnoDB tablespace usage.

Example 1: Show only the disk space usage for the employees and test databases in grid format.

mysqldiskusage --server=root@localhost
employees test

Example 2: Show all disk usage for the server in CSV format.

mysqldiskusage --server=root@localhost
--format=csv -a

mysqlindexcheck - Eeads the indexes for one or more tables and identifies duplicate and potentially redundant indexes.

Example 1: Scan all of the tables in the employees database to see the possible redundant and duplicate indexes as well as the DROP statements for the indexes.

mysqlindexcheck --server=root@localhost 
 --show-drops employees

Example 2: Scan all of the tables in the employees database and display the indexes. Display the output in a tabular format.

mysqlindexcheck --server=root@localhost
--show-indexes --format=TAB employees

mysqlmetagrep - Searches for objects matching a given pattern and shows a table of the objects that match the pattern `’t\_’.

mysqlmetagrep --pattern=“t_”
--server=mats@localhost

Example 2: Find all objects that contain ‘t2’ in the name or the body (for routines, triggers, and events).

mysqlmetagrep -b --pattern=“%t2%”
--server=mats@localhost:3306

mysqlprocgrep - Scans the process lists for processes that match the search criteria specified and will either print the result (the default) or execute certain actions on it.

Example 1: Kill all connections created by user “mats” that are younger than 1 minute.

mysqlprocgrep --server=root@localhost
--match-user=mats --age=1m
--kill-query

Example 2: Kill all connections with queries that have been idle for more than 1 hour.

mysqlprocgrep --server=root@localhost
--match-command=sleep --age=1h
--kill

mysqlreplicate - Permits an administrator to start replication among two servers. The user provides login information to the slave and provides connection information for connecting to the master.

Example 1: Setup replication between a MySQL instance on two different hosts using the default settings.

mysqlreplicate --rpl-user=rpl:rpl
--master=root@localhost:3306
--slave=root@localhost:3307

Example 2: Ensure the replication between the master and slave is successful if and only if the InnoDB storage engines are the same and both servers have the same storage engines with the same default specified.

mysqlreplicate --pedantic
--master=root@localhost:3306
--slave=root@localhost:3307
--rpl-user=rpl:rpl -vv

mysqlserverclone - Permits an administrator to start a new instance of a running server.

Example 1: Create a new instance of a running server.

mysqlserverclone --new-port=3310
--server=root:pass@localhost
--new-data=/source/test123

Example 2: Create a new instance of a running server set the root password and turn binary logging on.

mysqlserverclone --new-port=3310
--server=root:pass@localhost
--new-data=/source/test123
--root-password=pass
--mysqld=--log-bin=mysql-bin

mysqluserclone - Permits a database administrator to use an existing user account on one server as a template and clone a MySQL user such that one or more new user accounts are created on another (or the same) server with the same privileges as the original user.

Example 1: Clone ‘joe’ as ‘sam’ and ‘sally’ with passwords and logging in as root on the local machine.

mysqluserclone --source=root@localhost
--destination=root@localhost
joe@localhost sam:secret1@localhost
sally:secret2@localhost

Example 2: Show all of the users on the localhost server in the most verbose output in CSV format.

mysqluserclone --source=root@localhost
--list --format=CSV -vvv

mut - Designed to execute predefined tests to test the MySQL Utilities.

Example 1: Run all tests.

mut --server=root@localhost

Example 2: Run all tests that start with ‘clone_user’ and set the width of the display to 70 characters.

mut --server=root@localhost
--do-tests=clone_user –width=70

How Can I Get It?

You can download MySQL Workbench from:

http://www.mysql.com/downloads/workbench/

You can also download the latest development source code tree for the MySQL Workbench Utilities from:

https://launchpad/net/mysql-utilities