SlideShare a Scribd company logo
© 2017 Percona1
Marcelo Altmann
ProxySQL para MySQL
Mais que um simples proxy - Exemplos de uso
Senior Support Engineer
MySQL & MongoDB meetup - Sao Paulo
11/05/2017
© 2017 Percona2
Agenda
© 2017 Percona3
Agenda
▪ Sobre Mim
▪ ProxySQL
•Introdução
•Features
▪Load Balance
▪Firewall
▪Query Cache
▪Integrações
▪Outros
© 2017 Percona4
Sobre Mim
© 2017 Percona5
Marcelo Altmann
▪Engenheiro de Suporte @ percona
• MySQL DBA @ IEDR (CCTLD Irlanda)
▪Oracle ACE Associate
▪Certificaçoes
•Oracle Certified Professional, MySQL 5.6 Database Administrator
•Oracle Certified Professional, MySQL 5.6 Developer
•Oracle Certified Professional, MySQL 5 Database Administrator
•Oracle Certified Professional, MySQL 5 Developer
•Oracle Certified Associate, MySQL 5.0/5.1/5.5
▪blog.marceloaltmann.com
© 2017 Percona6
ProxySQL - Introdução
© 2017 Percona7
ProxySQL - Introdução
▪ Criado e mantido por René Cannaò
•MySQL Community - Contribuidor do ano (2017)
▪Open Source
•http://www.proxysql.com/
•https://github.com/sysown/proxysql
© 2017 Percona8
ProxySQL - Introdução
▪ Instalação
•Source Code / rpm / deb / percona repo (yum / apt-get)
▪MySQL admin interface
[root@localhost ~]# mysql -u admin -padmin -P 6032 -h 127.0.0.1 --prompt='Admin> '
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)
. . .
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
Admin>
© 2017 Percona9
ProxySQL - Introdução
▪ Camadas
•Runtime - Dados que o proxySQL acessa
•Main/Memory - Camada onde executamos alterações e monitoramento
•Disk - Camada para persistir dados. SQLite3
•Config - Arquivo em disco que é lido caso o banco de dados SQLite3 nao exista.
© 2017 Percona10
ProxySQL - Introdução
▪ Tabelas
•mysql_servers - Contém a lista de servidores.
•mysql_query_rules - Contém a lista de regras para cache, reescrita e
redirecionamento de queries.
•mysql_users - Contém a lista de usuário para autenticação.
•global_variables - Contém a lista de variáveis de configuração.
© 2017 Percona11
ProxySQL - Features
© 2017 Percona12
ProxySQL - Features - Load Balance
▪ Usuários de autenticação
Admin> INSERT INTO mysql_users (username,password) VALUES ('application', 'app');
Query OK, 1 row affected (0.00 sec)
Admin> LOAD MYSQL USERS TO RUNTIME ;
Query OK, 0 rows affected (0.00 sec)
Admin> SAVE MYSQL USERS FROM RUNTIME; -- Hack para o plain-text password
Query OK, 0 rows affected (0.00 sec)
Admin> SAVE MYSQL USERS TO DISK ;
Query OK, 0 rows affected (0.01 sec)
© 2017 Percona13
ProxySQL - Features - Load Balance
▪ Redirecionamento do tráfego para lista de servidores
Admin> INSERT INTO mysql_servers (hostname) VALUES ('192.168.19.71'),('192.168.19.72'),('192.168.19.73');
Query OK, 3 rows affected (0.00 sec)
Admin> LOAD MYSQL SERVERS TO RUNTIME;
Query OK, 0 rows affected (0.01 sec)
Admin> SAVE MYSQL SERVERS TO DISK;
Query OK, 0 rows affected (0.02 sec)
© 2017 Percona14
ProxySQL - Features - Load Balance
▪ Redirecionamento do tráfego para lista de servidores
[root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT @@server_id"
+-------------+
| @@server_id |
+-------------+
| 2 |
+-------------+
[root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT @@server_id"
+-------------+
| @@server_id |
+-------------+
| 3 |
+-------------+
[root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT @@server_id"
+-------------+
| @@server_id |
+-------------+
| 1 |
+-------------+
© 2017 Percona15
ProxySQL - Features - Load Balance
▪ Dividir leituras e escritas
•Host Groups - Agrupamento de servidores
Admin> UPDATE mysql_servers SET hostgroup_id = 1 WHERE hostname <> '192.168.19.71';
Admin> SELECT hostgroup_id, hostname FROM mysql_servers;
+--------------+---------------+
| hostgroup_id | hostname |
+--------------+---------------+
| 0 | 192.168.19.71 |
| 1 | 192.168.19.72 |
| 1 | 192.168.19.73 |
+--------------+---------------+
Admin> LOAD MYSQL SERVERS TO RUNTIME;
Admin> SAVE MYSQL SERVERS TO DISK;
© 2017 Percona16
ProxySQL - Features - Load Balance
▪ Dividir leituras e escritas
Admin> SELECT username, default_hostgroup FROM mysql_users;
+-------------+-------------------+
| username | default_hostgroup |
+-------------+-------------------+
| application | 0 |
+-------------+-------------------+
1 row in set (0.00 sec)
© 2017 Percona17
ProxySQL - Features - Load Balance
▪ Dividir leituras e escritas
Admin> INSERT INTO mysql_query_rules (rule_id,active,match_digest,destination_hostgroup)
-> VALUES
-> (1,1,'^SELECT.*FOR UPDATE$',0),
-> (2,1,'^SELECT',1);
Query OK, 2 rows affected (0.00 sec)
Admin> LOAD MYSQL QUERY RULES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)
Admin> SAVE MYSQL QUERY RULES TO DISK;
Query OK, 0 rows affected (0.01 sec)
© 2017 Percona18
ProxySQL - Features - Load Balance
▪ Dividir leituras e escritas
[root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "INSERT INTO test.t1 VALUES
(@@server_id)"
[root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT @@server_id, server FROM
test.t1"
+-------------+--------+
| @@server_id | server |
+-------------+--------+
| 3 | 1 |
+-------------+--------+
[root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT @@server_id, server FROM
test.t1"
+-------------+--------+
| @@server_id | server |
+-------------+--------+
| 2 | 1 |
+-------------+--------+
© 2017 Percona19
ProxySQL - Features - Stats
▪ SHOW TABLES FROM stats;
•Stats_mysql_commands_counters - Contador baseado em comandos
•Stats_mysql_connection_pool - Conexoes por servidor
•Stats_mysql_global - Estatisticas globais
•Stats_mysql_processlist - SHOW PROCESSLIST
•Stats_mysql_query_digest - Contador agrupado por digest
•Stats_mysql_query_rules - Contador baseado nas query rules
© 2017 Percona20
ProxySQL - Features - Reescrita de query
▪ stats_mysql_query_digest
Admin> SELECT digest, digest_text, sum_time FROM stats_mysql_query_digest ORDER BY sum_time DESC LIMIT 1;
+--------------------+-----------------------------------------+----------+
| digest | digest_text | sum_time |
+--------------------+-----------------------------------------+----------+
| 0xD69E622A5052289E | SELECT * FROM world.city WHERE Name = ? | 7016461 |
+--------------------+-----------------------------------------+----------+
Admin> INSERT INTO mysql_query_rules (rule_id,active,match_pattern, replace_pattern) VALUES (3,1,
'^SELECT * FROM world.city WHERE Name = (.*)$',
'SELECT Population FROM world.city WHERE Name = 1');
Query OK, 1 row affected (0.00 sec)
Admin> LOAD MYSQL QUERY RULES TO RUNTIME;
Query OK, 0 rows affected (0.01 sec)
Admin> SAVE MYSQL QUERY RULES TO DISK;
Query OK, 0 rows affected (0.01 sec)
© 2017 Percona21
ProxySQL - Features - Reescrita de query
[root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT * FROM world.city WHERE Name =
'São Paulo'"
+------------+
| Population |
+------------+
| 9968485 |
+------------+
© 2017 Percona22
ProxySQL - Features - Firewall
▪Bloquear queries - SQL INJECTION!
•Original query: SELECT Name FROM world.city WHERE Name = ‘?’
•SQL Injection: ? = São Paulo' OR ID > 0; --
•SELECT Name FROM world.city WHERE Name = 'São Paulo' OR ID > 0; --'
Admin> SELECT username, digest, digest_text FROM stats_mysql_query_digest WHERE digest_text LIKE '% OR ID %'
ORDER BY first_seen DESC LIMIT 1;
+-------------+--------------------+------------------------------------------------------+
| username | digest | digest_text |
+-------------+--------------------+------------------------------------------------------+
| application | 0xD8AF41BF32707ABD | SELECT Name FROM world.city WHERE Name = ? OR ID > ? |
+-------------+--------------------+------------------------------------------------------+
1 row in set (0.00 sec)
© 2017 Percona23
ProxySQL - Features - Firewall
Admin> INSERT INTO mysql_query_rules (rule_id, active, digest, error_msg, apply) VALUES
(4,1,'0xD8AF41BF32707ABD','Suspeita de SQL Injection',1);
Query OK, 1 row affected (0.00 sec)
Admin> LOAD MYSQL QUERY RULES TO RUNTIME;SAVE MYSQL QUERY RULES TO DISK;
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
[root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT Name FROM
world.city WHERE Name = 'São Paulo' OR ID > 0; --' "
ERROR 1148 (42000) at line 1: Suspeita de SQL Injection
© 2017 Percona24
ProxySQL - Features - Mirror
▪Espelhar queries em outros servidores
•Testar configurações
•Standby Master - Manter buffer pool quente
•Testar workload em versões diferentes - Upgrades
•Troubleshooting
© 2017 Percona25
ProxySQL - Features - Mirror
▪Esquentar buffer pool
Admin> INSERT INTO mysql_servers (hostgroup_id, hostname) VALUES (2, '192.168.29.29');
Admin> LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK;
Admin> SELECT rule_id, match_digest, destination_hostgroup, mirror_hostgroup FROM
mysql_query_rules WHERE rule_id=2;
+---------+--------------+-----------------------+------------------+
| rule_id | match_digest | destination_hostgroup | mirror_hostgroup |
+---------+--------------+-----------------------+------------------+
| 2 | ^SELECT | 1 | NULL |
+---------+--------------+-----------------------+------------------+
Admin> UPDATE mysql_query_rules SET mirror_hostgroup=2 WHERE rule_id=2;
Admin> LOAD MYSQL QUERY RULES TO RUNTIME;
Admin> SAVE MYSQL QUERY RULES TO DISK;
© 2017 Percona26
ProxySQL - Features - Query Cache
▪Cache de queries baseado em TTL
[root@localhost ~]# sysbench --num-threads=16 --max-requests=0 --max-time=60 --test=/usr/share/doc/sysbench/tests/db/oltp.lua --mysql-
user=application --mysql-password=app --mysql-db=test --mysql-host=127.0.0.1 --mysql-port=6033 --oltp-table-size=10000 --oltp-read-only=on
--oltp-skip-trx=on --oltp-point-selects=100 --oltp-simple-ranges=1 --oltp-sum-ranges=1 --oltp-order-ranges=1 --oltp-distinct-ranges=1 run
| grep 'read/write requests'
read/write requests: 105664 (1744.81 per sec.)
Admin> SELECT count_star,sum_time,hostgroup,digest,digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC;
+------------+-----------+-----------+--------------------+----------------------------------------------------------------------+
| count_star | sum_time | hostgroup | digest | digest_text |
+------------+-----------+-----------+--------------------+----------------------------------------------------------------------+
| 100200 | 459147213 | 1 | 0xBF001A0C13781C1D | SELECT c FROM sbtest1 WHERE id=? |
| 1002 | 6533622 | 1 | 0xF7D3CD60704822A0 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? ORDER BY c |
| 1002 | 6061540 | 1 | 0x877EEAAFADF3DDF2 | SELECT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? ORDER BY c |
| 1002 | 5905677 | 1 | 0xAF7A51977DD56217 | SELECT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? |
| 1002 | 5321376 | 1 | 0x3E268CF3E75DB831 | SELECT SUM(K) FROM sbtest1 WHERE id BETWEEN ? AND ?+? |
+------------+-----------+-----------+--------------------+----------------------------------------------------------------------+
© 2017 Percona27
ProxySQL - Features - Query Cache
Admin> INSERT INTO mysql_query_rules (rule_id,active,digest,cache_ttl,apply) VALUES (5,1, '0xBF001A0C13781C1D' ,2000,1);
Query OK, 1 row affected (0.00 sec)
Admin> LOAD MYSQL QUERY RULES TO RUNTIME;SAVE MYSQL QUERY RULES TO DISK;
[root@localhost ~]# sysbench --num-threads=16 --max-requests=0 --max-time=60 --test=/usr/share/doc/sysbench/tests/db/oltp.lua --mysql-
user=application --mysql-password=app --mysql-db=test --mysql-host=127.0.0.1 --mysql-port=6033 --oltp-table-size=10000 --oltp-read-only=on
--oltp-skip-trx=on --oltp-point-selects=100 --oltp-simple-ranges=1 --oltp-sum-ranges=1 --oltp-order-ranges=1 --oltp-distinct-ranges=1 run
| grep 'read/write requests'
read/write requests: 238680 (3956.42 per sec.)
Admin> SELECT count_star,sum_time,hostgroup,digest,digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC;
+------------+-----------+-----------+--------------------+----------------------------------------------------------------------+
| count_star | sum_time | hostgroup | digest | digest_text |
+------------+-----------+-----------+--------------------+----------------------------------------------------------------------+
| 140512 | 632180517 | 1 | 0xBF001A0C13781C1D | SELECT c FROM sbtest1 WHERE id=? |
| 3372 | 18351846 | 1 | 0xF7D3CD60704822A0 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? ORDER BY c |
| 3372 | 17739689 | 1 | 0x877EEAAFADF3DDF2 | SELECT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? ORDER BY c |
| 3372 | 17709660 | 1 | 0xAF7A51977DD56217 | SELECT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? |
| 3372 | 15646777 | 1 | 0x3E268CF3E75DB831 | SELECT SUM(K) FROM sbtest1 WHERE id BETWEEN ? AND ?+? |
| 196688 | 0 | -1 | 0xBF001A0C13781C1D | SELECT c FROM sbtest1 WHERE id=? |
+------------+-----------+-----------+--------------------+----------------------------------------------------------------------+
© 2017 Percona28
ProxySQL - Features - Query Cache
Admin> SHOW VARIABLES LIKE 'mysql-query_cache%';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| mysql-query_cache_size_MB | 256 |
+---------------------------+-------+
1 row in set (0.00 sec)
Admin> SET mysql-query_cache_size_MB=512;
Query OK, 1 row affected (0.00 sec)
Admin> LOAD MYSQL VARIABLES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)
Admin> SAVE MYSQL VARIABLES TO DISK;
Query OK, 72 rows affected (0.03 sec)
© 2017 Percona29
ProxySQL - Features - Query Cache
Admin> SELECT * FROM stats_mysql_global WHERE Variable_Name LIKE 'Query_Cache%';
+--------------------------+----------------+
| Variable_Name | Variable_Value |
+--------------------------+----------------+
| Query_Cache_Memory_bytes | 4469785 | -- Resultset armazenado no QC
| Query_Cache_count_GET | 542750 | -- Número de GET’s executados no QC
| Query_Cache_count_GET_OK | 441122 | -- Número de GET’s que retornaram OK (Query estava em cache e nao estava expirada)
| Query_Cache_count_SET | 101626 | -- Número de Resulset’s inseridos no QC
| Query_Cache_bytes_IN | 19613818 | -- Bytes escritos no QC
| Query_Cache_bytes_OUT | 85136546 | -- Bytes lidos do QC
| Query_Cache_Purged | 100257 | -- Número de queries Purged
| Query_Cache_Entries | 1369 | -- Queries atualmente no QC
+--------------------------+----------------+
© 2017 Percona30
ProxySQL - Features - Integrações
▪MHA (sem alterar DNS ou VIP)
•https://www.percona.com/blog/2016/09/13/proxysql-and-mha-integration/
▪Galera / Percona XtraDB Cluster
•https://www.percona.com/blog/2016/09/15/proxysql-percona-cluster-galera-
integration/
© 2017 Percona31
Estamos Contratando !!!
- Senior Support Engineer - Brazil - Americas
- Inglês fluente
- Manjar de MySQL
percona.com/careers
© 2017 Percona32
Perguntas?
© 2017 Percona33
DATABASE PERFORMANCE
MATTERS
Database Performance MattersDatabase Performance MattersDatabase Performance MattersDatabase Performance MattersDatabase Performance Matters
Obrigado!

More Related Content

PPTX
Percona Xtrabackup Best Practices
PPTX
MySQL Backup Best Practices and Case Study- .ie Continuous Restore Process
PDF
MySQL 5.7 innodb_enhance_partii_20160527
PDF
MySQL Performance Tuning Variables
PDF
Percona Xtrabackup - Highly Efficient Backups
PDF
DATABASE AUTOMATION with Thousands of database, monitoring and backup
PPTX
Streaming replication in PostgreSQL
PDF
MySQL Tuning
Percona Xtrabackup Best Practices
MySQL Backup Best Practices and Case Study- .ie Continuous Restore Process
MySQL 5.7 innodb_enhance_partii_20160527
MySQL Performance Tuning Variables
Percona Xtrabackup - Highly Efficient Backups
DATABASE AUTOMATION with Thousands of database, monitoring and backup
Streaming replication in PostgreSQL
MySQL Tuning

What's hot (20)

PPTX
DB Floripa - ProxySQL para MySQL
PPTX
MySQL Replication Overview -- PHPTek 2016
PDF
Percona University - ProxySQL para MySQL
PDF
MySQL Backup and Security Best Practices
PDF
What is new in PostgreSQL 14?
PDF
MySQL Performance Schema in 20 Minutes
PPT
Download presentation
PDF
MySQL Enterprise Backup (MEB)
PDF
Webinar: MariaDB Provides the Solution to Ease Multi-Source Replication
PPTX
MySQL8.0_performance_schema.pptx
PDF
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
KEY
Perf Tuning Short
PDF
Intro ProxySQL
PDF
Online MySQL Backups with Percona XtraBackup
PPT
PostgreSQL9.3 Switchover/Switchback
PDF
Performance Tuning Best Practices
PDF
PostgreSQL WAL for DBAs
PDF
Percona xtra db cluster(pxc) non blocking operations, what you need to know t...
PDF
InnoDB Performance Optimisation
PDF
Mysql 57-upcoming-changes
DB Floripa - ProxySQL para MySQL
MySQL Replication Overview -- PHPTek 2016
Percona University - ProxySQL para MySQL
MySQL Backup and Security Best Practices
What is new in PostgreSQL 14?
MySQL Performance Schema in 20 Minutes
Download presentation
MySQL Enterprise Backup (MEB)
Webinar: MariaDB Provides the Solution to Ease Multi-Source Replication
MySQL8.0_performance_schema.pptx
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
Perf Tuning Short
Intro ProxySQL
Online MySQL Backups with Percona XtraBackup
PostgreSQL9.3 Switchover/Switchback
Performance Tuning Best Practices
PostgreSQL WAL for DBAs
Percona xtra db cluster(pxc) non blocking operations, what you need to know t...
InnoDB Performance Optimisation
Mysql 57-upcoming-changes
Ad

Similar to ProxySQL para mysql (20)

PPTX
MysQL melhores práticas de seguranca
PDF
Guob - MySQL e LGPD
PDF
Proxy SQL 2.0 with PXC
PDF
Enhancing MySQL Security
PDF
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
PPTX
ProxySQL for MySQL
PDF
PPTX
Proxysql use case scenarios hl++ 2017
PPTX
ProxySQL Use Case Scenarios / Alkin Tezuysal (Percona)
PDF
MySQL NoSQL JSON JS Python "Document Store" demo
PDF
介绍 Percona 服务器 XtraDB 和 Xtrabackup
PDF
Enhancing MySQL Security
PPTX
Mysql ecosystem in 2018
PDF
ProxySQL - High Performance and HA Proxy for MySQL
PPTX
Proxysql use case scenarios plam 2016
PDF
Basic MySQL Troubleshooting for Oracle Database Administrators
PPTX
ProxySQL & PXC(Query routing and Failover Test)
PDF
MySQL Ecosystem in 2020
PDF
ProxySQL Tutorial - PLAM 2016
PPTX
Mysql ecosystem in 2019
MysQL melhores práticas de seguranca
Guob - MySQL e LGPD
Proxy SQL 2.0 with PXC
Enhancing MySQL Security
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
ProxySQL for MySQL
Proxysql use case scenarios hl++ 2017
ProxySQL Use Case Scenarios / Alkin Tezuysal (Percona)
MySQL NoSQL JSON JS Python "Document Store" demo
介绍 Percona 服务器 XtraDB 和 Xtrabackup
Enhancing MySQL Security
Mysql ecosystem in 2018
ProxySQL - High Performance and HA Proxy for MySQL
Proxysql use case scenarios plam 2016
Basic MySQL Troubleshooting for Oracle Database Administrators
ProxySQL & PXC(Query routing and Failover Test)
MySQL Ecosystem in 2020
ProxySQL Tutorial - PLAM 2016
Mysql ecosystem in 2019
Ad

More from Marcelo Altmann (9)

PPTX
Backup Online no MySQL com Percona Xtrabackup
PPTX
Percona XtraBackup - New Features and Improvements
PPTX
Troubleshooting MySQL from a MySQL Developer Perspective
PDF
Backup para MySQL
PPTX
GDB e Análise de Bugs
PPTX
A Percona Support Engineer Walkthrough on pt-stalk
PPTX
Optimizando MySQL
PPTX
MySQL - Melhores práticas de replicação de dados
PPTX
Percona Live London 2014 - MySQL Backup Strategy @ IEDR
Backup Online no MySQL com Percona Xtrabackup
Percona XtraBackup - New Features and Improvements
Troubleshooting MySQL from a MySQL Developer Perspective
Backup para MySQL
GDB e Análise de Bugs
A Percona Support Engineer Walkthrough on pt-stalk
Optimizando MySQL
MySQL - Melhores práticas de replicação de dados
Percona Live London 2014 - MySQL Backup Strategy @ IEDR

Recently uploaded (20)

PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PDF
Paper PDF World Game (s) Great Redesign.pdf
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PDF
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
PDF
Triggering QUIC, presented by Geoff Huston at IETF 123
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPTX
Introduction to Information and Communication Technology
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
PPTX
Digital Literacy And Online Safety on internet
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
presentation_pfe-universite-molay-seltan.pptx
Slides PPTX World Game (s) Eco Economic Epochs.pptx
Paper PDF World Game (s) Great Redesign.pdf
introduction about ICD -10 & ICD-11 ppt.pptx
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Tenda Login Guide: Access Your Router in 5 Easy Steps
Introuction about WHO-FIC in ICD-10.pptx
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
Triggering QUIC, presented by Geoff Huston at IETF 123
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
The New Creative Director: How AI Tools for Social Media Content Creation Are...
Decoding a Decade: 10 Years of Applied CTI Discipline
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Introduction to Information and Communication Technology
SAP Ariba Sourcing PPT for learning material
RPKI Status Update, presented by Makito Lay at IDNOG 10
Digital Literacy And Online Safety on internet
Cloud-Scale Log Monitoring _ Datadog.pdf
522797556-Unit-2-Temperature-measurement-1-1.pptx

ProxySQL para mysql

  • 1. © 2017 Percona1 Marcelo Altmann ProxySQL para MySQL Mais que um simples proxy - Exemplos de uso Senior Support Engineer MySQL & MongoDB meetup - Sao Paulo 11/05/2017
  • 3. © 2017 Percona3 Agenda ▪ Sobre Mim ▪ ProxySQL •Introdução •Features ▪Load Balance ▪Firewall ▪Query Cache ▪Integrações ▪Outros
  • 5. © 2017 Percona5 Marcelo Altmann ▪Engenheiro de Suporte @ percona • MySQL DBA @ IEDR (CCTLD Irlanda) ▪Oracle ACE Associate ▪Certificaçoes •Oracle Certified Professional, MySQL 5.6 Database Administrator •Oracle Certified Professional, MySQL 5.6 Developer •Oracle Certified Professional, MySQL 5 Database Administrator •Oracle Certified Professional, MySQL 5 Developer •Oracle Certified Associate, MySQL 5.0/5.1/5.5 ▪blog.marceloaltmann.com
  • 6. © 2017 Percona6 ProxySQL - Introdução
  • 7. © 2017 Percona7 ProxySQL - Introdução ▪ Criado e mantido por René Cannaò •MySQL Community - Contribuidor do ano (2017) ▪Open Source •http://www.proxysql.com/ •https://github.com/sysown/proxysql
  • 8. © 2017 Percona8 ProxySQL - Introdução ▪ Instalação •Source Code / rpm / deb / percona repo (yum / apt-get) ▪MySQL admin interface [root@localhost ~]# mysql -u admin -padmin -P 6032 -h 127.0.0.1 --prompt='Admin> ' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 1 Server version: 5.5.30 (ProxySQL Admin Module) . . . Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. Admin>
  • 9. © 2017 Percona9 ProxySQL - Introdução ▪ Camadas •Runtime - Dados que o proxySQL acessa •Main/Memory - Camada onde executamos alterações e monitoramento •Disk - Camada para persistir dados. SQLite3 •Config - Arquivo em disco que é lido caso o banco de dados SQLite3 nao exista.
  • 10. © 2017 Percona10 ProxySQL - Introdução ▪ Tabelas •mysql_servers - Contém a lista de servidores. •mysql_query_rules - Contém a lista de regras para cache, reescrita e redirecionamento de queries. •mysql_users - Contém a lista de usuário para autenticação. •global_variables - Contém a lista de variáveis de configuração.
  • 12. © 2017 Percona12 ProxySQL - Features - Load Balance ▪ Usuários de autenticação Admin> INSERT INTO mysql_users (username,password) VALUES ('application', 'app'); Query OK, 1 row affected (0.00 sec) Admin> LOAD MYSQL USERS TO RUNTIME ; Query OK, 0 rows affected (0.00 sec) Admin> SAVE MYSQL USERS FROM RUNTIME; -- Hack para o plain-text password Query OK, 0 rows affected (0.00 sec) Admin> SAVE MYSQL USERS TO DISK ; Query OK, 0 rows affected (0.01 sec)
  • 13. © 2017 Percona13 ProxySQL - Features - Load Balance ▪ Redirecionamento do tráfego para lista de servidores Admin> INSERT INTO mysql_servers (hostname) VALUES ('192.168.19.71'),('192.168.19.72'),('192.168.19.73'); Query OK, 3 rows affected (0.00 sec) Admin> LOAD MYSQL SERVERS TO RUNTIME; Query OK, 0 rows affected (0.01 sec) Admin> SAVE MYSQL SERVERS TO DISK; Query OK, 0 rows affected (0.02 sec)
  • 14. © 2017 Percona14 ProxySQL - Features - Load Balance ▪ Redirecionamento do tráfego para lista de servidores [root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT @@server_id" +-------------+ | @@server_id | +-------------+ | 2 | +-------------+ [root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT @@server_id" +-------------+ | @@server_id | +-------------+ | 3 | +-------------+ [root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT @@server_id" +-------------+ | @@server_id | +-------------+ | 1 | +-------------+
  • 15. © 2017 Percona15 ProxySQL - Features - Load Balance ▪ Dividir leituras e escritas •Host Groups - Agrupamento de servidores Admin> UPDATE mysql_servers SET hostgroup_id = 1 WHERE hostname <> '192.168.19.71'; Admin> SELECT hostgroup_id, hostname FROM mysql_servers; +--------------+---------------+ | hostgroup_id | hostname | +--------------+---------------+ | 0 | 192.168.19.71 | | 1 | 192.168.19.72 | | 1 | 192.168.19.73 | +--------------+---------------+ Admin> LOAD MYSQL SERVERS TO RUNTIME; Admin> SAVE MYSQL SERVERS TO DISK;
  • 16. © 2017 Percona16 ProxySQL - Features - Load Balance ▪ Dividir leituras e escritas Admin> SELECT username, default_hostgroup FROM mysql_users; +-------------+-------------------+ | username | default_hostgroup | +-------------+-------------------+ | application | 0 | +-------------+-------------------+ 1 row in set (0.00 sec)
  • 17. © 2017 Percona17 ProxySQL - Features - Load Balance ▪ Dividir leituras e escritas Admin> INSERT INTO mysql_query_rules (rule_id,active,match_digest,destination_hostgroup) -> VALUES -> (1,1,'^SELECT.*FOR UPDATE$',0), -> (2,1,'^SELECT',1); Query OK, 2 rows affected (0.00 sec) Admin> LOAD MYSQL QUERY RULES TO RUNTIME; Query OK, 0 rows affected (0.00 sec) Admin> SAVE MYSQL QUERY RULES TO DISK; Query OK, 0 rows affected (0.01 sec)
  • 18. © 2017 Percona18 ProxySQL - Features - Load Balance ▪ Dividir leituras e escritas [root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "INSERT INTO test.t1 VALUES (@@server_id)" [root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT @@server_id, server FROM test.t1" +-------------+--------+ | @@server_id | server | +-------------+--------+ | 3 | 1 | +-------------+--------+ [root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT @@server_id, server FROM test.t1" +-------------+--------+ | @@server_id | server | +-------------+--------+ | 2 | 1 | +-------------+--------+
  • 19. © 2017 Percona19 ProxySQL - Features - Stats ▪ SHOW TABLES FROM stats; •Stats_mysql_commands_counters - Contador baseado em comandos •Stats_mysql_connection_pool - Conexoes por servidor •Stats_mysql_global - Estatisticas globais •Stats_mysql_processlist - SHOW PROCESSLIST •Stats_mysql_query_digest - Contador agrupado por digest •Stats_mysql_query_rules - Contador baseado nas query rules
  • 20. © 2017 Percona20 ProxySQL - Features - Reescrita de query ▪ stats_mysql_query_digest Admin> SELECT digest, digest_text, sum_time FROM stats_mysql_query_digest ORDER BY sum_time DESC LIMIT 1; +--------------------+-----------------------------------------+----------+ | digest | digest_text | sum_time | +--------------------+-----------------------------------------+----------+ | 0xD69E622A5052289E | SELECT * FROM world.city WHERE Name = ? | 7016461 | +--------------------+-----------------------------------------+----------+ Admin> INSERT INTO mysql_query_rules (rule_id,active,match_pattern, replace_pattern) VALUES (3,1, '^SELECT * FROM world.city WHERE Name = (.*)$', 'SELECT Population FROM world.city WHERE Name = 1'); Query OK, 1 row affected (0.00 sec) Admin> LOAD MYSQL QUERY RULES TO RUNTIME; Query OK, 0 rows affected (0.01 sec) Admin> SAVE MYSQL QUERY RULES TO DISK; Query OK, 0 rows affected (0.01 sec)
  • 21. © 2017 Percona21 ProxySQL - Features - Reescrita de query [root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT * FROM world.city WHERE Name = 'São Paulo'" +------------+ | Population | +------------+ | 9968485 | +------------+
  • 22. © 2017 Percona22 ProxySQL - Features - Firewall ▪Bloquear queries - SQL INJECTION! •Original query: SELECT Name FROM world.city WHERE Name = ‘?’ •SQL Injection: ? = São Paulo' OR ID > 0; -- •SELECT Name FROM world.city WHERE Name = 'São Paulo' OR ID > 0; --' Admin> SELECT username, digest, digest_text FROM stats_mysql_query_digest WHERE digest_text LIKE '% OR ID %' ORDER BY first_seen DESC LIMIT 1; +-------------+--------------------+------------------------------------------------------+ | username | digest | digest_text | +-------------+--------------------+------------------------------------------------------+ | application | 0xD8AF41BF32707ABD | SELECT Name FROM world.city WHERE Name = ? OR ID > ? | +-------------+--------------------+------------------------------------------------------+ 1 row in set (0.00 sec)
  • 23. © 2017 Percona23 ProxySQL - Features - Firewall Admin> INSERT INTO mysql_query_rules (rule_id, active, digest, error_msg, apply) VALUES (4,1,'0xD8AF41BF32707ABD','Suspeita de SQL Injection',1); Query OK, 1 row affected (0.00 sec) Admin> LOAD MYSQL QUERY RULES TO RUNTIME;SAVE MYSQL QUERY RULES TO DISK; Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.01 sec) [root@localhost ~]# mysql -u application -papp -P 6033 -h 127.0.0.1 -e "SELECT Name FROM world.city WHERE Name = 'São Paulo' OR ID > 0; --' " ERROR 1148 (42000) at line 1: Suspeita de SQL Injection
  • 24. © 2017 Percona24 ProxySQL - Features - Mirror ▪Espelhar queries em outros servidores •Testar configurações •Standby Master - Manter buffer pool quente •Testar workload em versões diferentes - Upgrades •Troubleshooting
  • 25. © 2017 Percona25 ProxySQL - Features - Mirror ▪Esquentar buffer pool Admin> INSERT INTO mysql_servers (hostgroup_id, hostname) VALUES (2, '192.168.29.29'); Admin> LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK; Admin> SELECT rule_id, match_digest, destination_hostgroup, mirror_hostgroup FROM mysql_query_rules WHERE rule_id=2; +---------+--------------+-----------------------+------------------+ | rule_id | match_digest | destination_hostgroup | mirror_hostgroup | +---------+--------------+-----------------------+------------------+ | 2 | ^SELECT | 1 | NULL | +---------+--------------+-----------------------+------------------+ Admin> UPDATE mysql_query_rules SET mirror_hostgroup=2 WHERE rule_id=2; Admin> LOAD MYSQL QUERY RULES TO RUNTIME; Admin> SAVE MYSQL QUERY RULES TO DISK;
  • 26. © 2017 Percona26 ProxySQL - Features - Query Cache ▪Cache de queries baseado em TTL [root@localhost ~]# sysbench --num-threads=16 --max-requests=0 --max-time=60 --test=/usr/share/doc/sysbench/tests/db/oltp.lua --mysql- user=application --mysql-password=app --mysql-db=test --mysql-host=127.0.0.1 --mysql-port=6033 --oltp-table-size=10000 --oltp-read-only=on --oltp-skip-trx=on --oltp-point-selects=100 --oltp-simple-ranges=1 --oltp-sum-ranges=1 --oltp-order-ranges=1 --oltp-distinct-ranges=1 run | grep 'read/write requests' read/write requests: 105664 (1744.81 per sec.) Admin> SELECT count_star,sum_time,hostgroup,digest,digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC; +------------+-----------+-----------+--------------------+----------------------------------------------------------------------+ | count_star | sum_time | hostgroup | digest | digest_text | +------------+-----------+-----------+--------------------+----------------------------------------------------------------------+ | 100200 | 459147213 | 1 | 0xBF001A0C13781C1D | SELECT c FROM sbtest1 WHERE id=? | | 1002 | 6533622 | 1 | 0xF7D3CD60704822A0 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? ORDER BY c | | 1002 | 6061540 | 1 | 0x877EEAAFADF3DDF2 | SELECT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? ORDER BY c | | 1002 | 5905677 | 1 | 0xAF7A51977DD56217 | SELECT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? | | 1002 | 5321376 | 1 | 0x3E268CF3E75DB831 | SELECT SUM(K) FROM sbtest1 WHERE id BETWEEN ? AND ?+? | +------------+-----------+-----------+--------------------+----------------------------------------------------------------------+
  • 27. © 2017 Percona27 ProxySQL - Features - Query Cache Admin> INSERT INTO mysql_query_rules (rule_id,active,digest,cache_ttl,apply) VALUES (5,1, '0xBF001A0C13781C1D' ,2000,1); Query OK, 1 row affected (0.00 sec) Admin> LOAD MYSQL QUERY RULES TO RUNTIME;SAVE MYSQL QUERY RULES TO DISK; [root@localhost ~]# sysbench --num-threads=16 --max-requests=0 --max-time=60 --test=/usr/share/doc/sysbench/tests/db/oltp.lua --mysql- user=application --mysql-password=app --mysql-db=test --mysql-host=127.0.0.1 --mysql-port=6033 --oltp-table-size=10000 --oltp-read-only=on --oltp-skip-trx=on --oltp-point-selects=100 --oltp-simple-ranges=1 --oltp-sum-ranges=1 --oltp-order-ranges=1 --oltp-distinct-ranges=1 run | grep 'read/write requests' read/write requests: 238680 (3956.42 per sec.) Admin> SELECT count_star,sum_time,hostgroup,digest,digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC; +------------+-----------+-----------+--------------------+----------------------------------------------------------------------+ | count_star | sum_time | hostgroup | digest | digest_text | +------------+-----------+-----------+--------------------+----------------------------------------------------------------------+ | 140512 | 632180517 | 1 | 0xBF001A0C13781C1D | SELECT c FROM sbtest1 WHERE id=? | | 3372 | 18351846 | 1 | 0xF7D3CD60704822A0 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? ORDER BY c | | 3372 | 17739689 | 1 | 0x877EEAAFADF3DDF2 | SELECT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? ORDER BY c | | 3372 | 17709660 | 1 | 0xAF7A51977DD56217 | SELECT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? | | 3372 | 15646777 | 1 | 0x3E268CF3E75DB831 | SELECT SUM(K) FROM sbtest1 WHERE id BETWEEN ? AND ?+? | | 196688 | 0 | -1 | 0xBF001A0C13781C1D | SELECT c FROM sbtest1 WHERE id=? | +------------+-----------+-----------+--------------------+----------------------------------------------------------------------+
  • 28. © 2017 Percona28 ProxySQL - Features - Query Cache Admin> SHOW VARIABLES LIKE 'mysql-query_cache%'; +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | mysql-query_cache_size_MB | 256 | +---------------------------+-------+ 1 row in set (0.00 sec) Admin> SET mysql-query_cache_size_MB=512; Query OK, 1 row affected (0.00 sec) Admin> LOAD MYSQL VARIABLES TO RUNTIME; Query OK, 0 rows affected (0.00 sec) Admin> SAVE MYSQL VARIABLES TO DISK; Query OK, 72 rows affected (0.03 sec)
  • 29. © 2017 Percona29 ProxySQL - Features - Query Cache Admin> SELECT * FROM stats_mysql_global WHERE Variable_Name LIKE 'Query_Cache%'; +--------------------------+----------------+ | Variable_Name | Variable_Value | +--------------------------+----------------+ | Query_Cache_Memory_bytes | 4469785 | -- Resultset armazenado no QC | Query_Cache_count_GET | 542750 | -- Número de GET’s executados no QC | Query_Cache_count_GET_OK | 441122 | -- Número de GET’s que retornaram OK (Query estava em cache e nao estava expirada) | Query_Cache_count_SET | 101626 | -- Número de Resulset’s inseridos no QC | Query_Cache_bytes_IN | 19613818 | -- Bytes escritos no QC | Query_Cache_bytes_OUT | 85136546 | -- Bytes lidos do QC | Query_Cache_Purged | 100257 | -- Número de queries Purged | Query_Cache_Entries | 1369 | -- Queries atualmente no QC +--------------------------+----------------+
  • 30. © 2017 Percona30 ProxySQL - Features - Integrações ▪MHA (sem alterar DNS ou VIP) •https://www.percona.com/blog/2016/09/13/proxysql-and-mha-integration/ ▪Galera / Percona XtraDB Cluster •https://www.percona.com/blog/2016/09/15/proxysql-percona-cluster-galera- integration/
  • 31. © 2017 Percona31 Estamos Contratando !!! - Senior Support Engineer - Brazil - Americas - Inglês fluente - Manjar de MySQL percona.com/careers
  • 33. © 2017 Percona33 DATABASE PERFORMANCE MATTERS Database Performance MattersDatabase Performance MattersDatabase Performance MattersDatabase Performance MattersDatabase Performance Matters Obrigado!