SlideShare a Scribd company logo
Scaling MySQL with python
EuroPython 2015, Bilbao
Roberto Polli - roberto.polli@par-tec.it
Par-Tec Spa - Rome Operation Unit
P.zza S. Benedetto da Norcia, 33
00040, Pomezia RM - www.par-tec.it
20 July 2015
Roberto Polli - roberto.polli@par-tec.it
Agenda
Intro
MySQL
Python Utilities
Connectors
Replication
Failover
Fabric
Provisioning
Fabric in the Cloud
Roberto Polli - roberto.polli@par-tec.it
Who? What? Why?
Roberto Polli - Solutions Architect @ par-tec.it. Loves writing in C, Java and
Python. Red Hat Certified Engineer and Virtualization Administrator.
Par-Tec – Proud sponsor of this talk ;) Contributes to various FLOSS.
Provides:
• IT Infrastructure & Services expertise;
• Business Intelligence solutions;
• Vertical Applications for the financial market.
Manage, replicate, scale MySQL is easy with python (and Docker).
Intro Roberto Polli - roberto.polli@par-tec.it
MySQL
• Single-process, multi-thread
• 2-tier architecture
• Connection + SQL
• Storage Backend
• Greatly improved in 5.6
• More to come in 5.7: Dynamic Buffers & Multi-source replication
MySQL Roberto Polli - roberto.polli@par-tec.it
MySQL
Many Backends aka Storage Engines:
• InnoDB1
: ACID, MVCC2
, Redo|Undo logs;
• Memory: fast & volatile;
• NDB: MySQL Cluster, network distributed, auto-sharded;
Replication based on changelog files (binary logs).
1
default
2
Multi Versioning Concurrency Control
MySQL Roberto Polli - roberto.polli@par-tec.it
Manage & Use a database
Monitor:
• Database size: Tables, Indexes,
Binary Logs
• Replication inconsistencies
• Failover
Connect:
• Native Drivers
• for Python 2.6, 2.7 and 3.3
• SSL & compression
• Orchestrate
Download scripts from some blog, add random queries from docs.mysql.com,
Shake.
MySQL Roberto Polli - roberto.polli@par-tec.it
MySQL Utilities
A python package with utilities, drivers, fabric orchestrator
$ wget http://bit.ly/1CxNuZe
$ tar xf mysql-utilities-1.6.1.tar.gz
$ cd mysql-utilities-1.6.1
$ python setup.py install
mysql-utilities-1.6.1
|-- mysql
|-- connector
| |-- django
| ‘-- fabric
|-- fabric
| |-- protocols
| |-- providers
| ‘-- services
‘-- utilities
Python Utilities Roberto Polli - roberto.polli@par-tec.it
MySQL Utilities:
Start with mysqluc for a single entrypoint
• entrypoint for all utilities
• contextual help
• TAB completion
Or access each utility separately.
Specify server URIs as user:pass@host[:port].
Python Utilities Roberto Polli - roberto.polli@par-tec.it
MySQL Utilities
mysqluc> help utilities
Utility Description
----------------- --------------------------------------------------------
mysqlauditadmin audit log maintenance utility
mysqlauditgrep audit log search utility
...
mysqldbcompare compare databases for consistency
...
mysqldiskusage show disk usage for databases
mysqlfailover automatic replication health monitoring and failover
Python Utilities Roberto Polli - roberto.polli@par-tec.it
Disk Usage
A single command to show all disk usage infos (excluded system logs)
$ mysqldiskusage --all --server=$SERVER
...
| db_name | total |
| akonadi | 969150919 |
...
Total database disk usage = 971233529 bytes or 926,24 MB
...
| ib_logfile0 | 67108864 |
| ib_logfile1 | 67108864 |
| ibdata1 | 44040192 |
Total size of InnoDB files = 178257920 bytes or 170,00 MB
Python Utilities Roberto Polli - roberto.polli@par-tec.it
Connectors
from django.db.backends import BaseDatabaseValidation
if django.VERSION < (1, 7):
from django.db import models
else:
from django.core import checks
from django.db import connection
class DatabaseValidation(BaseDatabaseValidation):
if django.VERSION < (1, 7):
Connectors Roberto Polli - roberto.polli@par-tec.it
Log-based Replication: Master
Replication is asynchronous or semi − synchronous.
A Master with a Unique Server ID
• produces a changelog named binary log;
• assign each transaction a Global Transaction ID;
• grants access to a replica user;
Optionally:
• stores replication infos in ACID tables;
• may track slave-updates;
• waits for one slave ack on semi − synchronous replication;
Replication Roberto Polli - roberto.polli@par-tec.it
Log-based Replication: Slave
Slave
• connects to the master as the replica user;
• retrieves the binlog;
• applies the changes;
• ∀ slave ∃! master;
Optionally
• forbids local changes from non-root users;
If binlogs have been purged, you need to import the master database first!
Replication Roberto Polli - roberto.polli@par-tec.it
Benefits of replication
• Availability.
• Scaling reads with R/W split.
• Scaling reads differencing indexes.
• Backup and data warehouse
strategies.
# mysqlrplshow output
#
# Replication Topology Graph
s-1.docker:3306 (MASTER)
|
+--- s-3.docker:3306 - (SLAVE)
|
+--- s-4.docker:3306 - (SLAVE)
Replication Roberto Polli - roberto.polli@par-tec.it
mysqlreplicate
Configuring replication with one command:
mysqlreplicate -b --pedantic 
--master=$MASTER --slave=$SLAVE --rpl-user=repl:rpass
# master uuid = 2cda700...
# slave uuid = 7520cf0...
# Checking for binary logging on master...
# Granting replication access to
# replication user...
# Connecting slave to master...
# CHANGE MASTER TO MASTER_HOST = ’172.17.0.5’,...
• preliminary checks;
• create replica user on the master;
• point the slave to the master;
• start loading the first available
transaction in binlogs;
Replication Roberto Polli - roberto.polli@par-tec.it
Initialize new slaves
Beware!
1. Avoid replicating user provisioning and other host-related setup!
@SESSION.SQL_LOG_BIN=0;
2. Binlogs are usually retained for a fixed period - eg 15days.
To provision new slave after that period you must:
• start from a backup or a snapshot;
• apply the binlogs.
mysqldbexport >> data.sql --server=root:pass@master --rpl-user=repl:rpass 
--export=both --rpl=master --all
mysqldbimport --server=root:pass@slave data.sql
Replication Roberto Polli - roberto.polli@par-tec.it
Failover Basics
From replication to high availability, automagically discovering slaves.
• promote the most updated slave
• reconfigure the others
• disable the master
• eventually fix ip-address
# Read carefully the docs of
# mysqlfailover and of all
# the supported parameters!
mysqlfailover --master=$MASTER 
--discover-slaves-login=root:password 
--candidates=$SLAVE1,$SLAVE2 
--exec-before=/pre-fail.sh 
--exec-after=/post-fail.sh
Failover Roberto Polli - roberto.polli@par-tec.it
Fabric Orchestrator
You can try the content of those slides downloading code and baked docker
images from here:
git clone https://github.com/ioggstream/mysql-community
cd mysql-community/fabric
docker-compose scale fabric=1 slave=4
Let me know after the talk!
Fabric Roberto Polli - roberto.polli@par-tec.it
Fabric - HLA
A python framework for managing, replicating and scaling mysql servers.
• aggregate servers in high availability groups
• configure single-master replication topologies
• monitor and heal failures
• director for rw/split
• fabric connectors (caching db topology)
Fabric Roberto Polli - roberto.polli@par-tec.it
Fabric - HLA
Fabric Roberto Polli - roberto.polli@par-tec.it
mysqlfabric
mysqlfabric commands:
• manage - setup and manage fabric service
• group - administer high availability groups
• server - manage, clone, provision and decommission servers
• provider - manage server providers (eg. openstack, ...)
Further functionalities:
• statistics, dump, role, user,
• threat, snapshot, event
Fabric Roberto Polli - roberto.polli@par-tec.it
Manage
Setup and administration is done via
mysqlfabric manage [setup|start|ping|stop|teardown]
Configure in /etc/fabric.cfg
• listening ports for xmlrpc service
• admin user (eg. fabric to manage servers)
• applicative user credentials
• db to store fabric data
Setup and start the service
mysqlfabric manage setup
mysqlfabric manage start --daemon
Fabric Roberto Polli - roberto.polli@par-tec.it
Replication Groups
Groups are managed via
mysqlfabric group [create|add|promote|activate] $GROUP
Crete an High Availability group and add a bunch of servers.
mysqlfabric group create $GROUP
mysqlfabric group add $GROUP my-host-1:3306
mysqlfabric group add $GROUP my-host-2:3306
mysqlfabric group add $GROUP my-host-3:3306
Fabric Roberto Polli - roberto.polli@par-tec.it
Replication Groups
Promote one server as a master, eventually picking one.
mysqlfabric group promote $GROUP [--slave_id=UUID]
Show the server group
mysqlfabric group lookup_servers ha
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
server_uuid address status mode weight
------------ ----------- --------- --------- ------
2cda..110015 172.17.0.21 PRIMARY READ_WRITE 1.0
2cda..110016 172.17.0.22 SECONDARY READ_ONLY 1.0
2cda..110017 172.17.0.23 SECONDARY READ_ONLY 1.0
Fabric Roberto Polli - roberto.polli@par-tec.it
Replication Groups
Fabric support spare servers.
mysqlfabric server set_status $UUID spare
Monitoring failover and deactivating the master.
mysqlfabric group activate $GROUP
mysqladmin -h $MASTER shutdown
Checking group health we’ll see that...
mysqlfabric group health ha
uuid is_alive status ..error status..io_error sql_error
----------- -------- --------- --- --- --- --- -------- ---------
2cd..110015 0 FAULTY 0 0 0 0 False False
2cd..110016 1 SECONDARY 0 0 0 0 False False
2cd..110017 1 PRIMARY 0 0 0 0 False False
Fabric Roberto Polli - roberto.polli@par-tec.it
Connecting programmatically
from mysql.connector import connect, fabric
# Ask for a connection to the fabric server.
c = connect(fabric={host: .., port: .., user: ..},
autocommit=True,
database=’sample’, **sql_user)
# Fabric will point you to a suitable host
# - in this case the master - of the given
# group
c.set_property(mode=fabric.MODE_READWRITE, group="my-cluster-1")
Fabric Roberto Polli - roberto.polli@par-tec.it
Provisioning a new slave
Fabric uses mysqldump + mysql internally to clone a new slave.
# Always reset TARGET configuration
# before reinitializing
mysql -e ’
SET @SESSION.SQL_LOG_BIN=0;
STOP SLAVE;
RESET MASTER;
’
Cloning doesn’t attach the server to a group, nor starts the replica.
mysqlfabric server clone $GROUP $TARGET
Provisioning Roberto Polli - roberto.polli@par-tec.it
Reingesting a failed master
Reingesting a failed master is not trivial.
• non-replicated transactions;
• corrupt data;
Always reset it!
mysqlfabric server set_status f484...110039 spare
mysqlfabric server set_status f484...110039 secondary
Provisioning Roberto Polli - roberto.polli@par-tec.it
Fabric in the Cloud
Fabric can provision new machines via eg. Openstack API.
We implemented a DockerProvider: deploy containers, not machines.
# mysql.fabric.providers.dockerprovider
class MachineManager(AbstractMachineManager):
"""Manage Docker Containers."""
def create(self, parameters, wait_spawning):
...
def search(self, generic_filters, meta_filters):
...
def destroy(self, machine_uuid):
...
Fabric in the Cloud Roberto Polli - roberto.polli@par-tec.it
Docker Provisioning
# Register a provider (requires docker in http)
mysqlfabric provider register mydocker
user password
http://172.17.42.1:2375
--provider_type=DOCKER
# Create or remove Containers
mysqlfabric server create mydocker
--image=name=ioggstream/mysql-community
--flavor=name=v1
--meta=command="mysqld --log-bin=foo"
# List servers
mysqlfabric server list docker
Fabric in the Cloud Roberto Polli - roberto.polli@par-tec.it
Next steps
Openstack interface supports machine snapshots via NovaClient.
Docker volumes doesn’t support snapshots, but something is moving (in the
experimental tree).
VM Snapshot alternatives require root access to docker host and a FLUSH
TABLES on the source container:
• rsync on volumes
• implement snapshot via docker volume plugins
Fabric in the Cloud Roberto Polli - roberto.polli@par-tec.it
Wrap Up
• Replication is easier with Fabric and MySQL 5.6
• You can clone servers
• Failover is just one command ahead
• Don’t re-ingest failed masters (luckily;)
• Try Fabric with Docker!
• Play with docker volumes
Fabric in the Cloud Roberto Polli - roberto.polli@par-tec.it
That’s all folks!
Thank you for the attention!
Roberto Polli - roberto.polli@par-tec.it
Fabric in the Cloud Roberto Polli - roberto.polli@par-tec.it

