MySQL Shell - The Best MySQL DBA Tool

miguelgaraujo 652 views 50 slides Oct 16, 2020
Slide 1
Slide 1 of 50
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50

About This Presentation

Session presented at Oracle Developer Live - MySQL, 2020. Recording available at https://developer.oracle.com/developer-live/mysql/

Abstract:

MySQL Shell is the new, advanced command-line client and editor for MySQL. It sends SQL statements to MySQL server, supports both the classic MySQL protocol...


Slide Content

MySQL Shell
The Best MySQL DBA Tool
Miguel Araújo
Principal Software Developer
MySQL, Oracle
@m1guelaraujo

The following is intended to outline our general product direction. It is
intended for information purposes only, and may not be incorporated into
any contract. It is not a commitment to deliver any material, code, or
functionality, and should not be relied upon in making purchasing decisions.
The development, release, timing,and pricing of any features or functionality
described for Oracle’s products may change and remains at the sole
discretion of Oracle Corporation.
Safe harbor statement
Copyright © 2020, Oracle and/or its affiliates2

MySQL turned 25 years old this year!
It’s a relational database:
•SQL
•Has a command-line SQL client: mysql
ButMySQLisn’tjustrelationaldatabases...
Context
Copyright © 2020, Oracle and/or its affiliates3

Modern, new command-line client for MySQL
Written from scratch, supporting:
•MySQL Document Store (NoSQL, X DevAPI)
•JSON Documents / SQL Tables
•SQL, Python and JavaScript
•Fully customizable
Meet MySQL Shell
Copyright © 2020, Oracle and/or its affiliates4

Copyright © 2020, Oracle and/or its affiliates5
There’smuchmore in Shell thanmeetstheeye!

A DBA and Developer Toolbox:
•SQL, JavaScript and Python support
•Auto-completion
•Command history
•Integrated built-in help system
•Customizable prompts / colors
•APIs and Utilities built-in
•Extensible
•Open-source!
MySQL Shell
Copyright © 2020, Oracle and/or its affiliates6

Copyright © 2020, Oracle and/or its affiliates7
MySQL Shell Overview

Copyright © 2020, Oracle and/or its affiliates8
MySQL Shell Overview

MySQL Protocols:
•mysql
•mysqlx
The MySQL Toolbox
Copyright © 2020, Oracle and/or its affiliates9

MySQL Protocols:
•mysql
•mysqlx
ShellAPI:
•OS utilities
•General purpose
functions
•Create Reports
•Create Plugins
•Manage
Credentials
The MySQL Toolbox
Copyright © 2020, Oracle and/or its affiliates10

MySQL Protocols:
•mysql
•mysqlx
ShellAPI:
•OS utilities
•General purpose
functions
•Create Reports
•Create Plugins
•Manage
Credentials
AdminAPI:
•InnoDBCluster
•InnoDBReplicaSet
•Sandboxes
The MySQL Toolbox
Copyright © 2020, Oracle and/or its affiliates11

MySQL Protocols:
•mysql
•mysqlx
ShellAPI:
•OS utilities
•General purpose
functions
•Create Reports
•Create Plugins
•Manage
Credentials
AdminAPI:
•InnoDBCluster
•InnoDBReplicaSet
•Sandboxes
The MySQL Toolbox
Copyright © 2020, Oracle and/or its affiliates12
Utilities:
•Upgrade checker
•JSON import
•Parallel import
table
•Instance and
Schema dump
•Dump loading

•SQL
•JavaScript
•Full JS engine (V8)
•All core language features
•Python
•3.6+
Multi-language Support
Copyright © 2020, Oracle and/or its affiliates13

•Auto-complete
•Customizable prompt with colors and state information
Pleasant experience
Copyright © 2020, Oracle and/or its affiliates14

•Auto-complete
•Customizable prompt with colors and state information
•Command history; Pager: more/less
Pleasant experience
Copyright © 2020, Oracle and/or its affiliates15

•Integrated built-in Help system
•Accessible via:
•\? <topic>
•\help<topic>
•All functionality available
Pleasant experience
Copyright © 2020, Oracle and/or its affiliates16

•APIs for managing and interacting with MySQL
•All APIs available in JavaScript and Python:
•X DevAPI
•ShellAPI
•AdminAPI
•Flexibility and power of choice
Built-in APIs
Copyright © 2020, Oracle and/or its affiliates17

Classic protocol
//Print a welcome message
println('Create a local MySQL account with special privileges.');
// Prompt for the username if not passed by argument
if(sys.argv[1]==undefined) {
username=shell.prompt('Please enter the username for the account: ');
} else{
username=sys.argv[1];
}
// Prompt for the password
pwd=shell.prompt('Please enter a password for the account ' +username+': ',
{type: 'password'});
Database APIs
Copyright © 2020, Oracle and/or its affiliates18

