SlideShare a Scribd company logo
1 of 31
Download to read offline
Python is your friend with
MySQL Shell
Ivan Ma
Principal Solutions Engineer, MySQL
Nov 06, 2020
Ivan Ma
All MySQL Certifications in 2009 @ Sun Microsystems
HK MySQL User Group Leaders
Sybase ASE/ ASA, Oracle, MySQL
2
Copyright © 2020, Oracle and/or its affiliates3
Python & MySQL !!!
Tools and programming ….
MySQL Shell Overview
4
JavaScript
SQL
Python
Output Formats
(Table, JSON,
Tabbed, Vertical)
Batch Execution
MySQL 8.0
Upgrade Checker
ImportJSON
Auto Completion &
Command History &
Themes
Document Store
X DevAPI
SQL CLI
InnoDB Cluster
Admin
API
ReplicaSet
Fast Data Dump
& Load
Parallel Table
Import
Extensible Plugin
& Reports
MySQL Protocols
• mysql
• mysqlx
Admin API
• InnoDB Cluster
• InnoDB ReplicaSet
• Sandboxes
Shell API
• OS utilities
• General purpose
functions
• Create Reports
• Create Plugins
• Manage Credentials
Utilities
• Upgrade Checker
• JSON Import
• Parallel import table
• Fast Data dump &
load
The MySQL Shell Toolbox
5
Support python “-m” as --pym
python –m pip install --upgrade pip
What if in MySQL Shell (8.0.22+)
New feature “--pym”
mysqlsh --pym pip install --upgrade pip
MySQL Shell 8.0.22
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted6
How To Run
Directly from command line:
# mysqlsh root:@localhost:3306 -e "util.checkForServerUpgrade();"
From JavaScript or Python mode inside Shell:
mysql-py [if connected] > util.check_for_server_upgrade();
mysql-js> util.checkForServerUpgrade("root@localhost:3306");
Function accepts connection options or if Shell is already connected to the
server you can run function without any arguments
MySQL 8 Upgrade Check
Confidential - Oracle Internal7
Document Store - Overview
"An easy, straight forward way to work with JSON
documents in MySQL"
A Document-oriented database built on top of MySQL
Relational database aspects are abstracted
• No tables, no columns, no schema
• Just JSON documents in collections
MySQL Shell 8.0
Confidential - Oracle Internal8
• Collection:
– New Schema Object
– Stored as InnoDB Table
– ACID compliance
– Transaction Support
– Replication Support
– Row Locking
Document Store – Demo
Demo
Run MySQL Shell to connect to DB (portx)
• switch languages sql js py
• mysqlsh-py> use world_x
• mysqlsh> db.get_collections()
• mysqlsh> db.get_tables()
• mysqlsh> table = db.get_table(‘country’)
• mysqlsh> table.select()
MySQL Shell 8.0
Confidential - Oracle Internal9
get_collections( )
Connecting mysqlx port (33060)
Copyright © 2020, Oracle and/or its affiliates10
db.countryinfo.find().limit(1)
Collection API – Retrieve Data and limit to 1 document
Copyright © 2020, Oracle and/or its affiliates11
db.country.select().limit(3)
Table API – SELECT 3 rows
Copyright © 2020, Oracle and/or its affiliates12
Scripting – Built-in API
MySQL Shell 8.0
13
InnoDB Cluster
MySQL
Server
Document Store
Admin APIX DevAPI
Shell API
API for managing and interacting with MySQL
All APIs available in Python / Javascript
Multiple sessions with Classic Protocol
Copyright © 2020, Oracle and/or its affiliates14
MySQL Shell provides DBA utilities
• Upgrade Checker
• JSON Import
• Parallel table import
• Dump & Load
Exposed in Shell API util module
DBA activities
Copyright © 2020, Oracle and/or its affiliates15
Copyright © 2020, Oracle and/or its affiliates16
Getting Uptime using the following SQL
mysql> SELECT TIME FORMAT(SEC_TO_TIME(VARIABLE_VALUE), '%Hh %im %ss') AS Uptime FROM
performance_schema.global_status where VARIABLE_NAME='Uptime’
Make it easy to be as report in MySQL Shell
Extensible - Reports
Copyright © 2020, Oracle and/or its affiliates17
What about DB Size
SELECT table_schema AS 'Database', ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS
'Size (MB)', count(table_name) as '# of tables' FROM information_schema.TABLES GROUP BY
table_schema
Extensible - Reports
Copyright © 2020, Oracle and/or its affiliates18
$HOME/.mysqlsh/init.d/dbsize.py
def dbsize(session):
stmt = "SELECT table_schema AS 'Database', ROUND(SUM(data_length + index_length) / 1024 /
1024, 2) AS 'Size (MB)', count(table_name) as '# of tables' FROM information_schema.TABLES GROUP
BY table_schema"
result = session.run_sql(stmt)
report = [ result.get_column_names()]
for row in result.fetch_all():
report.append(list(row))
return{ 'report': report}
Creating a Python Report in MySQL Shell
Copyright © 2020, Oracle and/or its affiliates19
$HOME/.mysqlsh/init.d/dbsize.py
shell.register_report(
'dbsize',
'list',
dbsize,
{
'brief': 'Shows Database Size.',
'details' : [ 'You need the SELECT privileges on information_schema.TABLES.*'],
'argc': '0'
}
)
Creating a Python Report in MySQL Shell
Copyright © 2020, Oracle and/or its affiliates20
dbsize
[opc@workshop16 (150.136.104.97) init.d]$ mysqlsh --py
MySQL Shell 8.0.22-commercial
Copyright (c) 2016, 2020, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type 'help' or '?' for help; 'quit' to exit.
MySQL Py >
Showing the reports
Copyright © 2020, Oracle and/or its affiliates21
dbsize
[opc@workshop16 (150.136.104.97) init.d]$ mysqlsh --py
MySQL Shell 8.0.22-commercial
Copyright (c) 2016, 2020, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type 'help' or '?' for help; 'quit' to exit.
MySQL Py > connect root@127.0.0.1:3306
Creating a session to 'root@127.0.0.1:3306'
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 29
Server version: 8.0.22-commercial MySQL Enterprise Server - Commercial
No default schema selected; type use <schema> to set one.
MySQL 127.0.0.1:3306 ssl Py>
Showing the reports
Copyright © 2020, Oracle and/or its affiliates22
dbsize
[opc@workshop16 (150.136.104.97) init.d]$ mysqlsh --py
MySQL Shell 8.0.22-commercial
Copyright (c) 2016, 2020, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type 'help' or '?' for help; 'quit' to exit.
MySQL Py > connect root@127.0.0.1:3306
Creating a session to 'root@127.0.0.1:3306'
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 29
Server version: 8.0.22-commercial MySQL Enterprise Server - Commercial
No default schema selected; type use <schema> to set one.
MySQL 127.0.0.1:3306 ssl Py > show
Available reports: dbsize, query, thread, threads, uptime.
MySQL 127.0.0.1:3306 ssl Py >
Showing the reports
Copyright © 2020, Oracle and/or its affiliates23
dbsize
[opc@workshop16 (150.136.104.97) init.d]$ mysqlsh --py
MySQL Shell 8.0.22-commercial
Copyright (c) 2016, 2020, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type 'help' or '?' for help; 'quit' to exit.
MySQL Py > connect root@127.0.0.1:3306
Creating a session to 'root@127.0.0.1:3306'
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 29
Server version: 8.0.22-commercial MySQL Enterprise Server - Commercial
No default schema selected; type use <schema> to set one.
MySQL 127.0.0.1:3306 ssl Py > show
Available reports: dbsize, query, thread, threads, uptime.
MySQL 127.0.0.1:3306 ssl Py >
Showing the reports
Copyright © 2020, Oracle and/or its affiliates24
dbsize
[opc@workshop16 (150.136.104.97) init.d]$ mysqlsh --py
MySQL Shell 8.0.22-commercial
Copyright (c) 2016, 2020, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type 'help' or '?' for help; 'quit' to exit.
MySQL Py > connect root@127.0.0.1:3306
Creating a session to 'root@127.0.0.1:3306'
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 29
Server version: 8.0.22-commercial MySQL Enterprise Server - Commercial
No default schema selected; type use <schema> to set one.
MySQL 127.0.0.1:3306 ssl Py > show
Available reports: dbsize, query, thread, threads, uptime.
MySQL 127.0.0.1:3306 ssl Py >
Showing the reports
Copyright © 2020, Oracle and/or its affiliates25
$HOME/.mysqlsh/plugins/ext/system_info/init.py
Plugin Group as containing folder under the plugins folder : $HOME/.mysqlsh/plugins
e.g. Folder : $HOME/.mysqlsh/plugins/ext/system_info
init.py
from mysqlsh.plugin_manager import plugin, plugin_function
@plugin
class system_info:
"""
System Information
A collection of tools to gather system information.
"""
Creating plugin [ annotation - @plugin ]
Copyright © 2020, Oracle and/or its affiliates26
$HOME/.mysqlsh/plugins/ext/system_info/init.py
init.py
….
@plugin_function("system_info.get_public_key")
def get_public_key(session=None):
if session is None:
import mysqlsh
shell = mysqlsh.globals.shell
session = shell.get_session()
if session is None:
print("No session specified. Either pass a session object to this function or connect the shell to a database")
return
if session is not None:
r = session.run_sql("SELECT VARIABLE_VALUE FROM performance_schema.global_status where variable_name =
'Caching_sha2_password_rsa_public_key'")
shell.dump_rows(r)
Creating plugin [ annotation - @plugin_function ]
Copyright © 2020, Oracle and/or its affiliates27
system_info is declared as Global Class
mysqlsh> h
….
- system_info System Information A collection of tools to gather system information.
…
MySQL 127.0.0.1:3306 ssl JS > system_info.get_public_key()
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------+
| VARIABLE_VALUE
|
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------+
| -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy9s+YkbuVoAupgNA1hR0
zEltnMlUnhOY2R1twu8rsEUOLlxBfTHYtgQsiRj4YqdjDURnrUO0Ylk6G7VMvXmu
zOwdHFvZ6HVEN4eTOmh2wvJzJ62rJhJWABZcJBiTnFXAWGunWvZx4yAVzz7Lirb1
XiEqdqeITi3xKfT4VCskV7X6+EbQxwR3eOWiXrCcHOjeJdNSUNmg2wMscTm7/pIk
dw3XrRGkhTBSg64K4x4FRgHy/p+o5suJNvr9MlgsQSigPF712rE0nwhVb/2vmDVY
aJ4S2aiUrYNtGmMhVPQSlGel1ckX+Dkr03LI08fug30OkA6scQfDJtdcNfzJZiLB
yQIDAQAB
Using the plugin
Copyright © 2020, Oracle and/or its affiliates28
Copyright © 2020, Oracle and/or its affiliates29
InnoDB Cluster and MySQL Shell
Creating InnoDB Cluster
MySQL Shell
Supporting SQL, Python and Javascript
XDev API - Document Store
Shell API – Interacting with MySQL
Admin API – Innodb Cluster
Utililities – DBA activities
Allow Scripting in Python
Extensible with Reports
Extensible with Plugins
A Powerful and Flexible MySQL Toolbox
Summary
Copyright © 2020, Oracle and/or its affiliates30
20201106 hk-py con-mysql-shell

More Related Content

What's hot

MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
MySQL Goes to 8!  FOSDEM 2020 Database Track, January 2nd, 2020MySQL Goes to 8!  FOSDEM 2020 Database Track, January 2nd, 2020
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
Geir Høydalsvik
 
MySQL cluster 72 in the Cloud
MySQL cluster 72 in the CloudMySQL cluster 72 in the Cloud
MySQL cluster 72 in the Cloud
Marco Tusa
 

What's hot (20)

Json within a relational database
Json within a relational databaseJson within a relational database
Json within a relational database
 
Discover the Power of the NoSQL + SQL with MySQL
Discover the Power of the NoSQL + SQL with MySQLDiscover the Power of the NoSQL + SQL with MySQL
Discover the Power of the NoSQL + SQL with MySQL
 
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql  - how do pdo, mysq-li, and x devapi do what they doPhp &amp; my sql  - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
 
Datacon LA - MySQL without the SQL - Oh my!
Datacon LA - MySQL without the SQL - Oh my! Datacon LA - MySQL without the SQL - Oh my!
Datacon LA - MySQL without the SQL - Oh my!
 
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
 
MySQL 8 for Developers
MySQL 8 for DevelopersMySQL 8 for Developers
MySQL 8 for Developers
 
MySQL For Oracle DBA's and Developers
MySQL For Oracle DBA's and DevelopersMySQL For Oracle DBA's and Developers
MySQL For Oracle DBA's and Developers
 
MySQL Replication Update - DEbconf 2020 presentation
MySQL Replication Update - DEbconf 2020 presentationMySQL Replication Update - DEbconf 2020 presentation
MySQL Replication Update - DEbconf 2020 presentation
 
Data dictionary pl17
Data dictionary pl17Data dictionary pl17
Data dictionary pl17
 
Adding Value to HBase with IBM InfoSphere BigInsights and BigSQL
Adding Value to HBase with IBM InfoSphere BigInsights and BigSQLAdding Value to HBase with IBM InfoSphere BigInsights and BigSQL
Adding Value to HBase with IBM InfoSphere BigInsights and BigSQL
 
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
MySQL Goes to 8!  FOSDEM 2020 Database Track, January 2nd, 2020MySQL Goes to 8!  FOSDEM 2020 Database Track, January 2nd, 2020
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
 
MariaDB pres at LeMUG
MariaDB pres at LeMUGMariaDB pres at LeMUG
MariaDB pres at LeMUG
 
Plmce 14 be a_hero_16x9_final
Plmce 14 be a_hero_16x9_finalPlmce 14 be a_hero_16x9_final
Plmce 14 be a_hero_16x9_final
 
cPanel now supports MySQL 8.0 - My Top Seven Features
cPanel now supports MySQL 8.0 - My Top Seven FeaturescPanel now supports MySQL 8.0 - My Top Seven Features
cPanel now supports MySQL 8.0 - My Top Seven Features
 
MySQL 5.7 + JSON
MySQL 5.7 + JSONMySQL 5.7 + JSON
MySQL 5.7 + JSON
 
MySQL Performance Best Practices
MySQL Performance Best PracticesMySQL Performance Best Practices
MySQL Performance Best Practices
 
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
MySQL 8 Tips and Tricks from Symfony USA 2018, San FranciscoMySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
 
MySQL cluster 72 in the Cloud
MySQL cluster 72 in the CloudMySQL cluster 72 in the Cloud
MySQL cluster 72 in the Cloud
 
MySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. RyengMySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. Ryeng
 
MySQL 8.0 Operational Changes
MySQL 8.0 Operational ChangesMySQL 8.0 Operational Changes
MySQL 8.0 Operational Changes
 

Similar to 20201106 hk-py con-mysql-shell

android sqlite
android sqliteandroid sqlite
android sqlite
Deepa Rani
 
The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...
The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...
The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...
Geir Høydalsvik
 

Similar to 20201106 hk-py con-mysql-shell (20)

MySQL Shell - The Best MySQL DBA Tool
MySQL Shell - The Best MySQL DBA ToolMySQL Shell - The Best MySQL DBA Tool
MySQL Shell - The Best MySQL DBA Tool
 
20190713_MySQL開発最新動向
20190713_MySQL開発最新動向20190713_MySQL開発最新動向
20190713_MySQL開発最新動向
 
MySQL 5.7 - What's new and How to upgrade
MySQL 5.7 - What's new and How to upgradeMySQL 5.7 - What's new and How to upgrade
MySQL 5.7 - What's new and How to upgrade
 
MySQL 5.7 - What's new, How to upgrade and Document Store
MySQL 5.7 - What's new, How to upgrade and Document StoreMySQL 5.7 - What's new, How to upgrade and Document Store
MySQL 5.7 - What's new, How to upgrade and Document Store
 
NoSQL and MySQL: News about JSON
NoSQL and MySQL: News about JSONNoSQL and MySQL: News about JSON
NoSQL and MySQL: News about JSON
 
Upgrade from MySQL 5.7 to MySQL 8.0
Upgrade from MySQL 5.7 to MySQL 8.0Upgrade from MySQL 5.7 to MySQL 8.0
Upgrade from MySQL 5.7 to MySQL 8.0
 
MySQL 8.0 from December London Open Source Database Meetup
MySQL 8.0 from December London Open Source Database MeetupMySQL 8.0 from December London Open Source Database Meetup
MySQL 8.0 from December London Open Source Database Meetup
 
20160821 coscup-my sql57docstorelab01
20160821 coscup-my sql57docstorelab0120160821 coscup-my sql57docstorelab01
20160821 coscup-my sql57docstorelab01
 
Getting Started with ElastiCache for Redis
Getting Started with ElastiCache for RedisGetting Started with ElastiCache for Redis
Getting Started with ElastiCache for Redis
 
Inno db 5_7_features
Inno db 5_7_featuresInno db 5_7_features
Inno db 5_7_features
 
MySQL Tech Café #8: MySQL 8.0 for Python Developers
MySQL Tech Café #8: MySQL 8.0 for Python DevelopersMySQL Tech Café #8: MySQL 8.0 for Python Developers
MySQL Tech Café #8: MySQL 8.0 for Python Developers
 
android sqlite
android sqliteandroid sqlite
android sqlite
 
MySQL For Oracle Developers
MySQL For Oracle DevelopersMySQL For Oracle Developers
MySQL For Oracle Developers
 
Mysql8for blr usercamp
Mysql8for blr usercampMysql8for blr usercamp
Mysql8for blr usercamp
 
MySQL Document Store (Oracle Code Warsaw 2018)
MySQL Document Store (Oracle Code Warsaw 2018)MySQL Document Store (Oracle Code Warsaw 2018)
MySQL Document Store (Oracle Code Warsaw 2018)
 
MySQL Shell for DBAs
MySQL Shell for DBAsMySQL Shell for DBAs
MySQL Shell for DBAs
 
Automatic upgrade and new error logging in my sql 8.0 oct
Automatic upgrade and new error logging in my sql 8.0 octAutomatic upgrade and new error logging in my sql 8.0 oct
Automatic upgrade and new error logging in my sql 8.0 oct
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 
The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...
The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...
The State of the Dolphin, MySQL Keynote at Percona Live Europe 2019, Amsterda...
 
MySQL NoSQL JSON JS Python "Document Store" demo
MySQL NoSQL JSON JS Python "Document Store" demoMySQL NoSQL JSON JS Python "Document Store" demo
MySQL NoSQL JSON JS Python "Document Store" demo
 

More from Ivan Ma

01 demystifying mysq-lfororacledbaanddeveloperv1
01 demystifying mysq-lfororacledbaanddeveloperv101 demystifying mysq-lfororacledbaanddeveloperv1
01 demystifying mysq-lfororacledbaanddeveloperv1
Ivan Ma
 

More from Ivan Ma (16)

Exploring MySQL Operator for Kubernetes in Python
Exploring MySQL Operator for Kubernetes in PythonExploring MySQL Operator for Kubernetes in Python
Exploring MySQL Operator for Kubernetes in Python
 
20200613 my sql-ha-deployment
20200613 my sql-ha-deployment20200613 my sql-ha-deployment
20200613 my sql-ha-deployment
 
20191001 bkk-secret-of inno-db_clusterv1
20191001 bkk-secret-of inno-db_clusterv120191001 bkk-secret-of inno-db_clusterv1
20191001 bkk-secret-of inno-db_clusterv1
 
20190817 coscup-oracle my sql innodb cluster sharing
20190817 coscup-oracle my sql innodb cluster sharing20190817 coscup-oracle my sql innodb cluster sharing
20190817 coscup-oracle my sql innodb cluster sharing
 
20190615 hkos-mysql-troubleshootingandperformancev2
20190615 hkos-mysql-troubleshootingandperformancev220190615 hkos-mysql-troubleshootingandperformancev2
20190615 hkos-mysql-troubleshootingandperformancev2
 
20180420 hk-the powerofmysql8
20180420 hk-the powerofmysql820180420 hk-the powerofmysql8
20180420 hk-the powerofmysql8
 
20171104 hk-py con-mysql-documentstore_v1
20171104 hk-py con-mysql-documentstore_v120171104 hk-py con-mysql-documentstore_v1
20171104 hk-py con-mysql-documentstore_v1
 
MySQL InnoDB Cluster and MySQL Group Replication @HKOSC 2017
MySQL InnoDB Cluster and MySQL Group Replication @HKOSC 2017MySQL InnoDB Cluster and MySQL Group Replication @HKOSC 2017
MySQL InnoDB Cluster and MySQL Group Replication @HKOSC 2017
 
What's New in MySQL 8.0 @ HKOSC 2017
What's New in MySQL 8.0 @ HKOSC 2017What's New in MySQL 8.0 @ HKOSC 2017
What's New in MySQL 8.0 @ HKOSC 2017
 
20161029 py con-mysq-lv3
20161029 py con-mysq-lv320161029 py con-mysq-lv3
20161029 py con-mysq-lv3
 
Hkosc group replication-lecture_lab07
Hkosc group replication-lecture_lab07Hkosc group replication-lecture_lab07
Hkosc group replication-lecture_lab07
 
20151010 my sq-landjavav2a
20151010 my sq-landjavav2a20151010 my sq-landjavav2a
20151010 my sq-landjavav2a
 
01 demystifying mysq-lfororacledbaanddeveloperv1
01 demystifying mysq-lfororacledbaanddeveloperv101 demystifying mysq-lfororacledbaanddeveloperv1
01 demystifying mysq-lfororacledbaanddeveloperv1
 
Exploring mysql cluster 7.4
Exploring mysql cluster 7.4Exploring mysql cluster 7.4
Exploring mysql cluster 7.4
 
20150110 my sql-performanceschema
20150110 my sql-performanceschema20150110 my sql-performanceschema
20150110 my sql-performanceschema
 
20141011 my sql clusterv01pptx
20141011 my sql clusterv01pptx20141011 my sql clusterv01pptx
20141011 my sql clusterv01pptx
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
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, ...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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
 

20201106 hk-py con-mysql-shell

  • 1. Python is your friend with MySQL Shell Ivan Ma Principal Solutions Engineer, MySQL Nov 06, 2020
  • 2. Ivan Ma All MySQL Certifications in 2009 @ Sun Microsystems HK MySQL User Group Leaders Sybase ASE/ ASA, Oracle, MySQL 2
  • 3. Copyright © 2020, Oracle and/or its affiliates3 Python & MySQL !!! Tools and programming ….
  • 4. MySQL Shell Overview 4 JavaScript SQL Python Output Formats (Table, JSON, Tabbed, Vertical) Batch Execution MySQL 8.0 Upgrade Checker ImportJSON Auto Completion & Command History & Themes Document Store X DevAPI SQL CLI InnoDB Cluster Admin API ReplicaSet Fast Data Dump & Load Parallel Table Import Extensible Plugin & Reports
  • 5. MySQL Protocols • mysql • mysqlx Admin API • InnoDB Cluster • InnoDB ReplicaSet • Sandboxes Shell API • OS utilities • General purpose functions • Create Reports • Create Plugins • Manage Credentials Utilities • Upgrade Checker • JSON Import • Parallel import table • Fast Data dump & load The MySQL Shell Toolbox 5
  • 6. Support python “-m” as --pym python –m pip install --upgrade pip What if in MySQL Shell (8.0.22+) New feature “--pym” mysqlsh --pym pip install --upgrade pip MySQL Shell 8.0.22 Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted6
  • 7. How To Run Directly from command line: # mysqlsh root:@localhost:3306 -e "util.checkForServerUpgrade();" From JavaScript or Python mode inside Shell: mysql-py [if connected] > util.check_for_server_upgrade(); mysql-js> util.checkForServerUpgrade("root@localhost:3306"); Function accepts connection options or if Shell is already connected to the server you can run function without any arguments MySQL 8 Upgrade Check Confidential - Oracle Internal7
  • 8. Document Store - Overview "An easy, straight forward way to work with JSON documents in MySQL" A Document-oriented database built on top of MySQL Relational database aspects are abstracted • No tables, no columns, no schema • Just JSON documents in collections MySQL Shell 8.0 Confidential - Oracle Internal8 • Collection: – New Schema Object – Stored as InnoDB Table – ACID compliance – Transaction Support – Replication Support – Row Locking
  • 9. Document Store – Demo Demo Run MySQL Shell to connect to DB (portx) • switch languages sql js py • mysqlsh-py> use world_x • mysqlsh> db.get_collections() • mysqlsh> db.get_tables() • mysqlsh> table = db.get_table(‘country’) • mysqlsh> table.select() MySQL Shell 8.0 Confidential - Oracle Internal9
  • 10. get_collections( ) Connecting mysqlx port (33060) Copyright © 2020, Oracle and/or its affiliates10
  • 11. db.countryinfo.find().limit(1) Collection API – Retrieve Data and limit to 1 document Copyright © 2020, Oracle and/or its affiliates11
  • 12. db.country.select().limit(3) Table API – SELECT 3 rows Copyright © 2020, Oracle and/or its affiliates12
  • 13. Scripting – Built-in API MySQL Shell 8.0 13 InnoDB Cluster MySQL Server Document Store Admin APIX DevAPI Shell API API for managing and interacting with MySQL All APIs available in Python / Javascript
  • 14. Multiple sessions with Classic Protocol Copyright © 2020, Oracle and/or its affiliates14
  • 15. MySQL Shell provides DBA utilities • Upgrade Checker • JSON Import • Parallel table import • Dump & Load Exposed in Shell API util module DBA activities Copyright © 2020, Oracle and/or its affiliates15
  • 16. Copyright © 2020, Oracle and/or its affiliates16
  • 17. Getting Uptime using the following SQL mysql> SELECT TIME FORMAT(SEC_TO_TIME(VARIABLE_VALUE), '%Hh %im %ss') AS Uptime FROM performance_schema.global_status where VARIABLE_NAME='Uptime’ Make it easy to be as report in MySQL Shell Extensible - Reports Copyright © 2020, Oracle and/or its affiliates17
  • 18. What about DB Size SELECT table_schema AS 'Database', ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 'Size (MB)', count(table_name) as '# of tables' FROM information_schema.TABLES GROUP BY table_schema Extensible - Reports Copyright © 2020, Oracle and/or its affiliates18
  • 19. $HOME/.mysqlsh/init.d/dbsize.py def dbsize(session): stmt = "SELECT table_schema AS 'Database', ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 'Size (MB)', count(table_name) as '# of tables' FROM information_schema.TABLES GROUP BY table_schema" result = session.run_sql(stmt) report = [ result.get_column_names()] for row in result.fetch_all(): report.append(list(row)) return{ 'report': report} Creating a Python Report in MySQL Shell Copyright © 2020, Oracle and/or its affiliates19
  • 20. $HOME/.mysqlsh/init.d/dbsize.py shell.register_report( 'dbsize', 'list', dbsize, { 'brief': 'Shows Database Size.', 'details' : [ 'You need the SELECT privileges on information_schema.TABLES.*'], 'argc': '0' } ) Creating a Python Report in MySQL Shell Copyright © 2020, Oracle and/or its affiliates20
  • 21. dbsize [opc@workshop16 (150.136.104.97) init.d]$ mysqlsh --py MySQL Shell 8.0.22-commercial Copyright (c) 2016, 2020, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help' or '?' for help; 'quit' to exit. MySQL Py > Showing the reports Copyright © 2020, Oracle and/or its affiliates21
  • 22. dbsize [opc@workshop16 (150.136.104.97) init.d]$ mysqlsh --py MySQL Shell 8.0.22-commercial Copyright (c) 2016, 2020, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help' or '?' for help; 'quit' to exit. MySQL Py > connect root@127.0.0.1:3306 Creating a session to 'root@127.0.0.1:3306' Fetching schema names for autocompletion... Press ^C to stop. Your MySQL connection id is 29 Server version: 8.0.22-commercial MySQL Enterprise Server - Commercial No default schema selected; type use <schema> to set one. MySQL 127.0.0.1:3306 ssl Py> Showing the reports Copyright © 2020, Oracle and/or its affiliates22
  • 23. dbsize [opc@workshop16 (150.136.104.97) init.d]$ mysqlsh --py MySQL Shell 8.0.22-commercial Copyright (c) 2016, 2020, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help' or '?' for help; 'quit' to exit. MySQL Py > connect root@127.0.0.1:3306 Creating a session to 'root@127.0.0.1:3306' Fetching schema names for autocompletion... Press ^C to stop. Your MySQL connection id is 29 Server version: 8.0.22-commercial MySQL Enterprise Server - Commercial No default schema selected; type use <schema> to set one. MySQL 127.0.0.1:3306 ssl Py > show Available reports: dbsize, query, thread, threads, uptime. MySQL 127.0.0.1:3306 ssl Py > Showing the reports Copyright © 2020, Oracle and/or its affiliates23
  • 24. dbsize [opc@workshop16 (150.136.104.97) init.d]$ mysqlsh --py MySQL Shell 8.0.22-commercial Copyright (c) 2016, 2020, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help' or '?' for help; 'quit' to exit. MySQL Py > connect root@127.0.0.1:3306 Creating a session to 'root@127.0.0.1:3306' Fetching schema names for autocompletion... Press ^C to stop. Your MySQL connection id is 29 Server version: 8.0.22-commercial MySQL Enterprise Server - Commercial No default schema selected; type use <schema> to set one. MySQL 127.0.0.1:3306 ssl Py > show Available reports: dbsize, query, thread, threads, uptime. MySQL 127.0.0.1:3306 ssl Py > Showing the reports Copyright © 2020, Oracle and/or its affiliates24
  • 25. dbsize [opc@workshop16 (150.136.104.97) init.d]$ mysqlsh --py MySQL Shell 8.0.22-commercial Copyright (c) 2016, 2020, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help' or '?' for help; 'quit' to exit. MySQL Py > connect root@127.0.0.1:3306 Creating a session to 'root@127.0.0.1:3306' Fetching schema names for autocompletion... Press ^C to stop. Your MySQL connection id is 29 Server version: 8.0.22-commercial MySQL Enterprise Server - Commercial No default schema selected; type use <schema> to set one. MySQL 127.0.0.1:3306 ssl Py > show Available reports: dbsize, query, thread, threads, uptime. MySQL 127.0.0.1:3306 ssl Py > Showing the reports Copyright © 2020, Oracle and/or its affiliates25
  • 26. $HOME/.mysqlsh/plugins/ext/system_info/init.py Plugin Group as containing folder under the plugins folder : $HOME/.mysqlsh/plugins e.g. Folder : $HOME/.mysqlsh/plugins/ext/system_info init.py from mysqlsh.plugin_manager import plugin, plugin_function @plugin class system_info: """ System Information A collection of tools to gather system information. """ Creating plugin [ annotation - @plugin ] Copyright © 2020, Oracle and/or its affiliates26
  • 27. $HOME/.mysqlsh/plugins/ext/system_info/init.py init.py …. @plugin_function("system_info.get_public_key") def get_public_key(session=None): if session is None: import mysqlsh shell = mysqlsh.globals.shell session = shell.get_session() if session is None: print("No session specified. Either pass a session object to this function or connect the shell to a database") return if session is not None: r = session.run_sql("SELECT VARIABLE_VALUE FROM performance_schema.global_status where variable_name = 'Caching_sha2_password_rsa_public_key'") shell.dump_rows(r) Creating plugin [ annotation - @plugin_function ] Copyright © 2020, Oracle and/or its affiliates27
  • 28. system_info is declared as Global Class mysqlsh> h …. - system_info System Information A collection of tools to gather system information. … MySQL 127.0.0.1:3306 ssl JS > system_info.get_public_key() +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------+ | VARIABLE_VALUE | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------+ | -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy9s+YkbuVoAupgNA1hR0 zEltnMlUnhOY2R1twu8rsEUOLlxBfTHYtgQsiRj4YqdjDURnrUO0Ylk6G7VMvXmu zOwdHFvZ6HVEN4eTOmh2wvJzJ62rJhJWABZcJBiTnFXAWGunWvZx4yAVzz7Lirb1 XiEqdqeITi3xKfT4VCskV7X6+EbQxwR3eOWiXrCcHOjeJdNSUNmg2wMscTm7/pIk dw3XrRGkhTBSg64K4x4FRgHy/p+o5suJNvr9MlgsQSigPF712rE0nwhVb/2vmDVY aJ4S2aiUrYNtGmMhVPQSlGel1ckX+Dkr03LI08fug30OkA6scQfDJtdcNfzJZiLB yQIDAQAB Using the plugin Copyright © 2020, Oracle and/or its affiliates28
  • 29. Copyright © 2020, Oracle and/or its affiliates29 InnoDB Cluster and MySQL Shell Creating InnoDB Cluster
  • 30. MySQL Shell Supporting SQL, Python and Javascript XDev API - Document Store Shell API – Interacting with MySQL Admin API – Innodb Cluster Utililities – DBA activities Allow Scripting in Python Extensible with Reports Extensible with Plugins A Powerful and Flexible MySQL Toolbox Summary Copyright © 2020, Oracle and/or its affiliates30