More Related Content

What's hot (20)

Software Packaging with RPM
Software Packaging with RPM
Schalk Cronjé
 
Configure, Pack and Distribute: An RPM Creation Workshop
Configure, Pack and Distribute: An RPM Creation Workshop
Novell
 
nftables - the evolution of Linux Firewall
nftables - the evolution of Linux Firewall
Marian Marinov
 
zebra & openconfigd Introduction
zebra & openconfigd Introduction
Kentaro Ebisawa
 
Iptablesrocks
Iptablesrocks
qwer_asdf
 
Lab manual
Lab manual
BNilavara
 
Linux Networking Explained
Linux Networking Explained
Thomas Graf
 
Building RT image with Yocto
Building RT image with Yocto
Alexandre LAHAYE
 
Generator Tricks for Systems Programmers
Generator Tricks for Systems Programmers
David Beazley (Dabeaz LLC)
 
Kernel Recipes 2019 - Analyzing changes to the binary interface exposed by th...
Kernel Recipes 2019 - Analyzing changes to the binary interface exposed by th...
Anne Nicolas
 
Deeper dive in Docker Overlay Networks
Deeper dive in Docker Overlay Networks
Laurent Bernaille
 
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Anne Nicolas
 
Cisco CCNA OSPF IPV6 Configuration
Cisco CCNA OSPF IPV6 Configuration
Hamed Moghaddam
 