Classic protocol
// List of required grants
vargrantsList=[
'USAGE ON *.*',
'SELECT, INSERT, UPDATE, DELETE, CREATE TEMPORARY TABLES ON world_x.*'
];
try {
// Create the account
session.runSql('CREATE USER ?@\'localhost\'IDENTIFIED BY ?', [username, pwd]);
for(vargrant ingrantsList) {
session.runSql('GRAN ' +grantsList[grant] +'TO ?@\'localhost\'',
[username])
}
} catch(err) { (...)}
Database APIs
Copyright © 2020, Oracle and/or its affiliates19

Multiple MySQL connections
Copyright © 2020, Oracle and/or its affiliates20
session1 =mysql.get_session("admin@t480:3330")
session2 =mysql.get_session("root@localhost:3306")
session1.run_sql("SELEC @@hostname, @@port;")
result =session2.run_sql("SELEC * FROM performance_schema.global_status;")
row =result.fetch_all()
print(row[10])
•Work with multiple MySQL sessions simultaneously
•Using ShellAPIand/or X DevAPI
Example in Python:

•DBA focused:
•Upgrade Checker
•JSON import
•Parallel table import
•Dump & load
•Session inspector
•Exposed on theShellAPIutilmodule
Utilities
Copyright © 2020, Oracle and/or its affiliates21

Upgrade Checker
Copyright © 2020, Oracle and/or its affiliates22
//Available through the utilobject
mysqlsh-js> util.checkForServerUpgrade("root@localhost");
•Check for compatibility issues for upgrading to MySQL 8
•5.7à8.0+ (current Shell version)
•Warns about required actions:
•Obsolete/conflicting settings
•Schema properties requiring changes
•Can check .cnffiles

Upgrade Checker
Copyright © 2020, Oracle and/or its affiliates23

API Command-line Integration
Copyright © 2020, Oracle and/or its affiliates24
•AllAPIs are available on the command-line
•Accessible using the special delimiter character --
•Arguments follow a syntax suitable for command-line use:
mysqlsh[shell options] --<object> <command> [command options]
Example:
mysqlshadmin@t480 --cluster status --extended=1

API Command-line Integration
Copyright © 2020, Oracle and/or its affiliates25
•Integrating Shell’s Upgrade Checker with Puppet:
service { 'mysql':
ensure=>latest,
enable=>true,
require=>Package ['mysql-server-community'],
before=>Exec ['upgrade checker']
}
exec { "upgrade checker":
command=>"mysqlsh--utilcheck-for-server-upgrade --user=root --host=localhost
--port=33100 --password='mypassword'"
require=>service["mysql"]
}

JSON Import
Copyright © 2020, Oracle and/or its affiliates26
•Import JSON documents into a collection or relational table
•Removes the need to use multiple INSERTstatements or write scripts
•Supports BSON data types
//Available through the utilobject
mysqlsh-js> util.importJson("/home/miguel/banks.json", {schema:"docstore",
collection: "banks"})
//Very handy to be used with the API command-line integration
$mysqlshadmin@localhost/docstore--utilimport-jsonbanks.json--collection=banks

Dump & Load
Copyright © 2020, Oracle and/or its affiliates27
•Powerful utilities to make it easy to do logical dumps and load of:
•Awhole database instance
•A set of schemas
•Focus on ease of use:
//Dump and entire database instance, including users
util.dumpInstance(...)
//Dump a set of schemas
util.dumpSchemas(...)
//Load a dump into a target database
util.loadDump(...)

Dump & Load
28
•Focus on performance:
•Multi-threaded dump and load
•Concurrent execution of dump and load
•Built-in compression (zstd& gzip)
•Focus on integration:
•Dump & Load straight to/from OCI Object Storage
•Compatibility modes for importing into OCI MySQL Database Service
Copyright © 2020, Oracle and/or its affiliates

Dump & Load
Copyright © 2020, Oracle and/or its affiliates29

Replication & High Availability
Copyright © 2020, Oracle and/or its affiliates30
•MySQL offered individual components to achieve Highly Available setups
•Leaving it up to the user to set-up the architectures…

Replication & High Availability
Copyright © 2020, Oracle and/or its affiliates31
•MySQL offered individual components to achieve Highly Available setups
•Leaving it up to the user to set-up the architectures…
1.Provisioning, restoring backups
2.User management
3.Replication configuration
4.Deployment of client proxies
5.Manually changing topologies (or rely on external tools)
6.Use additional monitoring tools

Copyright © 2020, Oracle and/or its affiliates32
A lot of work for DBA’s and
SysAdmins, who spend their
time automating.

AdminAPI
33
•Setup and Management of InnoDBCluster / ReplicaSet
•Hides the complexity of:
•Configuration
•Provisioning
•Orchestration
•Simple and straight-forward
•Doesn’t require MySQL expertise
•Flexible, powerful and secure
MySQL
Administration
Copyright © 2020, Oracle and/or its affiliates

§MySQL Group Replication
§MySQL Shell
§MySQL Router
Complete HA Solution!
MySQL InnoDBCluster
Copyright © 2020, Oracle and/or its affiliates34

§MySQL Replication
§MySQL Shell
§MySQL Router
Simple but complete Replication
Solution!
MySQL InnoDBReplicaSet
Copyright © 2020, Oracle and/or its affiliates35

