More Related Content Similar to 20201106 hk-py con-mysql-shell (20) 20201106 hk-py con-mysql-shell1. 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
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
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
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
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
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