Juniper JNCIA – Juniper Floating Static Route Configuration
Juniper JNCIA – Juniper Floating Static Route Configuration
Hamed Moghaddam
 
Fluentd Hacking Guide at RubyKaigi 2014
Fluentd Hacking Guide at RubyKaigi 2014
Naotoshi Seo
 
Cisco CCNA IPV6 Static Configuration
Cisco CCNA IPV6 Static Configuration
Hamed Moghaddam
 
2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku
ronnywang_tw
 
openPOWERLINK over Xenomai
openPOWERLINK over Xenomai
Alexandre LAHAYE
 
Sockets and Socket-Buffer
Sockets and Socket-Buffer
Sourav Punoriyar
 
The Internals of "Hello World" Program
The Internals of "Hello World" Program
National Cheng Kung University
 
Software Packaging with RPM
Software Packaging with RPM
Schalk Cronjé
 
Configure, Pack and Distribute: An RPM Creation Workshop
Configure, Pack and Distribute: An RPM Creation Workshop
Novell
 
nftables - the evolution of Linux Firewall
nftables - the evolution of Linux Firewall
Marian Marinov
 
zebra & openconfigd Introduction
zebra & openconfigd Introduction
Kentaro Ebisawa
 
Iptablesrocks
Iptablesrocks
qwer_asdf
 
Linux Networking Explained
Linux Networking Explained
Thomas Graf
 
Building RT image with Yocto
Building RT image with Yocto
Alexandre LAHAYE
 
Kernel Recipes 2019 - Analyzing changes to the binary interface exposed by th...
Kernel Recipes 2019 - Analyzing changes to the binary interface exposed by th...
Anne Nicolas
 
Deeper dive in Docker Overlay Networks
Deeper dive in Docker Overlay Networks
Laurent Bernaille
 
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Anne Nicolas
 
Cisco CCNA OSPF IPV6 Configuration
Cisco CCNA OSPF IPV6 Configuration
Hamed Moghaddam
 
Juniper JNCIA – Juniper Floating Static Route Configuration
Juniper JNCIA – Juniper Floating Static Route Configuration
Hamed Moghaddam
 
Fluentd Hacking Guide at RubyKaigi 2014
Fluentd Hacking Guide at RubyKaigi 2014
Naotoshi Seo
 
Cisco CCNA IPV6 Static Configuration
Cisco CCNA IPV6 Static Configuration
Hamed Moghaddam
 
2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku
ronnywang_tw
 
openPOWERLINK over Xenomai
openPOWERLINK over Xenomai
Alexandre LAHAYE
 

Viewers also liked (20)

Www Kitebird Com Articles Pydbapi Html Toc 1
Www Kitebird Com Articles Pydbapi Html Toc 1
AkramWaseem
 
Rethink db with Python
Rethink db with Python
Prabhu Raghav
 
Succumbing to the Python in Financial Markets
Succumbing to the Python in Financial Markets
dcerezo
 