Creating a Cluster
//Configure Instances for Cluster usage
mysqlsh-js> dba.configureInstance("root@mysql1", {clusterAdmin:"admin"})
(...)
//Create the Cluster
mysqlsh-js> \c admin@mysql1
mysqlsh-js> cluster= dba.createCluster("myCluster")
//Add Instances to the Cluster
mysqlsh-js> cluster.addInstance("mysql2")
AdminAPI
Copyright © 2020, Oracle and/or its affiliates36

Creating a ReplicaSet
//Configure Instances for ReplicaSetusage
mysqlsh-js> dba.configureReplicaSetInstance("root@mysql4", {clusterAdmin:"admin"})
(...)
//Create the ReplicaSet
mysqlsh-js> \c admin@mysql4
mysqlsh-js> replicaset= dba.createReplicaSet("myReplicaSet")
//Add Instances to the ReplicaSet
mysqlsh-js> replicaset.addInstance("mysql5")
AdminAPI
Copyright © 2020, Oracle and/or its affiliates37

Couldn’t be
easier!
Copyright © 2020, Oracle and/or its affiliates38
Powerful solutions supported by Shell’s
ease of use.

•General purpose functions and properties
•OS utilities
•Credential Manager
•Extend MySQL Shell
•Reports
•Plugins
ShellAPI
Copyright © 2020, Oracle and/or its affiliates39

Credential Manager
Copyright © 2020, Oracle and/or its affiliates40
•Store passwords using a secret store or keychain
•Greatly improves usability and saves time
•Built-in support for:
•MySQL login-path (mysql_config_editor)
•macOSkeychain
•Windows Credentials Management API

Credential Manager
Copyright © 2020, Oracle and/or its affiliates41
//List available Secret Stores
mysqlsh-js> shell.listCredentialHelpers();
[
"keychain",
"login-path"
]
//List stored credentials
mysqlsh-js> shell.listCredentials();
[
"miguel@t480:3306",
"[email protected]:3306",
"[email protected]:3306"
]

Extend MySQL Shell
Copyright © 2020, Oracle and/or its affiliates42
•Extend Shell functionality through plugins
•Create new global reports or extensions objects
•Available in JavaScript or Python
•All Shell APIs available
•Automatically loaded at start-up

Extend MySQL Shell
Copyright © 2020, Oracle and/or its affiliates43
//Register a user-defined report
shell.registerReport([name, type, report[, description]);
//Create an extension object
shell.createExtensionObject();
//Add a new member into an extension object
shell.addExtensionObjectMember([object, name, member[, definition]);
//Register an extension object as a Shell global object
shell.registerGlobal(name, object[, definition]);
•API to register extensions:

Create a Report to get the uptime
Copyright © 2020, Oracle and/or its affiliates44
defuptime(session):
stmt="SELEC TIME_FORMAT(SEC_TO_TIME(VARIABLE_VALUE ), '%Hh%im%ss') AS Uptime
FROM performance_schema.global_statusWHERE VARIABLE_VALUE='Uptime';"
result =session.run_sql(stmt)
report =[result.get_column_names()]
for row inresult.fetch_all():
report.append(list(row))
return{'report': report}
.mysqlsh/init.d/uptime.py:0

Create a Report to get the uptime
Copyright © 2020, Oracle and/or its affiliates45
shell.register_report(
'uptime',
'list',
uptime,
{
'brief': 'Shows Server Uptime.',
'details': ['You need the SELEC privileges on performance_schema.*'],
'arg': '0'
}
}
.mysqlsh/init.d/uptime.py:13

Create a Report to get the uptime
Copyright © 2020, Oracle and/or its affiliates46

Create a Plugin for system information
Copyright © 2020, Oracle and/or its affiliates47
.mysqlsh/plugins/ext/system_info/init.py
frommysqlsh.plugin_managerimportplugin, plugin_function
@plugin
classsystem_info:
"""
System Information
A collection of tools to gather system information.
"""
@plugin_function("system_info.get_public_key")
defget_public_key(session=None, verbose=False):
"""
Get RSA Public Key of (...)

Create a Plugin for system information
Copyright © 2020, Oracle and/or its affiliates48

•MySQLShell’sgoalisto betheprimaryfrontendfor mostofMySQL
functionality
•It’sanunifiedinterface for DevelopersandDBAs
•Bringseaseofuse for complextasks
•Frontendmanager for InnoDBCluster andInnoDBReplicaSet
•Extensibleandopen-source!
Summary
Copyright © 2020, Oracle and/or its affiliates49

üGiveita try! dev.mysql.com/downloads/shell/
üCheckandcontributeto lefred’scollectionofMySQLShell plugins:
github.com/lefred/mysqlshell-plugins
üBlogs:mysqlhighavailability.com/mysqlserverteam.com
üReachusout onSlack! mysqlcommunity.slack.com
#shell / #mysql_innodb_cluster
Additional Resources
Copyright © 2020, Oracle and/or its affiliates50