SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Query logging with
ProxySQL
Summary
2
This document explains how to use proxysql to leave query logs that users execute
directly by connecting to db.
This document includes the following things:
- How to install proxysql
- How to set up proxysql for query logging
- How to convert binary format query log to text format in proxysql
- How to install ELK stack and set up
Architecture
3
BIN TXT
Users
Admin
……
ProxySQL
DB Servers
FileBeat
Logstash
Elasticsearch Kibana
Set up ProxySQL 4
# rpm -ivh proxysql-2.0.4-1-centos67.x86_64.rpm
# service proxysql start
# mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> '
Install & Start & Connect proxysql
https://github.com/sysown/proxysql/releases/tag/v2.0.4
5
Set up ProxySQL
-- Adding User
insert into mysql_users(username, password, transaction_persistent)
values('test_user','test1234', 0);
load mysql users to runtime;
save mysql users to disk;
-- Adding Server Information
insert into mysql_servers(hostname,hostgroup_id,port,comment)
values('10.xx.xx.01',0,3306,'customerdb');
insert into mysql_servers(hostname,hostgroup_id,port,comment)
values('10.xx.xx.02',1,3306,'productdb');
insert into mysql_servers(hostname,hostgroup_id,port,comment)
values('10.xx.xx.03',2,3306,'orderdb');
load mysql servers to runtime;
save mysql servers to disk;
Set up configuration
6
Set up ProxySQL
-- Set enable query logging
set mysql-eventslog_filename='queries.log';
set mysql-eventslog_filesize=1048576;
load mysql variables to runtime;
save mysql variables to disk;
insert into mysql_query_rules (rule_id, active, match_digest, log,apply)
values (1,1,'.',1,0);
insert into mysql_query_rules
(rule_id, active, flagIN, match_pattern, re_modifiers, replace_pattern, apply)
values (2,1,0,'n','GLOBAL',' ',0);
load mysql query rules to runtime;
save mysql query rules to disk;
Set up configuration
7
Set up ProxySQL
-- Add query rules for query routing
insert into mysql_query_rules(active,match_pattern, destination_hostgroup,apply)
values(1,'^SELECT .* customerdb',0,1);
insert into mysql_query_rules(active,match_pattern, destination_hostgroup,apply)
values(1,'^SELECT .* productdb',1,1);
insert into mysql_query_rules(active,match_pattern, destination_hostgroup,apply)
values(1,'^SELECT .* orderdb',2,1);
load mysql query rules to runtime;
save mysql query rules to disk;
Set up configuration
8
Set up ProxySQL
-- You can check to connect db
# mysql -utest_user -p'test1234' -h10.xx.xx.001 -P6033 -e"SELECT * from customerdb.member limit 10"
# mysql -utest_user -p'test1234' -h10.xx.xx.001 -P6033 -e"SELECT * from orderdb.order limit 10"
-- You can find a query log file(queries.log.0000000x)
# cd /var/lib/proxysql
# ls -al
total 7876
drwxr-xr-x 4 proxysql proxysql 4096 Jun 20 14:47 .
drwxr-xr-x. 27 root root 4096 Jun 19 18:26 ..
-rw------- 1 proxysql proxysql 2177193 Jun 20 14:47 proxysql.log
-rw-r--r-- 1 proxysql proxysql 6 Jun 20 11:18 proxysql.pid
-rw------- 1 proxysql proxysql 2781184 Jun 20 14:47 proxysql_stats.db
-rw------- 1 proxysql proxysql 89474 Jun 20 14:19 queries.log.00000001
Check query logging
9
Convert query log
ProxySQL leaves a query log in binary format.
We need to convert it to text format.
ProxySQL supports tool that convert binary format file to text
format.(eventslog_reader_sample)
# git clone https://github.com/sysown/proxysql.git
# cd proxysql/tools
# ll
total 128
-rwxr-xr-x 1 root root 2896 Jun 11 10:28 check_variables.pl
-rw-r--r-- 1 root root 4527 Jun 11 10:28 eventslog_reader_sample.cpp
-rw-r--r-- 1 root root 122 Jun 11 10:28 Makefile
-rwxr-xr-x 1 root root 16869 Jun 11 10:28 proxysql_galera_checker.sh
-rwxr-xr-x 1 root root 3574 Jun 11 10:28 proxysql_galera_writer.pl
10
Convert query log
You can compile eventslog_reader_sample.cpp file.
And you can convert binary query log file to text format.
# make
# ll
total 128
-rwxr-xr-x 1 root root 2896 Jun 11 10:28 check_variables.pl
-rwxr-xr-x 1 root root 87168 Jun 11 10:41 eventslog_reader_sample
-rw-r--r-- 1 root root 4527 Jun 11 10:28 eventslog_reader_sample.cpp
-rw-r--r-- 1 root root 122 Jun 11 10:28 Makefile
-rwxr-xr-x 1 root root 16869 Jun 11 10:28 proxysql_galera_checker.sh
-rwxr-xr-x 1 root root 3574 Jun 11 10:28 proxysql_galera_writer.pl
# ./eventslog_reader_sample /var/lib/proxysql/queries.log.00000001
or
# ./eventslog_reader_sample /var/lib/proxysql/queries.log.00000001 > result.txt
11
Convert query log
You can see two types of query logs, binary and text.
# tail -n 10 queries.log.00000001
��:�L14SHOW SESSION VARIABLES LIKE 'lower_case_table_names'w�� test_userinformation_schema10.xxx.xxx.x:1475310.xx.xx.xx:3306�KSw/���RSELECT current_user()x��
test_userinformation_schema10.xxx.xxx.xx:1475310.xx.xx.xx:3306��cw�X��Efw�X��
��l��SET CHARACTER SET utf8g�� test_userinformation_schema10.xxx.xxx.xxx:14753�����������pw�X���pw�X����8���Q0.xxx.x:14753x��
test_userinformation_schema10.xxx.xxx.x:1475310.xx.xx.xx:3306�K~w�X���w�X���$�f��SET SQL_SAFE_UPDATES=1o��
test_userinformation_schema10.xxx.xxx.x:14753����������‫ۏ‬w�X��‫ۏ‬w�X��yS��g�H�0.xxx.x:14753ION_ID()���
test_userinformation_schema10.xxx.xxx.x:1475310.xx.xx.xx:3306���w�X��h�w�X��Y�Z�~a��%SHOWorderdb10.xxx.xxx.x:14753����������ӹw�X��ӹw�X��0�o�/0.xxx.x:14753=1
# tail -n 10 queries_result.log.00000001
ProxySQL LOG QUERY: thread_id="30" username="test_user" schemaname="information_schema" client="10.xxx.xxx.x:63285" HID=0 server="10.xx.xx.xxx:3306" starttime="2019-06-20 17:35:20.108808" endtime="2019-06-20
17:35:20.109038" duration=230us digest="0xD8CCF296B01AC933"
select *
from orderdb.order
limit 105
ProxySQL LOG QUERY: thread_id="30" username="test_user" schemaname="information_schema" client="10.xxx.xxx.x:63285" HID=0 server="10.xx.xx.xxx:3306" starttime="2019-06-20 17:35:21.300584" endtime="2019-06-20
17:35:21.300738" duration=154us digest="0xD8CCF296B01AC933"
select *
from customerdb.custom
limit 100
12
Make cron job for converting query log file
Query log file will be created sequentially, we need a script for converting binary query
log file to text format file whenever new query log file is created.
BIN TXTqueries.log.00000001 ./result/queries_result.log.00000001
./eventslog_reader_sample queries.log.00000001 > ./result/queries_result.log.00000001
BIN TXTqueries.log.00000002 ./result/queries_result.log.00000002
BIN TXTqueries.log.00000003 ./result/queries_result.log.00000003
13
How user uses with workbench tool
The user who uses to connect directly db can use like this with workbench.
User has to use table name with database name(schema name) in query.
(ex. orderdb.order)
14
Set up FileBeat
Download
# tar -xzvf filebeat-7.1.1-linux-x86_64.tar.gz -C /opt/
# vi filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/lib/proxysql/result/queries_result.log.*
multiline.pattern: '^ProxySQL'
multiline.negate: true
multiline.match: after
output.logstash:
hosts: ["localhost:5044"]
https://www.elastic.co/kr/downloads/beats/filebeat
15
Set up Logstash
Download
# tar -xzvf logstash-7.1.1.tar.gz -C /opt/
# cd config
# vi logstash.conf
input {
beats {
port => 5044
}
}
---continue behind
https://www.elastic.co/kr/downloads/logstash
16
Set up Logstash
# vi logstash.conf
filter {
mutate {
gsub => ["message",""",""]
}
mutate {
gsub => ["message",". ",".0"]
}
grok {
match => {"message" => "ProxySQL LOG QUERY: thread_id=%{GREEDYDATA:thread_id} username=%{GREEDYDATA:username}
schemaname=%{GREEDYDATA:schemaname} client=%{GREEDYDATA:client} HID=%{GREEDYDATA:HID} server=%{GREEDYDATA:server}
starttime=%{TIMESTAMP_ISO8601:starttime} endtime=%{TIMESTAMP_ISO8601:endtime} duration=%{GREEDYDATA:duration}
digest=%{DATA:digest}n%{GREEDYDATA:query}"}
}
}
---continue behind
17
Set up Logstash
# vi logstash.conf
output {
elasticsearch {
hosts => ["http://[elasticsearch_server_ip]:9200"]
index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
}
}
18
Set up Elasticsearch
Download
# tar -xzvf elasticsearch-7.1.1-linux-x86_64.tar.gz -C /opt/
# cd config
# vi elasticsearch.yml
cluster.name: db-admin
node.name: first-node
path.data: /elasticsearch/data
path.logs: /elasticsearch/log
bootstrap.system_call_filter: false
network.host: [elasticsearch_server_ip]
http.port: 9200
cluster.initial_master_nodes: "first-node"
https://www.elastic.co/kr/downloads/elasticsearch
19
Set up Kibana
Download
# tar -xzvf kibana-7.1.1-linux-x86_64.tar.gz -C /opt/
# cd config
# vi kibana.yml
server.host: "[kibana_server_ip]"
elasticsearch.hosts: ["http://[elasticsearch_server_ip]:9200"]
https://www.elastic.co/kr/downloads/kibana
20
-- Start Filebeat
# cd /opt/filebeat-7.1.1-linux-x86_64
# ./filebeat -e -c filebeat.yml
-- Start Logstash
# cd /opt/logstash-7.1.1
# bin/logstash -f config/logstash.conf
-- Start Elasticsearch
# cd /opt/elasticsearch-7.1.1
# bin/elasticsearch
-- Start Kibana
# cd /opt/kibana-7.1.1-linux-x86_64
# bin/kibana
Start ELK stack
Kibana Web 21
Confirm the query logs in kibana web page
http://kibana_server_ip:5601
Reference 22
https://github.com/sysown/proxysql/wiki/Query-Logging
https://github.com/sysown/proxysql/issues/871
Thank You

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

MySQL Database Architectures - MySQL InnoDB ClusterSet 2021-11
MySQL Database Architectures - MySQL InnoDB ClusterSet 2021-11MySQL Database Architectures - MySQL InnoDB ClusterSet 2021-11
MySQL Database Architectures - MySQL InnoDB ClusterSet 2021-11
 
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdfProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
 
MySQL/MariaDB Proxy Software Test
MySQL/MariaDB Proxy Software TestMySQL/MariaDB Proxy Software Test
MySQL/MariaDB Proxy Software Test
 
Intro ProxySQL
Intro ProxySQLIntro ProxySQL
Intro ProxySQL
 
ProxySQL High Availability (Clustering)
ProxySQL High Availability (Clustering)ProxySQL High Availability (Clustering)
ProxySQL High Availability (Clustering)
 
ProxySQL High Avalability and Configuration Management Overview
ProxySQL High Avalability and Configuration Management OverviewProxySQL High Avalability and Configuration Management Overview
ProxySQL High Avalability and Configuration Management Overview
 
MySQL8.0_performance_schema.pptx
MySQL8.0_performance_schema.pptxMySQL8.0_performance_schema.pptx
MySQL8.0_performance_schema.pptx
 
HandsOn ProxySQL Tutorial - PLSC18
HandsOn ProxySQL Tutorial - PLSC18HandsOn ProxySQL Tutorial - PLSC18
HandsOn ProxySQL Tutorial - PLSC18
 
Proxysql sharding
Proxysql shardingProxysql sharding
Proxysql sharding
 
Automated master failover
Automated master failoverAutomated master failover
Automated master failover
 
ProxySQL Cluster - Percona Live 2022
ProxySQL Cluster - Percona Live 2022ProxySQL Cluster - Percona Live 2022
ProxySQL Cluster - Percona Live 2022
 
MySQL GTID 시작하기
MySQL GTID 시작하기MySQL GTID 시작하기
MySQL GTID 시작하기
 
MySQL Timeout Variables Explained
MySQL Timeout Variables Explained MySQL Timeout Variables Explained
MySQL Timeout Variables Explained
 
MaxScale이해와활용-2023.11
MaxScale이해와활용-2023.11MaxScale이해와활용-2023.11
MaxScale이해와활용-2023.11
 
Percona Xtrabackup - Highly Efficient Backups
Percona Xtrabackup - Highly Efficient BackupsPercona Xtrabackup - Highly Efficient Backups
Percona Xtrabackup - Highly Efficient Backups
 
MySQL_MariaDB-성능개선-202201.pptx
MySQL_MariaDB-성능개선-202201.pptxMySQL_MariaDB-성능개선-202201.pptx
MySQL_MariaDB-성능개선-202201.pptx
 
Performance Schema for MySQL troubleshooting
Performance Schema for MySQL troubleshootingPerformance Schema for MySQL troubleshooting
Performance Schema for MySQL troubleshooting
 
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group ReplicationPercona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
 
PGConf.ASIA 2017 Logical Replication Internals (English)
PGConf.ASIA 2017 Logical Replication Internals (English)PGConf.ASIA 2017 Logical Replication Internals (English)
PGConf.ASIA 2017 Logical Replication Internals (English)
 
Maxscale 소개 1.1.1
Maxscale 소개 1.1.1Maxscale 소개 1.1.1
Maxscale 소개 1.1.1
 

Ähnlich wie Query logging with proxysql

X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
Yiwei Ma
 
基于Mongodb的压力评测工具 ycsb的一些概括
基于Mongodb的压力评测工具 ycsb的一些概括基于Mongodb的压力评测工具 ycsb的一些概括
基于Mongodb的压力评测工具 ycsb的一些概括
Louis liu
 

Ähnlich wie Query logging with proxysql (20)

MySQL Audit using Percona audit plugin and ELK
MySQL Audit using Percona audit plugin and ELKMySQL Audit using Percona audit plugin and ELK
MySQL Audit using Percona audit plugin and ELK
 
ProxySQL & PXC(Query routing and Failover Test)
ProxySQL & PXC(Query routing and Failover Test)ProxySQL & PXC(Query routing and Failover Test)
ProxySQL & PXC(Query routing and Failover Test)
 
High Availability Content Caching with NGINX
High Availability Content Caching with NGINXHigh Availability Content Caching with NGINX
High Availability Content Caching with NGINX
 
High Availability Content Caching with NGINX
High Availability Content Caching with NGINXHigh Availability Content Caching with NGINX
High Availability Content Caching with NGINX
 
Automatically scaling your Kubernetes workloads - SVC201-S - Chicago AWS Summit
Automatically scaling your Kubernetes workloads - SVC201-S - Chicago AWS SummitAutomatically scaling your Kubernetes workloads - SVC201-S - Chicago AWS Summit
Automatically scaling your Kubernetes workloads - SVC201-S - Chicago AWS Summit
 
Autoscaling Your Kubernetes Workloads (Sponsored by Datadog) - AWS Summit Sydney
Autoscaling Your Kubernetes Workloads (Sponsored by Datadog) - AWS Summit SydneyAutoscaling Your Kubernetes Workloads (Sponsored by Datadog) - AWS Summit Sydney
Autoscaling Your Kubernetes Workloads (Sponsored by Datadog) - AWS Summit Sydney
 
Deploying Percona XtraDB Cluster in Openshift
Deploying Percona XtraDB Cluster in OpenshiftDeploying Percona XtraDB Cluster in Openshift
Deploying Percona XtraDB Cluster in Openshift
 
Automatically scaling your Kubernetes workloads - SVC210-S - Santa Clara AWS ...
Automatically scaling your Kubernetes workloads - SVC210-S - Santa Clara AWS ...Automatically scaling your Kubernetes workloads - SVC210-S - Santa Clara AWS ...
Automatically scaling your Kubernetes workloads - SVC210-S - Santa Clara AWS ...
 
Solving anything in VCL
Solving anything in VCLSolving anything in VCL
Solving anything in VCL
 
Install elasticsearch, logstash and kibana
Install elasticsearch, logstash and kibana Install elasticsearch, logstash and kibana
Install elasticsearch, logstash and kibana
 
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScriptJavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
 
Automatically Scaling Your Kubernetes Workloads - SVC209-S - Anaheim AWS Summit
Automatically Scaling Your Kubernetes Workloads - SVC209-S - Anaheim AWS SummitAutomatically Scaling Your Kubernetes Workloads - SVC209-S - Anaheim AWS Summit
Automatically Scaling Your Kubernetes Workloads - SVC209-S - Anaheim AWS Summit
 
Incrementalism: An Industrial Strategy For Adopting Modern Automation
Incrementalism: An Industrial Strategy For Adopting Modern AutomationIncrementalism: An Industrial Strategy For Adopting Modern Automation
Incrementalism: An Industrial Strategy For Adopting Modern Automation
 
LogStash in action
LogStash in actionLogStash in action
LogStash in action
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
 
Elasticsearch on Kubernetes
Elasticsearch on KubernetesElasticsearch on Kubernetes
Elasticsearch on Kubernetes
 
Why Managed Service Providers Should Embrace Container Technology
Why Managed Service Providers Should Embrace Container TechnologyWhy Managed Service Providers Should Embrace Container Technology
Why Managed Service Providers Should Embrace Container Technology
 
GE Predix 新手入门 赵锴 物联网_IoT
GE Predix 新手入门 赵锴 物联网_IoTGE Predix 新手入门 赵锴 物联网_IoT
GE Predix 新手入门 赵锴 物联网_IoT
 
MySQL Utilities -- PyTexas 2015
MySQL Utilities -- PyTexas 2015MySQL Utilities -- PyTexas 2015
MySQL Utilities -- PyTexas 2015
 
基于Mongodb的压力评测工具 ycsb的一些概括
基于Mongodb的压力评测工具 ycsb的一些概括基于Mongodb的压力评测工具 ycsb的一些概括
基于Mongodb的压力评测工具 ycsb的一些概括
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 

Query logging with proxysql

  • 2. Summary 2 This document explains how to use proxysql to leave query logs that users execute directly by connecting to db. This document includes the following things: - How to install proxysql - How to set up proxysql for query logging - How to convert binary format query log to text format in proxysql - How to install ELK stack and set up
  • 4. Set up ProxySQL 4 # rpm -ivh proxysql-2.0.4-1-centos67.x86_64.rpm # service proxysql start # mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> ' Install & Start & Connect proxysql https://github.com/sysown/proxysql/releases/tag/v2.0.4
  • 5. 5 Set up ProxySQL -- Adding User insert into mysql_users(username, password, transaction_persistent) values('test_user','test1234', 0); load mysql users to runtime; save mysql users to disk; -- Adding Server Information insert into mysql_servers(hostname,hostgroup_id,port,comment) values('10.xx.xx.01',0,3306,'customerdb'); insert into mysql_servers(hostname,hostgroup_id,port,comment) values('10.xx.xx.02',1,3306,'productdb'); insert into mysql_servers(hostname,hostgroup_id,port,comment) values('10.xx.xx.03',2,3306,'orderdb'); load mysql servers to runtime; save mysql servers to disk; Set up configuration
  • 6. 6 Set up ProxySQL -- Set enable query logging set mysql-eventslog_filename='queries.log'; set mysql-eventslog_filesize=1048576; load mysql variables to runtime; save mysql variables to disk; insert into mysql_query_rules (rule_id, active, match_digest, log,apply) values (1,1,'.',1,0); insert into mysql_query_rules (rule_id, active, flagIN, match_pattern, re_modifiers, replace_pattern, apply) values (2,1,0,'n','GLOBAL',' ',0); load mysql query rules to runtime; save mysql query rules to disk; Set up configuration
  • 7. 7 Set up ProxySQL -- Add query rules for query routing insert into mysql_query_rules(active,match_pattern, destination_hostgroup,apply) values(1,'^SELECT .* customerdb',0,1); insert into mysql_query_rules(active,match_pattern, destination_hostgroup,apply) values(1,'^SELECT .* productdb',1,1); insert into mysql_query_rules(active,match_pattern, destination_hostgroup,apply) values(1,'^SELECT .* orderdb',2,1); load mysql query rules to runtime; save mysql query rules to disk; Set up configuration
  • 8. 8 Set up ProxySQL -- You can check to connect db # mysql -utest_user -p'test1234' -h10.xx.xx.001 -P6033 -e"SELECT * from customerdb.member limit 10" # mysql -utest_user -p'test1234' -h10.xx.xx.001 -P6033 -e"SELECT * from orderdb.order limit 10" -- You can find a query log file(queries.log.0000000x) # cd /var/lib/proxysql # ls -al total 7876 drwxr-xr-x 4 proxysql proxysql 4096 Jun 20 14:47 . drwxr-xr-x. 27 root root 4096 Jun 19 18:26 .. -rw------- 1 proxysql proxysql 2177193 Jun 20 14:47 proxysql.log -rw-r--r-- 1 proxysql proxysql 6 Jun 20 11:18 proxysql.pid -rw------- 1 proxysql proxysql 2781184 Jun 20 14:47 proxysql_stats.db -rw------- 1 proxysql proxysql 89474 Jun 20 14:19 queries.log.00000001 Check query logging
  • 9. 9 Convert query log ProxySQL leaves a query log in binary format. We need to convert it to text format. ProxySQL supports tool that convert binary format file to text format.(eventslog_reader_sample) # git clone https://github.com/sysown/proxysql.git # cd proxysql/tools # ll total 128 -rwxr-xr-x 1 root root 2896 Jun 11 10:28 check_variables.pl -rw-r--r-- 1 root root 4527 Jun 11 10:28 eventslog_reader_sample.cpp -rw-r--r-- 1 root root 122 Jun 11 10:28 Makefile -rwxr-xr-x 1 root root 16869 Jun 11 10:28 proxysql_galera_checker.sh -rwxr-xr-x 1 root root 3574 Jun 11 10:28 proxysql_galera_writer.pl
  • 10. 10 Convert query log You can compile eventslog_reader_sample.cpp file. And you can convert binary query log file to text format. # make # ll total 128 -rwxr-xr-x 1 root root 2896 Jun 11 10:28 check_variables.pl -rwxr-xr-x 1 root root 87168 Jun 11 10:41 eventslog_reader_sample -rw-r--r-- 1 root root 4527 Jun 11 10:28 eventslog_reader_sample.cpp -rw-r--r-- 1 root root 122 Jun 11 10:28 Makefile -rwxr-xr-x 1 root root 16869 Jun 11 10:28 proxysql_galera_checker.sh -rwxr-xr-x 1 root root 3574 Jun 11 10:28 proxysql_galera_writer.pl # ./eventslog_reader_sample /var/lib/proxysql/queries.log.00000001 or # ./eventslog_reader_sample /var/lib/proxysql/queries.log.00000001 > result.txt
  • 11. 11 Convert query log You can see two types of query logs, binary and text. # tail -n 10 queries.log.00000001 ��:�L14SHOW SESSION VARIABLES LIKE 'lower_case_table_names'w�� test_userinformation_schema10.xxx.xxx.x:1475310.xx.xx.xx:3306�KSw/���RSELECT current_user()x�� test_userinformation_schema10.xxx.xxx.xx:1475310.xx.xx.xx:3306��cw�X��Efw�X�� ��l��SET CHARACTER SET utf8g�� test_userinformation_schema10.xxx.xxx.xxx:14753�����������pw�X���pw�X����8���Q0.xxx.x:14753x�� test_userinformation_schema10.xxx.xxx.x:1475310.xx.xx.xx:3306�K~w�X���w�X���$�f��SET SQL_SAFE_UPDATES=1o�� test_userinformation_schema10.xxx.xxx.x:14753����������‫ۏ‬w�X��‫ۏ‬w�X��yS��g�H�0.xxx.x:14753ION_ID()��� test_userinformation_schema10.xxx.xxx.x:1475310.xx.xx.xx:3306���w�X��h�w�X��Y�Z�~a��%SHOWorderdb10.xxx.xxx.x:14753����������ӹw�X��ӹw�X��0�o�/0.xxx.x:14753=1 # tail -n 10 queries_result.log.00000001 ProxySQL LOG QUERY: thread_id="30" username="test_user" schemaname="information_schema" client="10.xxx.xxx.x:63285" HID=0 server="10.xx.xx.xxx:3306" starttime="2019-06-20 17:35:20.108808" endtime="2019-06-20 17:35:20.109038" duration=230us digest="0xD8CCF296B01AC933" select * from orderdb.order limit 105 ProxySQL LOG QUERY: thread_id="30" username="test_user" schemaname="information_schema" client="10.xxx.xxx.x:63285" HID=0 server="10.xx.xx.xxx:3306" starttime="2019-06-20 17:35:21.300584" endtime="2019-06-20 17:35:21.300738" duration=154us digest="0xD8CCF296B01AC933" select * from customerdb.custom limit 100
  • 12. 12 Make cron job for converting query log file Query log file will be created sequentially, we need a script for converting binary query log file to text format file whenever new query log file is created. BIN TXTqueries.log.00000001 ./result/queries_result.log.00000001 ./eventslog_reader_sample queries.log.00000001 > ./result/queries_result.log.00000001 BIN TXTqueries.log.00000002 ./result/queries_result.log.00000002 BIN TXTqueries.log.00000003 ./result/queries_result.log.00000003
  • 13. 13 How user uses with workbench tool The user who uses to connect directly db can use like this with workbench. User has to use table name with database name(schema name) in query. (ex. orderdb.order)
  • 14. 14 Set up FileBeat Download # tar -xzvf filebeat-7.1.1-linux-x86_64.tar.gz -C /opt/ # vi filebeat.yml filebeat.inputs: - type: log enabled: true paths: - /var/lib/proxysql/result/queries_result.log.* multiline.pattern: '^ProxySQL' multiline.negate: true multiline.match: after output.logstash: hosts: ["localhost:5044"] https://www.elastic.co/kr/downloads/beats/filebeat
  • 15. 15 Set up Logstash Download # tar -xzvf logstash-7.1.1.tar.gz -C /opt/ # cd config # vi logstash.conf input { beats { port => 5044 } } ---continue behind https://www.elastic.co/kr/downloads/logstash
  • 16. 16 Set up Logstash # vi logstash.conf filter { mutate { gsub => ["message",""",""] } mutate { gsub => ["message",". ",".0"] } grok { match => {"message" => "ProxySQL LOG QUERY: thread_id=%{GREEDYDATA:thread_id} username=%{GREEDYDATA:username} schemaname=%{GREEDYDATA:schemaname} client=%{GREEDYDATA:client} HID=%{GREEDYDATA:HID} server=%{GREEDYDATA:server} starttime=%{TIMESTAMP_ISO8601:starttime} endtime=%{TIMESTAMP_ISO8601:endtime} duration=%{GREEDYDATA:duration} digest=%{DATA:digest}n%{GREEDYDATA:query}"} } } ---continue behind
  • 17. 17 Set up Logstash # vi logstash.conf output { elasticsearch { hosts => ["http://[elasticsearch_server_ip]:9200"] index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}" } }
  • 18. 18 Set up Elasticsearch Download # tar -xzvf elasticsearch-7.1.1-linux-x86_64.tar.gz -C /opt/ # cd config # vi elasticsearch.yml cluster.name: db-admin node.name: first-node path.data: /elasticsearch/data path.logs: /elasticsearch/log bootstrap.system_call_filter: false network.host: [elasticsearch_server_ip] http.port: 9200 cluster.initial_master_nodes: "first-node" https://www.elastic.co/kr/downloads/elasticsearch
  • 19. 19 Set up Kibana Download # tar -xzvf kibana-7.1.1-linux-x86_64.tar.gz -C /opt/ # cd config # vi kibana.yml server.host: "[kibana_server_ip]" elasticsearch.hosts: ["http://[elasticsearch_server_ip]:9200"] https://www.elastic.co/kr/downloads/kibana
  • 20. 20 -- Start Filebeat # cd /opt/filebeat-7.1.1-linux-x86_64 # ./filebeat -e -c filebeat.yml -- Start Logstash # cd /opt/logstash-7.1.1 # bin/logstash -f config/logstash.conf -- Start Elasticsearch # cd /opt/elasticsearch-7.1.1 # bin/elasticsearch -- Start Kibana # cd /opt/kibana-7.1.1-linux-x86_64 # bin/kibana Start ELK stack
  • 21. Kibana Web 21 Confirm the query logs in kibana web page http://kibana_server_ip:5601