"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014
Henning Jacobs
 
MySQL User Conference 2009: Python and MySQL
MySQL User Conference 2009: Python and MySQL
Ted Leung
 
Fabric (python)
Fabric (python)
Fanani M. Ihsan
 
Fabric
Fabric
Joe_noh
 
Fabric-让部署变得简单
Fabric-让部署变得简单
Eric Lo
 
fabfile.py
fabfile.py
Corey Oordt
 
Automation - fabric, django and more
Automation - fabric, django and more
Ilian Iliev
 
Fabric - a server management tool from Instagram
Fabric - a server management tool from Instagram
Jay Ren
 
DevOps with Fabric
DevOps with Fabric
Simone Federici
 
Python for Derivative Analytics
Python for Derivative Analytics
Alicia G
 
Relational Database Access with Python ‘sans’ ORM
Relational Database Access with Python ‘sans’ ORM
Mark Rees
 
Python Deployment with Fabric
Python Deployment with Fabric
andymccurdy
 
Pythonic Deployment with Fabric 0.9
Pythonic Deployment with Fabric 0.9
Corey Oordt
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQL
Peter Eisentraut
 
Python Utilities for Managing MySQL Databases
Python Utilities for Managing MySQL Databases
Mats Kindahl
 
PostgreSQLとPythonとSQL
PostgreSQLとPythonとSQL
Satoshi Yamada
 
Fabric
Fabric
Calvin Cheng
 
Www Kitebird Com Articles Pydbapi Html Toc 1
Www Kitebird Com Articles Pydbapi Html Toc 1
AkramWaseem
 
Rethink db with Python
Rethink db with Python
Prabhu Raghav
 
Succumbing to the Python in Financial Markets
Succumbing to the Python in Financial Markets
dcerezo
 
"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014
Henning Jacobs
 
MySQL User Conference 2009: Python and MySQL
MySQL User Conference 2009: Python and MySQL
Ted Leung
 
Fabric-让部署变得简单
Fabric-让部署变得简单
Eric Lo
 
Automation - fabric, django and more
Automation - fabric, django and more
Ilian Iliev
 
Fabric - a server management tool from Instagram
Fabric - a server management tool from Instagram
Jay Ren
 
Python for Derivative Analytics
Python for Derivative Analytics
Alicia G
 
Relational Database Access with Python ‘sans’ ORM
Relational Database Access with Python ‘sans’ ORM
Mark Rees
 
Python Deployment with Fabric
Python Deployment with Fabric
andymccurdy
 
Pythonic Deployment with Fabric 0.9
Pythonic Deployment with Fabric 0.9
Corey Oordt
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQL
Peter Eisentraut
 
Python Utilities for Managing MySQL Databases
Python Utilities for Managing MySQL Databases
Mats Kindahl
 
PostgreSQLとPythonとSQL
PostgreSQLとPythonとSQL
Satoshi Yamada
 
Ad

Similar to Scaling mysql with python (and Docker). (20)

DBA だってもっと効率化したい!〜最近の自動化事情とOracle Database〜
DBA だってもっと効率化したい!〜最近の自動化事情とOracle Database〜
Michitoshi Yoshida
 
MySQL InnoDB Cluster / ReplicaSet - Tutorial
MySQL InnoDB Cluster / ReplicaSet - Tutorial
Miguel Araújo
 
MySQL InnoDB Cluster / ReplicaSet - Tutorial
MySQL InnoDB Cluster / ReplicaSet - Tutorial
Kenny Gryp
 
Percona toolkit
Percona toolkit
Karwin Software Solutions LLC
 
MySQL 5.7 innodb_enhance_partii_20160527
MySQL 5.7 innodb_enhance_partii_20160527
Saewoong Lee
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Miguel Araújo
 
User Camp High Availability Presentation
User Camp High Availability Presentation
sankalita chakraborty
 
The SaltStack Pub Crawl - Fosscomm 2016
The SaltStack Pub Crawl - Fosscomm 2016
effie mouzeli
 
Build Automation 101
Build Automation 101
Martin Jackson
 
10 Lessons Learned from using Kafka in 1000 microservices - ScalaUA
10 Lessons Learned from using Kafka in 1000 microservices - ScalaUA
Natan Silnitsky
 
MySQL Replication Basics -Ohio Linux Fest 2016
MySQL Replication Basics -Ohio Linux Fest 2016
Dave Stokes
 
MySQL Parallel Replication: All the 5.7 and 8.0 Details (LOGICAL_CLOCK)
MySQL Parallel Replication: All the 5.7 and 8.0 Details (LOGICAL_CLOCK)
Jean-François Gagné
 
Curso de MySQL 5.7
Curso de MySQL 5.7
Eduardo Legatti
 
Introduction to MariaDB
Introduction to MariaDB
JongJin Lee
 
Chicago Docker Meetup Presentation - Mediafly
Chicago Docker Meetup Presentation - Mediafly
Mediafly
 
MySQL Utilities -- Cool Tools For You: PHP World Nov 16 2016
MySQL Utilities -- Cool Tools For You: PHP World Nov 16 2016
Dave Stokes
 
MySQL Replication Update -- Zendcon 2016
MySQL Replication Update -- Zendcon 2016
Dave Stokes
 
MongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
MongoDB
 
The Switch as a Server - PuppetConf 2014
The Switch as a Server - PuppetConf 2014
Puppet
 
Oracle 11g R2 RAC setup on rhel 5.0
Oracle 11g R2 RAC setup on rhel 5.0
Santosh Kangane
 
DBA だってもっと効率化したい!〜最近の自動化事情とOracle Database〜
DBA だってもっと効率化したい!〜最近の自動化事情とOracle Database〜
Michitoshi Yoshida
 
