Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

OpenWorld 2014 - Schema Management: versioning and automation with Puppet and MySQL Utilities

332 views

Published on

More devops in DBA's daily work, especially continuous integration of new schemes.

Published in: Data & Analytics
  • Be the first to comment

  • Be the first to like this

OpenWorld 2014 - Schema Management: versioning and automation with Puppet and MySQL Utilities

  1. 1. Schema Management: Versioning and Automation with Puppet and MySQL Utilities Oracle Open World 2014 : MySQL Central Frédéric Descamps
  2. 2. Who am I ? ● Frédéric Descamps «lefred» ● @lefred ● http://about.me/lefred ● within Percona since 2011 ● managing MySQL since 3.23 (as far as I remember) ● devops believer
  3. 3. We live in a devops world o/ Oracle Open World 2014 : MySQL Central -3-
  4. 4. devops ? we can say that devops is bringing together people and practices from development and operations Oracle Open World 2014 : MySQL Central
  5. 5. devops ? we can say that devops is bringing together people and practices from development and operations I am a happy dev !I am a happy op ! Oracle Open World 2014 : MySQL Central
  6. 6. CAMS ● devops is about CAMS – Culture – Automation – Measurement – Sharing @botchagalupe @damonedwards http://www.opscode.com/blog/2010/07/16/what-devops-means-to-me/ Oracle Open World 2014 : MySQL Central
  7. 7. This talk is about automation ● What techniques can I use ? – Infrastructure as Code – Continuous Improvement Oracle Open World 2014 : MySQL Central
  8. 8. WE are already between devs and ops Oracle Open World 2014 : MySQL Central
  9. 9. WE are already between devs and ops Oracle Open World 2014 : MySQL Central I am a happy DBA !
  10. 10. so... AUTOMATION ● Can we automate DBAs job ? ● Should we ?? ● Even if we have only one database ? Oracle Open World 2014 : MySQL Central
  11. 11. ● Everything needs to be reproducible Oracle Open World 2014 : MySQL Central
  12. 12. What do we «usually» automate ? ● binaries installation (which version of MySQL) ● configuration (my.cnf ?) ● cron jobs ● monitoring ● databases (schemas) ● grants ● replication ? Oracle Open World 2014 : MySQL Central
  13. 13. Infrastructure as code ● So to automate all these tasks a multiplicity of projects appeared on the web: – puppet – chef – cfengine – ansible – saltstack – ... Oracle Open World 2014 : MySQL Central
  14. 14. let's focus on Puppet ● we will install MySQL on a new node or or Oracle Open World 2014 : MySQL Central
  15. 15. let's focus on Puppet ● we will install MySQL on a new node 5.5 or 5.6 or 10.0 or 5.7 ? Oracle Open World 2014 : MySQL Central
  16. 16. let's focus on Puppet ● Puppet recipes will take care of the OS or Oracle Open World 2014 : MySQL Central
  17. 17. let's focus on Puppet ● Puppet recipes will take care of the OS or Oracle Open World 2014 : MySQL Central case $::osfamily {      'RedHat': {            $require = Yumrepo['mysql­repo']       }       'Debian': {            $require = Apt::Source['mysql­repo']       } }
  18. 18. Example in hiera ­­­ classes:   ­ mysql   ­ mysql::server mysql::mysql_root_password: «fred» mysql::mysql_distro: «community» mysql::mysql_version: «5.6» mysql::mysql_serverid: «2» mysql::mysql_bind_interface: «eth1» mysql::ensure: «running» Oracle Open World 2014 : MySQL Central
  19. 19. puppet (continue) ● And we can automate many things like schema creation, grants: mysql::db { $graphite_db_name:       user     => $graphite_db_user,       password => $graphite_db_password,       host     => $graphite_db_host,       grant    => ['SELECT', 'UPDATE'],          } Oracle Open World 2014 : MySQL Central
  20. 20. puppet: MySQL modules ● You can find many MySQL modules for puppet: – https://github.com/olindata/puppetlabs-mysql – https://github.com/lefred/puppet-lefred-mysql Oracle Open World 2014 : MySQL Central
  21. 21. and now ? ● can we go further ? ● what could we automate ? ● schemas ! – using mysqldump ? – using frameworks ? Oracle Open World 2014 : MySQL Central
  22. 22. but... automation ! ● we want this automated ● easy to test multiple times in development, staging, ... ● to use in production ● and if possible to rollback easily if needed ! v1 v2 Oracle Open World 2014 : MySQL Central
  23. 23. Database Schema Migration Tools ● There are some existing tools: – Liquibase – Flyway – c5-db-migration – dbdeploy – MyBatis – Autopatch – Sqitch Oracle Open World 2014 : MySQL Central
  24. 24. Database Schema Migration Tools Most of them are... Integrated with a framework or an ORM Oracle Open World 2014 : MySQL Central
  25. 25. Database Schema Migration Tools Most of them are... for JAVA applications Oracle Open World 2014 : MySQL Central
  26. 26. Database Schema Migration Tools Most of them use... XML definitions Oracle Open World 2014 : MySQL Central
  27. 27. XML Definitions ● What is easier to read and maintain for us, DBAs ? Oracle Open World 2014 : MySQL Central <?xml version="1.0" encoding="UTF­8"?>   <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema­instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog­2.0.xsd">   <changeSet id="1" author="lefred"> <createTable tableName="department"> <column name="id" type="int"> <constraints primaryKey="true" nullable="false"/> </column> <column name="name" type="varchar(50)"> <constraints nullable="false"/> </column> <column name="active" type="boolean" defaultValueBoolean="true"/> </createTable> </changeSet>   </databaseChangeLog>
  28. 28. XML Definitions ● or this ? Oracle Open World 2014 : MySQL Central CREATE TABLE department ( id INT NOT NULL PRIMARY KEY, name VARCHAR(50) NOT NULL );
  29. 29. Database Schema Migration Tools most of them are... blocking schema changes Oracle Open World 2014 : MySQL Central
  30. 30. Database Schema Migration Tools and finally... not designed to be used via a configuration management system Oracle Open World 2014 : MySQL Central
  31. 31. so are we blocked ? NO ! ● Oracle's MySQL-Utilities ● Percona Toolkit ● a standalone Python script ● a new Puppet type and provider Oracle Open World 2014 : MySQL Central
  32. 32. Oracle's MySQL-Utilities ● toolkit that was generally distributed via MySQL Workbench ● Dec 7th 2010 : v 1.0.0 ● Aug 20th 2014: v.1.5.1 RC ● it contains a package of tools used for maintenance and administration of MySQL servers ● they are written in Python ● the utilities are command line commands and a Python library Oracle Open World 2014 : MySQL Central
  33. 33. Oracle's MySQL-Utilities (2) ● Utilities can help for – database operations: provision a slave, copy a db, diff tables, synchronize tables – general operations: find disk space used, table structure, duplicate users, work on indexes, find database objects matching patterns – HA operations: add a slave, setup failover, find slaves, check replication – server operations: make a temp copy of the server, use secure connection – «specialized operations»: record login events, copy/move audit log, show failed queries, show connections Oracle Open World 2014 : MySQL Central
  34. 34. What are the interresting tools for us ● mysqldbcompare — compares two databases and identify differences (objects, row counts, data concistency) ● mysqldiff — identifies differences among database objects ● And all those options can be integrated into a Python script using the library Oracle Open World 2014 : MySQL Central
  35. 35. Percona-Toolkit ● collection of advanced command-line tools ● Aug 6th 2014: v2.2.10 ● They can do a lot of useful tasks like table checksum and synchronization, delay slave on version where it wasn't implemented yet, digest the queries ● But for us, the most important: pt­online­schema­change Oracle Open World 2014 : MySQL Central
  36. 36. pt-online-schema-change ● this tool allows to alter a table's structure without blocking reads or writes ● the tool works on a copy of the table you wish to alter ● switches them at the end of the process ● and delete the original one
  37. 37. online-migration.py ● standalone Python script ● command line only no GUI sorry ;-) ● uses Oracle's MySQL-Utilities library ● uses pt-online-schema-change ● uses normal SQL statements that all DBAs understand ● can be integrated in Puppet Oracle Open World 2014 : MySQL Central
  38. 38. what can it do ? ● The script is able to – return the schema version of a database – list all versions and their status – migrate up – migrate down (rollback a migration) – checksum the schema – return the difference between expected schema and installed schema Oracle Open World 2014 : MySQL Central
  39. 39. and in Puppet ? node percona1 {  include mysql  include mysql::server  include mysql::utilities  include mysql::percona_toolkit  include online­migration         Class['percona::repository'] ­> Class['percona::server']         mysql_schema { "world":                      ensure  => present,          require => [ Package["online­migration"],     Service['mysql'] ],                      version => 2,                       cwd     => "/root/om",         }         ... } Oracle Open World 2014 : MySQL Central
  40. 40. and in Puppet ? node percona1 {  include mysql  include mysql::server  include mysql::utilities  include mysql::percona_toolkit  include online­migration         Class['percona::repository'] ­> Class['percona::server']         mysql_schema { "world":                      ensure  => present,          require => [ Package["online­migration"],     Service['mysql'] ],                      version => 2,                       cwd     => "/root/om",         }         ... } Oracle Open World 2014 : MySQL Central ... info: Mysql_schema[world](provider=mysql_schema): performing the check, last_version is 1 info: Mysql_schema[world](provider=mysql_schema): we need to migrate to another version (2) info: Mysql_schema[world](provider=mysql_schema): performing the migration ...
  41. 41. online-migration.py internals ● uses a dedicated table : online_migration.migration_sys – it contains some meta data like db, version, apply date, status – created by: online­migration.py init_sysdb – first entry is version 0 and is created by the «init» command ● uses a folder with meta-data: – XXXX­up.meta: meta data required to upgrade to version XXXX, like version, checksum and comment – XXXX­up.mig: contains the required changes/statements – XXXX­down.mig: contains the automated reverse statements
  42. 42. online-migration.py: features ● versioning and status $ online­migration.py status world Migration of schema world : +­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­+ | VERSION | APPLIED             | STATUS           | COMMENT                | +­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­+ |    0000 | 2014­09­22 09:17:16 |               ok |           Initial file | |    0001 |                none |          pending |                   none | |    0002 |                none |          pending |                   none | |    0003 |                none |          pending | add nice column to cit | +­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­+
  43. 43. online-migration.py: features (2) ● migration up $ online­migration.py up world to 3 INFO : You want to migrate up to version 0003 INFO : Ok this version is pending INFO : Preparing migration to version 0001 INFO : Applied changes match the requested schema INFO : Preparing migration to version 0002 INFO : Applied changes match the requested schema INFO : Preparing migration to version 0003 INFO : Applied changes match the requested schema $ online­migration.py status world Migration of schema world : +­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­+ | VERSION | APPLIED             | STATUS           |                COMMENT | +­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­+ |    0000 | 2014­09­22 09:17:16 |               ok |           Initial file | |    0001 | 2014­09­22 09:17:46 |               ok |                   none | |    0002 | 2014­09­22 09:18:24 |               ok |                   none | |    0003 | 2014­09­22 09:18:36 |               ok | add nice column to cit | +­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­+
  44. 44. online-migration.py: features (3) ● checksum $ online­migration.py status world Migration of schema world : +­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­+ | VERSION | APPLIED             | STATUS           |                COMMENT | +­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­+ |    0000 | 2014­09­22 09:17:16 |               ok |           Initial file | |    0001 | 2014­09­22 09:17:46 | checksum problem |                   none | +­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­+ $ online­migration.py checksum world world's current schema checksum = 3239e7245d98d27681020e36d0085676
  45. 45. online-migration.py: features (4) ● migration down (rollback) $ online­migration.py down world 2 INFO : You want to migrate down 2 version(s) INFO : rollback from 0003 to 0002 INFO : rollback from 0002 to 0001 $ online­migration.py status world Migration of schema world : +­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­+ | VERSION | APPLIED             | STATUS           |                COMMENT | +­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­+ |    0000 | 2014­09­22 09:17:16 |               ok |           Initial file | |    0001 | 2014­09­22 09:17:46 |               ok |                   none | |    0002 | 2014­09­22 09:18:24 |         rollback |                   none | |    0003 | 2014­09­22 09:18:36 |         rollback | add nice column to cit | |    0002 |                none |          pending |                   none | |    0003 |                none |          pending | add nice column to cit | +­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­+
  46. 46. online-migration.py: features (5) ● schema diff $ online­migration.py diff world WARNING : Schema of world doesn't have expected checksum (4478d85870969436400bac023f2b2b7c) TABLE `City` ­ `CountryCode` varchar(10) DEFAULT NULL, + `CountryCode` char(3) NOT NULL DEFAULT '',
  47. 47. todo ● Make the code more robust (handling errors) ● Write support for store procedures, functions,... ● Support online DDL (ALGORITHM=INPLACE) ● Add configuration file
  48. 48. Resources ● Oracle MySQL — http://dev.mysql.com/downloads/mysql/ ● MySQL-Utilities — http://dev.mysql.com/downloads/utilities/ ● Percona-Toolkit - http://www.percona.com/doc/percona- toolkit/2.2/index.html ● pt-online-schema-change — http://www.percona.com/doc/percona- toolkit/2.2/pt-online-schema-change.html ● puppet — http://puppetlabs.com ● online-migration.py — https://github.com/lefred/online-migration Oracle Open World 2014 : MySQL Central
  49. 49. Thank you Questions ? Oracle Open World 2014 : MySQL Central

×