MySQL InnoDB Cluster / ReplicaSet - Tutorial
MySQL InnoDB Cluster / ReplicaSet - Tutorial
Miguel Araújo
 
MySQL InnoDB Cluster / ReplicaSet - Tutorial
MySQL InnoDB Cluster / ReplicaSet - Tutorial
Kenny Gryp
 
MySQL 5.7 innodb_enhance_partii_20160527
MySQL 5.7 innodb_enhance_partii_20160527
Saewoong Lee
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Miguel Araújo
 
User Camp High Availability Presentation
User Camp High Availability Presentation
sankalita chakraborty
 
The SaltStack Pub Crawl - Fosscomm 2016
The SaltStack Pub Crawl - Fosscomm 2016
effie mouzeli
 
10 Lessons Learned from using Kafka in 1000 microservices - ScalaUA
10 Lessons Learned from using Kafka in 1000 microservices - ScalaUA
Natan Silnitsky
 
MySQL Replication Basics -Ohio Linux Fest 2016
MySQL Replication Basics -Ohio Linux Fest 2016
Dave Stokes
 
MySQL Parallel Replication: All the 5.7 and 8.0 Details (LOGICAL_CLOCK)
MySQL Parallel Replication: All the 5.7 and 8.0 Details (LOGICAL_CLOCK)
Jean-François Gagné
 
Introduction to MariaDB
Introduction to MariaDB
JongJin Lee
 
Chicago Docker Meetup Presentation - Mediafly
Chicago Docker Meetup Presentation - Mediafly
Mediafly
 
MySQL Utilities -- Cool Tools For You: PHP World Nov 16 2016
MySQL Utilities -- Cool Tools For You: PHP World Nov 16 2016
Dave Stokes
 
MySQL Replication Update -- Zendcon 2016
MySQL Replication Update -- Zendcon 2016
Dave Stokes
 
MongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
MongoDB
 
The Switch as a Server - PuppetConf 2014
The Switch as a Server - PuppetConf 2014
Puppet
 
Oracle 11g R2 RAC setup on rhel 5.0
Oracle 11g R2 RAC setup on rhel 5.0
Santosh Kangane
 
Ad

More from Roberto Polli (20)

Ratelimit Headers for HTTP
Ratelimit Headers for HTTP
Roberto Polli
 
Interoperability rules for an European API ecosystem: do we still need SOAP?
Interoperability rules for an European API ecosystem: do we still need SOAP?
Roberto Polli
 
Docker - virtualizzazione leggera
Docker - virtualizzazione leggera
Roberto Polli
 
Just one-shade-of-openstack
Just one-shade-of-openstack
Roberto Polli
 
Test Drive Deployment with python and nosetest
Test Drive Deployment with python and nosetest
Roberto Polli
 
Tox as project descriptor.
Tox as project descriptor.
Roberto Polli
 
Statistics 101 for System Administrators
Statistics 101 for System Administrators
Roberto Polli
 
Will iPython replace bash?
Will iPython replace bash?
Roberto Polli
 
Pysmbc Python C Modules are Easy
Pysmbc Python C Modules are Easy
Roberto Polli
 
Git gestione comoda del repository
Git gestione comoda del repository
Roberto Polli
 
Testing with my sql embedded
Testing with my sql embedded
Roberto Polli
 
Servizi di messaging & collaboration in mobilità: Il panorama open source
Servizi di messaging & collaboration in mobilità: Il panorama open source
Roberto Polli
 
Funambol al Linux Day 2009
Funambol al Linux Day 2009
Roberto Polli
 
ICalendar RFC2445 - draft1
ICalendar RFC2445 - draft1
Roberto Polli
 
Presenting CalDAV (draft 1)
Presenting CalDAV (draft 1)
Roberto Polli
 
Integrating Funambol with CalDAV and LDAP
Integrating Funambol with CalDAV and LDAP
Roberto Polli
 
ultimo-miglio-v3
ultimo-miglio-v3
Roberto Polli
 
Ultimo Miglio v2
Ultimo Miglio v2
Roberto Polli
 
Ultimo Miglio
Ultimo Miglio
Roberto Polli
 
ds risparmio energetico
ds risparmio energetico
Roberto Polli
 
Ratelimit Headers for HTTP
Ratelimit Headers for HTTP
Roberto Polli
 
Interoperability rules for an European API ecosystem: do we still need SOAP?
Interoperability rules for an European API ecosystem: do we still need SOAP?
Roberto Polli
 
Docker - virtualizzazione leggera
Docker - virtualizzazione leggera
Roberto Polli
 
Just one-shade-of-openstack
Just one-shade-of-openstack
Roberto Polli
 
Test Drive Deployment with python and nosetest
Test Drive Deployment with python and nosetest
Roberto Polli
 
Tox as project descriptor.
Tox as project descriptor.
Roberto Polli
 
Statistics 101 for System Administrators
Statistics 101 for System Administrators
Roberto Polli
 
Will iPython replace bash?
Will iPython replace bash?
Roberto Polli
 
Pysmbc Python C Modules are Easy
Pysmbc Python C Modules are Easy
Roberto Polli
 
Git gestione comoda del repository
Git gestione comoda del repository
Roberto Polli
 
Testing with my sql embedded
Testing with my sql embedded
Roberto Polli
 
Servizi di messaging & collaboration in mobilità: Il panorama open source
Servizi di messaging & collaboration in mobilità: Il panorama open source
Roberto Polli
 
Funambol al Linux Day 2009
Funambol al Linux Day 2009
Roberto Polli
 
ICalendar RFC2445 - draft1
ICalendar RFC2445 - draft1
Roberto Polli
 
Presenting CalDAV (draft 1)
Presenting CalDAV (draft 1)
Roberto Polli
 
Integrating Funambol with CalDAV and LDAP
Integrating Funambol with CalDAV and LDAP
Roberto Polli
 
ds risparmio energetico
ds risparmio energetico
Roberto Polli
 

Recently uploaded (20)

Vigilanti-Cura-Protecting-the-Faith.pptx
Vigilanti-Cura-Protecting-the-Faith.pptx
secretarysocom
 
CBUSDAW - Ash Lewis - Reducing LLM Hallucinations
CBUSDAW - Ash Lewis - Reducing LLM Hallucinations
Jason Packer
 
最新版美国威斯康星大学绿湾分校毕业证(UWGB毕业证书)原版定制
最新版美国威斯康星大学绿湾分校毕业证(UWGB毕业证书)原版定制
Taqyea
 
NOC Services for maintaining network as MSA.ppt
NOC Services for maintaining network as MSA.ppt
ankurnagar22
 
The it.com Domains Brand Book for registrars, domain resellers and hosting co...
The it.com Domains Brand Book for registrars, domain resellers and hosting co...
it.com Domains
 
PPT 18.03.2023.pptx for i smart programme
PPT 18.03.2023.pptx for i smart programme
AbhimanShastry
 
Internet & Protocols : A Blueprint of the Internet System
Internet & Protocols : A Blueprint of the Internet System
cpnabil59
 
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
treyka
 
Unlocking Business Growth Through Targeted Social Engagement
Unlocking Business Growth Through Targeted Social Engagement
Digital Guider
 
Google_Cloud_Computing_Fundamentals.pptx
Google_Cloud_Computing_Fundamentals.pptx
ektadangwal2005
 
Topic 1 Foundational IT Infrastructure_.pptx
Topic 1 Foundational IT Infrastructure_.pptx
oneillp100
 
Communio-et-Progressio - Catholic Church Document on communication
Communio-et-Progressio - Catholic Church Document on communication
secretarysocom
 
simple-presentationtestingdocument2007.pptx
simple-presentationtestingdocument2007.pptx
ashokjayapal
 
05-introduction-to-operating-systems.pptx
05-introduction-to-operating-systems.pptx
pepecompany1
 
AI theory work for students to understand the logic
AI theory work for students to understand the logic
areeba15775n
 
COMPUTER ETHICS AND CRIME.......................................................
COMPUTER ETHICS AND CRIME.......................................................
FOOLKUMARI
 
最新版英国北安普顿大学毕业证(UoN毕业证书)原版定制
最新版英国北安普顿大学毕业证(UoN毕业证书)原版定制
Taqyea
 
Quantiuwewe e3er14e we3223 32222 m2.pptx
Quantiuwewe e3er14e we3223 32222 m2.pptx
cyberesearchprof
 
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Behzad Hussain
 
Common Pitfalls in Magento to Shopify Migration and How to Avoid Them.pdf
Common Pitfalls in Magento to Shopify Migration and How to Avoid Them.pdf
CartCoders
 
Vigilanti-Cura-Protecting-the-Faith.pptx
Vigilanti-Cura-Protecting-the-Faith.pptx
secretarysocom
 
CBUSDAW - Ash Lewis - Reducing LLM Hallucinations
CBUSDAW - Ash Lewis - Reducing LLM Hallucinations
Jason Packer
 
最新版美国威斯康星大学绿湾分校毕业证(UWGB毕业证书)原版定制
最新版美国威斯康星大学绿湾分校毕业证(UWGB毕业证书)原版定制
Taqyea
 
NOC Services for maintaining network as MSA.ppt
NOC Services for maintaining network as MSA.ppt
ankurnagar22
 
The it.com Domains Brand Book for registrars, domain resellers and hosting co...
The it.com Domains Brand Book for registrars, domain resellers and hosting co...
it.com Domains
 
PPT 18.03.2023.pptx for i smart programme
PPT 18.03.2023.pptx for i smart programme
AbhimanShastry
 
Internet & Protocols : A Blueprint of the Internet System
Internet & Protocols : A Blueprint of the Internet System
cpnabil59
 
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
treyka
 
Unlocking Business Growth Through Targeted Social Engagement
Unlocking Business Growth Through Targeted Social Engagement
Digital Guider
 
Google_Cloud_Computing_Fundamentals.pptx
Google_Cloud_Computing_Fundamentals.pptx
ektadangwal2005
 
Topic 1 Foundational IT Infrastructure_.pptx
Topic 1 Foundational IT Infrastructure_.pptx
oneillp100
 
Communio-et-Progressio - Catholic Church Document on communication
Communio-et-Progressio - Catholic Church Document on communication
secretarysocom
 
simple-presentationtestingdocument2007.pptx
simple-presentationtestingdocument2007.pptx
ashokjayapal
 
05-introduction-to-operating-systems.pptx
05-introduction-to-operating-systems.pptx
pepecompany1
 
AI theory work for students to understand the logic
AI theory work for students to understand the logic
areeba15775n
 
COMPUTER ETHICS AND CRIME.......................................................
COMPUTER ETHICS AND CRIME.......................................................
FOOLKUMARI
 
最新版英国北安普顿大学毕业证(UoN毕业证书)原版定制
最新版英国北安普顿大学毕业证(UoN毕业证书)原版定制
Taqyea
 
Quantiuwewe e3er14e we3223 32222 m2.pptx
Quantiuwewe e3er14e we3223 32222 m2.pptx
cyberesearchprof
 
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Behzad Hussain
 
Common Pitfalls in Magento to Shopify Migration and How to Avoid Them.pdf
Common Pitfalls in Magento to Shopify Migration and How to Avoid Them.pdf
CartCoders
 

Scaling mysql with python (and Docker).

  • 1. Scaling MySQL with python EuroPython 2015, Bilbao Roberto Polli - [email protected] Par-Tec Spa - Rome Operation Unit P.zza S. Benedetto da Norcia, 33 00040, Pomezia RM - www.par-tec.it 20 July 2015 Roberto Polli - [email protected]
  • 3. Who? What? Why? Roberto Polli - Solutions Architect @ par-tec.it. Loves writing in C, Java and Python. Red Hat Certified Engineer and Virtualization Administrator. Par-Tec – Proud sponsor of this talk ;) Contributes to various FLOSS. Provides: • IT Infrastructure & Services expertise; • Business Intelligence solutions; • Vertical Applications for the financial market. Manage, replicate, scale MySQL is easy with python (and Docker). Intro Roberto Polli - [email protected]
  • 4. MySQL • Single-process, multi-thread • 2-tier architecture • Connection + SQL • Storage Backend • Greatly improved in 5.6 • More to come in 5.7: Dynamic Buffers & Multi-source replication MySQL Roberto Polli - [email protected]
  • 5. MySQL Many Backends aka Storage Engines: • InnoDB1 : ACID, MVCC2 , Redo|Undo logs; • Memory: fast & volatile; • NDB: MySQL Cluster, network distributed, auto-sharded; Replication based on changelog files (binary logs). 1 default 2 Multi Versioning Concurrency Control MySQL Roberto Polli - [email protected]
  • 6. Manage & Use a database Monitor: • Database size: Tables, Indexes, Binary Logs • Replication inconsistencies • Failover Connect: • Native Drivers • for Python 2.6, 2.7 and 3.3 • SSL & compression • Orchestrate Download scripts from some blog, add random queries from docs.mysql.com, Shake. MySQL Roberto Polli - [email protected]
  • 7. MySQL Utilities A python package with utilities, drivers, fabric orchestrator $ wget http://bit.ly/1CxNuZe $ tar xf mysql-utilities-1.6.1.tar.gz $ cd mysql-utilities-1.6.1 $ python setup.py install mysql-utilities-1.6.1 |-- mysql |-- connector | |-- django | ‘-- fabric |-- fabric | |-- protocols | |-- providers | ‘-- services ‘-- utilities Python Utilities Roberto Polli - [email protected]
  • 8. MySQL Utilities: Start with mysqluc for a single entrypoint • entrypoint for all utilities • contextual help • TAB completion Or access each utility separately. Specify server URIs as user:pass@host[:port]. Python Utilities Roberto Polli - [email protected]
  • 9. MySQL Utilities mysqluc> help utilities Utility Description ----------------- -------------------------------------------------------- mysqlauditadmin audit log maintenance utility mysqlauditgrep audit log search utility ... mysqldbcompare compare databases for consistency ... mysqldiskusage show disk usage for databases mysqlfailover automatic replication health monitoring and failover Python Utilities Roberto Polli - [email protected]
  • 10. Disk Usage A single command to show all disk usage infos (excluded system logs) $ mysqldiskusage --all --server=$SERVER ... | db_name | total | | akonadi | 969150919 | ... Total database disk usage = 971233529 bytes or 926,24 MB ... | ib_logfile0 | 67108864 | | ib_logfile1 | 67108864 | | ibdata1 | 44040192 | Total size of InnoDB files = 178257920 bytes or 170,00 MB Python Utilities Roberto Polli - [email protected]
  • 11. Connectors from django.db.backends import BaseDatabaseValidation if django.VERSION < (1, 7): from django.db import models else: from django.core import checks from django.db import connection class DatabaseValidation(BaseDatabaseValidation): if django.VERSION < (1, 7): Connectors Roberto Polli - [email protected]
  • 12. Log-based Replication: Master Replication is asynchronous or semi − synchronous. A Master with a Unique Server ID • produces a changelog named binary log; • assign each transaction a Global Transaction ID; • grants access to a replica user; Optionally: • stores replication infos in ACID tables; • may track slave-updates; • waits for one slave ack on semi − synchronous replication; Replication Roberto Polli - [email protected]
  • 13. Log-based Replication: Slave Slave • connects to the master as the replica user; • retrieves the binlog; • applies the changes; • ∀ slave ∃! master; Optionally • forbids local changes from non-root users; If binlogs have been purged, you need to import the master database first! Replication Roberto Polli - [email protected]
  • 14. Benefits of replication • Availability. • Scaling reads with R/W split. • Scaling reads differencing indexes. • Backup and data warehouse strategies. # mysqlrplshow output # # Replication Topology Graph s-1.docker:3306 (MASTER) | +--- s-3.docker:3306 - (SLAVE) | +--- s-4.docker:3306 - (SLAVE) Replication Roberto Polli - [email protected]
  • 15. mysqlreplicate Configuring replication with one command: mysqlreplicate -b --pedantic --master=$MASTER --slave=$SLAVE --rpl-user=repl:rpass # master uuid = 2cda700... # slave uuid = 7520cf0... # Checking for binary logging on master... # Granting replication access to # replication user... # Connecting slave to master... # CHANGE MASTER TO MASTER_HOST = ’172.17.0.5’,... • preliminary checks; • create replica user on the master; • point the slave to the master; • start loading the first available transaction in binlogs; Replication Roberto Polli - [email protected]
  • 16. Initialize new slaves Beware! 1. Avoid replicating user provisioning and other host-related setup! @SESSION.SQL_LOG_BIN=0; 2. Binlogs are usually retained for a fixed period - eg 15days. To provision new slave after that period you must: • start from a backup or a snapshot; • apply the binlogs. mysqldbexport >> data.sql --server=root:pass@master --rpl-user=repl:rpass --export=both --rpl=master --all mysqldbimport --server=root:pass@slave data.sql Replication Roberto Polli - [email protected]
  • 17. Failover Basics From replication to high availability, automagically discovering slaves. • promote the most updated slave • reconfigure the others • disable the master • eventually fix ip-address # Read carefully the docs of # mysqlfailover and of all # the supported parameters! mysqlfailover --master=$MASTER --discover-slaves-login=root:password --candidates=$SLAVE1,$SLAVE2 --exec-before=/pre-fail.sh --exec-after=/post-fail.sh Failover Roberto Polli - [email protected]
  • 18. Fabric Orchestrator You can try the content of those slides downloading code and baked docker images from here: git clone https://github.com/ioggstream/mysql-community cd mysql-community/fabric docker-compose scale fabric=1 slave=4 Let me know after the talk! Fabric Roberto Polli - [email protected]
  • 19. Fabric - HLA A python framework for managing, replicating and scaling mysql servers. • aggregate servers in high availability groups • configure single-master replication topologies • monitor and heal failures • director for rw/split • fabric connectors (caching db topology) Fabric Roberto Polli - [email protected]
  • 21. mysqlfabric mysqlfabric commands: • manage - setup and manage fabric service • group - administer high availability groups • server - manage, clone, provision and decommission servers • provider - manage server providers (eg. openstack, ...) Further functionalities: • statistics, dump, role, user, • threat, snapshot, event Fabric Roberto Polli - [email protected]
  • 22. Manage Setup and administration is done via mysqlfabric manage [setup|start|ping|stop|teardown] Configure in /etc/fabric.cfg • listening ports for xmlrpc service • admin user (eg. fabric to manage servers) • applicative user credentials • db to store fabric data Setup and start the service mysqlfabric manage setup mysqlfabric manage start --daemon Fabric Roberto Polli - [email protected]
  • 23. Replication Groups Groups are managed via mysqlfabric group [create|add|promote|activate] $GROUP Crete an High Availability group and add a bunch of servers. mysqlfabric group create $GROUP mysqlfabric group add $GROUP my-host-1:3306 mysqlfabric group add $GROUP my-host-2:3306 mysqlfabric group add $GROUP my-host-3:3306 Fabric Roberto Polli - [email protected]
  • 24. Replication Groups Promote one server as a master, eventually picking one. mysqlfabric group promote $GROUP [--slave_id=UUID] Show the server group mysqlfabric group lookup_servers ha Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e Time-To-Live: 1 server_uuid address status mode weight ------------ ----------- --------- --------- ------ 2cda..110015 172.17.0.21 PRIMARY READ_WRITE 1.0 2cda..110016 172.17.0.22 SECONDARY READ_ONLY 1.0 2cda..110017 172.17.0.23 SECONDARY READ_ONLY 1.0 Fabric Roberto Polli - [email protected]
  • 25. Replication Groups Fabric support spare servers. mysqlfabric server set_status $UUID spare Monitoring failover and deactivating the master. mysqlfabric group activate $GROUP mysqladmin -h $MASTER shutdown Checking group health we’ll see that... mysqlfabric group health ha uuid is_alive status ..error status..io_error sql_error ----------- -------- --------- --- --- --- --- -------- --------- 2cd..110015 0 FAULTY 0 0 0 0 False False 2cd..110016 1 SECONDARY 0 0 0 0 False False 2cd..110017 1 PRIMARY 0 0 0 0 False False Fabric Roberto Polli - [email protected]
  • 26. Connecting programmatically from mysql.connector import connect, fabric # Ask for a connection to the fabric server. c = connect(fabric={host: .., port: .., user: ..}, autocommit=True, database=’sample’, **sql_user) # Fabric will point you to a suitable host # - in this case the master - of the given # group c.set_property(mode=fabric.MODE_READWRITE, group="my-cluster-1") Fabric Roberto Polli - [email protected]
  • 27. Provisioning a new slave Fabric uses mysqldump + mysql internally to clone a new slave. # Always reset TARGET configuration # before reinitializing mysql -e ’ SET @SESSION.SQL_LOG_BIN=0; STOP SLAVE; RESET MASTER; ’ Cloning doesn’t attach the server to a group, nor starts the replica. mysqlfabric server clone $GROUP $TARGET Provisioning Roberto Polli - [email protected]
  • 28. Reingesting a failed master Reingesting a failed master is not trivial. • non-replicated transactions; • corrupt data; Always reset it! mysqlfabric server set_status f484...110039 spare mysqlfabric server set_status f484...110039 secondary Provisioning Roberto Polli - [email protected]
  • 29. Fabric in the Cloud Fabric can provision new machines via eg. Openstack API. We implemented a DockerProvider: deploy containers, not machines. # mysql.fabric.providers.dockerprovider class MachineManager(AbstractMachineManager): """Manage Docker Containers.""" def create(self, parameters, wait_spawning): ... def search(self, generic_filters, meta_filters): ... def destroy(self, machine_uuid): ... Fabric in the Cloud Roberto Polli - [email protected]
  • 30. Docker Provisioning # Register a provider (requires docker in http) mysqlfabric provider register mydocker user password http://172.17.42.1:2375 --provider_type=DOCKER # Create or remove Containers mysqlfabric server create mydocker --image=name=ioggstream/mysql-community --flavor=name=v1 --meta=command="mysqld --log-bin=foo" # List servers mysqlfabric server list docker Fabric in the Cloud Roberto Polli - [email protected]
  • 31. Next steps Openstack interface supports machine snapshots via NovaClient. Docker volumes doesn’t support snapshots, but something is moving (in the experimental tree). VM Snapshot alternatives require root access to docker host and a FLUSH TABLES on the source container: • rsync on volumes • implement snapshot via docker volume plugins Fabric in the Cloud Roberto Polli - [email protected]
  • 32. Wrap Up • Replication is easier with Fabric and MySQL 5.6 • You can clone servers • Failover is just one command ahead • Don’t re-ingest failed masters (luckily;) • Try Fabric with Docker! • Play with docker volumes Fabric in the Cloud Roberto Polli - [email protected]
  • 33. That’s all folks! Thank you for the attention! Roberto Polli - [email protected] Fabric in the Cloud Roberto Polli - [email protected]