SlideShare a Scribd company logo
MySQl aS A
dOCUMENTSTORE
David.Stokes@Oracle.com @ Stoker
slideshare.net/davidmstokes
elelphantanddolphin.blogger.com
"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, AND TIMING OF ANY FEATURES
OR FUNCTIONALITY DESCRIBED FOR ORACLE'S PRODUCTS
REMAINS AT THE SOLE DISCRETION OF ORACLE."
Safe Harbor
2
Hello!I am Dave Stokes, MySQL Community Manager
David.Stokes@Oracle.com @Stoker
Slides at Slideshare.net/davidmstokes
Blogs: Opensourcedba.wordpress.com
elephantanddolphin.blogger.com 3
1.
Json & json dATATYPE
MySQL 5.7
4
“JSON JavaScript Object
Notation is a lightweight
data-interchange format. It
is built on two structures:
A collection of name/vaule
pairs and and ordered list
of values.”
5
MySQL JSON data type
× Schemaless data
× UTF8MB4
× It is just like a INT, or REAL, or TIMESTAMP. You
can store an entire document in a column of a
row of a table.
You could store JSON in a CHAR/TEXT field but it is
not sexy and you end up using REGEXP --- Ughhh!
6
Big
Very few developers are learning SQL (Structured
Query Language), Relational Theory, Sets, data
normalization or other database related subjects!
7
SQL
Data is normalized and
formats are rigorously
enforced. JOIN and logical
operators allow complex
queries. Costly to change
schema.
The big contest of 2014 In case you missed it
NoSQL
Schemaless, no need for
relational overhead,
changes as fast as the
data, easy to implement.
8
MySQl offers both SQL & NoSQL
RDMS
Good old relational
database
InnoDB/Memcached
This plug-in allows
direct access to
InnoDB or NDB
storage engine data
using the
memcached
protocol, up to 9x
faster
JSON
Data is stored in
JSON format. No
schema needed.
Functions supplied
for SQL based
access &
manipulation
9
But what if you
wanted a database
but do not know
sql?
10
New
Definitions
11
12
JSON Documents and Collections
A JSON document is a data structure
composed of field/value pairs stored
within a collection. The values of fields
often contain other documents, arrays,
and lists of documents.
CRUD
Operations
Create, Read, Update and
Delete (CRUD) operations are
the four basic operations that
can be performed on a
database Collection or Table
13
X Protocol
The X Protocol supports both
CRUD and SQL operations,
authentication via SASL,
allows streaming (pipelining)
of commands and is extensible
on the protocol and the
message layer.
14
Mysqlsh has three modes
js -- JavaScript
py -- python
SQL -- SQL (like old
Mysql shell)
15
1. MySQL 5.17.12 or later
2. Install X plugin
a. mysql> INSTALL PLUGINmysqlx SONAME
“mysqlx.so”; OR
b. mysqlsh -u user -h localhost --classic --dba
enableXProtocol
3. Install mysqlsh *
16
Installation needs
● Note: Installation of
MySQL Shell using the
MySQL APT repository is
only supported on Ubuntu
14.04 LTS (“Trusty Tahr”)
and Ubuntu 15.10 (“Wily
Werewolf”).
mysqlsh
$ mysqlsh --uri dstokes:hidave@localhost world_x --node
mysqlx: [Warning] Using a password on the command line interface can be insecure.
Creating a Node Session to dstokes@localhost:33060/world_x
Default schema `world_x` accessible through db.
Welcome to MySQL Shell 1.0.5 Development Preview
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help', 'h' or '?' for help, type 'quit' or 'q' to exit.
Currently in JavaScript mode. Use sql to switch to SQL mode and execute queries.
mysql-js>
17
MySQLSh shell Sessions
MySQL Shell is a unified interface
to operate MySQL Server through
scripting languages such as
JavaScript or Python. To maintain
compatibility with previous
versions, SQL can also be
executed in certain modes. A
connection to a MySQL server is
required. In MySQL Shell these
connections are handled by a
Session object.
18
XSession: Use this session type for new
application development. It offers the
best integration with MySQL Server, and
therefore, it is used by default. SQL
execution is not supported
Node Session: Use this session type for
SQL execution on a MySQL Server with
the X Protocol enabled. SQL execution
is available
Classic Session Use this session type to
interact with MySQL Servers that do not
have the X Protocol enabled
● --node creates a Node Session.
● --classic creates a Classic Session.
● --x creates an XSession.
session
mysql-js> session
<NodeSession:dstokes@localhost:33060/world_x>
mysql-js>
The above tells us we are a JavaScript mode
Account is dstokes@localhost on port 33060
The Database/schema is world_x
19
session
mysql-js> session
<NodeSession:dstokes@localhost:33060/world_x>
mysql-js>
The above tells us we are a JavaScript mode
Account is dstokes@localhost on port 33060
The Database/schema is world_x
20
WHICH DATABASE AND WHICH COLLECTION
mysql-js> db
<Schema:world_x>
mysql-js> db.getCollections()
[
<Collection:countryinfo>
]
mysql-js>
21
<-- The Database
← the table
Batch Mode Example
22
echo "SELECT * FROM world_x.city LIMIT 2;" | mysqlsh --json=pretty --sqlc --uri user@host
{
"executionTime": "0.00 sec",
"info": "",
"rows": [
{
"ID": 1,
"Name": "Kabul",
"CountryCode": "AFG",
"District": "Kabol",
"Info": "{"Population": 1780000}"
},
{
"ID": 2,
"Name": "Qandahar",
"CountryCode": "AFG",
"District": "Qandahar",
"Info": "{"Population": 237500}"
}
],
"warningCount": 0,
"warnings": [],
"hasData": true,
"affectedRowCount": 0,
"autoIncrementValue": 0
}
This is a batch mode
example where a query is
sent to mysqlsh in sql mode
and the output is produced
in ‘pretty’ JSON format.
Interactive help
mysql-js> var mySchema =
session.getSchema('Zendcon')
The schema Zendcon does not
exist, do you want to create it?
[y/N]: y
mysql-js> mySchema
<Schema:Zendcon>
mysql-js>
Note the interactive error
correction when a non
existent schema is
accessed.
23
db
24
$ mysqlsh --uri dstokes:hidave@localhost world_x
Default schema `world_x` accessible through db.
...
mysql-js> db
<Schema:world_x>
mysql-js> db.getCollections()
[
<Collection:countryinfo>
]
mysql-js> db.countryinfo.find().limit(1)
[
{
"GNP": 828,
"IndepYear": null,
"Name": "Aruba" ….
db is a global variable
assigned to the current
active schema that you
specified on the command
line.
Basic Operators
25
db.name.add()
The add() method inserts one document or a list of documents into the named collection.
db.name.find()
The find() method returns some or all documents in the named collection.
db.name.modify()
The modify() method updates documents in the named collection.
db.name.remove()
The remove() method deletes one document or a list of documents from the named collection.
Create a Collection named ‘Zendcon’ and list all collections
26
mysql-js> db.createCollection("Zendcon")
<Collection:Zendcon>
mysql-js> db.getCollections()
[
<Collection:Zendcon>,
<Collection:countryinfo>
]
mysql-js>
mysql-js>db.countryinfo.add(
{
GNP: .6,
IndepYear: 1967,
Name: "Sealand",
_id: "SEA",
demographics: {
LifeExpectancy: 79,
Population: 27
},
geography: {
Continent: "Europe",
Region: "British Islands",
SurfaceArea: 193
},
government: {
GovernmentForm: "Monarchy",
HeadOfState: "Michael Bates"
}
}
)
27
use world_x
So lets use the
world_x sample
database and
add a new
record!
28
mysql-js> db.countryinfo.find("_id = 'USA'")
[
{
"GNP": 8510700,
"IndepYear": 1776,
"Name": "United States",
"_id": "USA",
"demographics": {
"LifeExpectancy": 77.0999984741211,
"Population": 278357000
},
"geography": {
"Continent": "North America",
"Region": "North America",
"SurfaceArea": 9363520
},
"government": {
"GovernmentForm": "Federal Republic",
"HeadOfState": "Donald J Clinton"
}
}
]
1 document in set (0.00 sec)
Use find to search
All the old qualifiers
are available for limit,
sort, etc.
29
Examples
mysql-js> db.countryinfo.find("GNP > 500000")
mysql-js> db.countryinfo.find("GNP > 500000 and demographics.Population
< 100000000")
db.countryinfo.find("Name = :country").bind("country", "Italy")
What if you want only certain tags?
mysql-js> db.countryinfo.find("GNP > 5000000").fields(["GNP", "Name"])
[
{
"GNP": 8510700,
"Name": "United States"
}
]
1 document in set (0.00 sec)
mysql-js>
30
More Examples
db.countryinfo.find().limit(5)
db.countryinfo.find().sort(["IndepYear desc"]).limit(8)
db.countryinfo.find().sort(["IndepYear desc"]).limit(8).skip(1)
db.countryinfo.modify("_id = 'SEA'").
set("demographics", {LifeExpectancy: 78, Population: 28})
db.countryinfo.modify("Name = 'Sealand'").unset("GNP")
31
db.countryinfo.modify().
set("Airports", [])
This will add a new array field named
Airports
In all records
32
db.countryinfo.modify("Name = 'France'").arrayAppend("$.Airports", "ORY")
db.countryinfo.find("Name = 'France'")
[
{
"Airports": [
"ORY"
],
"GNP": 1424285,
"IndepYear": 843,
"Name": "France",
"_id": "FRA",
33
Remove examples
db.countryinfo.remove("_id = 'SEA'")
db.countryinfo.remove().limit(1)
db.countryinfo.remove().sort(["Name desc"]).limit(1)
34
Indexes
db.countryinfo.createIndex("pop").
field("demographics.Population", "INTEGER",
false).execute()
35
Indexes can be added -- this index is on population value
under the demographics key, of type INTEGER, no NULLs
allowed.
There is also a dropIndex
You can also
access
relational
tables!!
36
37
Seeing tables
mysql-js> db.getTables()
[
<Table:city>,
<Table:country>,
<Table:countrylanguage>
]
mysql-js>
Basic table operations
db.name.insert()
The insert() method inserts one or more records into the named table.
db.name.select()
The select() method returns some or all records in the named table.
db.name.update()
The update() method updates records in the named table.
db.name.delete()
The delete() method deletes one or more records from the named table.
38
Insert Example
db.city.insert("ID", "Name", "CountryCode", "District",
"Info").
values(null, "Olympia", "USA", "Washington",
'{"Population": 5000}')
39
SELECT Examples
mysql-js> db.city.select().limit(1)
+----+-------+-------------+----------+-------------------------+
| ID | Name | CountryCode | District | Info |
+----+-------+-------------+----------+-------------------------+
| 1 | Kabul | AFG | Kabol | {"Population": 1780000} |
+----+-------+-------------+----------+-------------------------+
1 row in set (0.00 sec)
mysql-js> db.city.select(["Name", "CountryCode"])
db.city.select(["Name", "CountryCode"]).where("Name like 'Z%'")
40
UPdate & delete
db.city.update().set("Name", "Beijing").where("Name = 'Peking'")
db.city.delete().where("Name = 'Olympia'")
41
Supported languages
JavaScript, Python and SQL *
● More on the way 42
THANKS!Any questions?
You can find me at @stoker or david.stokes@oracle.com
slideshare.net/davidmstokes
Elephantanddolphin.blogger.com https://legacy.joind.in/talk/view/19538
43

More Related Content

PDF
MySQL Replication Basics -Ohio Linux Fest 2016
PDF
MySQL Replication Update -- Zendcon 2016
PPTX
MySQL Utilities -- Cool Tools For You: PHP World Nov 16 2016
PDF
MariaDB 10.5 binary install (바이너리 설치)
PDF
MySQL Audit using Percona audit plugin and ELK
PDF
Introduction to MySQL InnoDB Cluster
PDF
Intro ProxySQL
PPT
Oracle12c Pluggable Database Hands On - TROUG 2014
MySQL Replication Basics -Ohio Linux Fest 2016
MySQL Replication Update -- Zendcon 2016
MySQL Utilities -- Cool Tools For You: PHP World Nov 16 2016
MariaDB 10.5 binary install (바이너리 설치)
MySQL Audit using Percona audit plugin and ELK
Introduction to MySQL InnoDB Cluster
Intro ProxySQL
Oracle12c Pluggable Database Hands On - TROUG 2014

What's hot (20)

ODP
MySQL 101 PHPTek 2017
PDF
MySQL Advanced Administrator 2021 - 네오클로바
PPTX
MySQL InnoDB Cluster 미리보기 (remote cluster test)
PDF
Oss4b - pxc introduction
PDF
Galera Replication Demystified: How Does It Work?
PDF
Introduction to ClustrixDB
PDF
Ohio Linux Fest -- MySQL's NoSQL
PDF
MySQL Group Replication
PDF
MySQL Multi-Source Replication for PL2016
PPTX
MySQL Replication Overview -- PHPTek 2016
PDF
Replication Troubleshooting in Classic VS GTID
PDF
Introduction to MariaDB MaxScale
PDF
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
DOC
Schema replication using oracle golden gate 12c
PDF
Multi thread slave_performance_on_opc
PDF
Plny12 galera-cluster-best-practices
PPT
Oracle Golden Gate
PPTX
Easy MySQL Replication Setup and Troubleshooting
PDF
Managing MariaDB Server operations with Percona Toolkit
PDF
監査ログをもっと身近に!〜統合監査のすすめ〜
MySQL 101 PHPTek 2017
MySQL Advanced Administrator 2021 - 네오클로바
MySQL InnoDB Cluster 미리보기 (remote cluster test)
Oss4b - pxc introduction
Galera Replication Demystified: How Does It Work?
Introduction to ClustrixDB
Ohio Linux Fest -- MySQL's NoSQL
MySQL Group Replication
MySQL Multi-Source Replication for PL2016
MySQL Replication Overview -- PHPTek 2016
Replication Troubleshooting in Classic VS GTID
Introduction to MariaDB MaxScale
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Schema replication using oracle golden gate 12c
Multi thread slave_performance_on_opc
Plny12 galera-cluster-best-practices
Oracle Golden Gate
Easy MySQL Replication Setup and Troubleshooting
Managing MariaDB Server operations with Percona Toolkit
監査ログをもっと身近に!〜統合監査のすすめ〜
Ad

Similar to MySQL as a Document Store (20)

PDF
MySQL Document Store -- SCaLE 17x Presentation
PDF
Open Source World June '21 -- JSON Within a Relational Database
PPTX
MySQL Without the SQL - Oh My! -> MySQL Document Store -- Confoo.CA 2019
PDF
MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...
PDF
MySQL Without the SQL -- Oh My!
PDF
Datacon LA - MySQL without the SQL - Oh my!
PDF
Json within a relational database
PDF
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
PPTX
Discover the Power of the NoSQL + SQL with MySQL
PPTX
Discover The Power of NoSQL + MySQL with MySQL
PPTX
MySQL Without the SQL -- Oh My! Longhorn PHP Conference
ODP
MySQL Without the MySQL -- Oh My!
PPTX
Making MySQL Agile-ish
PDF
20201106 hk-py con-mysql-shell
PDF
All Things Open 2016 -- Database Programming for Newbies
PPTX
Python And The MySQL X DevAPI - PyCaribbean 2019
PDF
MySQL Document Store
PDF
[SSA] 03.newsql database (2014.02.05)
PDF
MySQL without the SQL -- Cascadia PHP
PDF
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL Document Store -- SCaLE 17x Presentation
Open Source World June '21 -- JSON Within a Relational Database
MySQL Without the SQL - Oh My! -> MySQL Document Store -- Confoo.CA 2019
MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...
MySQL Without the SQL -- Oh My!
Datacon LA - MySQL without the SQL - Oh my!
Json within a relational database
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
Discover the Power of the NoSQL + SQL with MySQL
Discover The Power of NoSQL + MySQL with MySQL
MySQL Without the SQL -- Oh My! Longhorn PHP Conference
MySQL Without the MySQL -- Oh My!
Making MySQL Agile-ish
20201106 hk-py con-mysql-shell
All Things Open 2016 -- Database Programming for Newbies
Python And The MySQL X DevAPI - PyCaribbean 2019
MySQL Document Store
[SSA] 03.newsql database (2014.02.05)
MySQL without the SQL -- Cascadia PHP
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
Ad

More from Dave Stokes (20)

PDF
Valkey 101 - SCaLE 22x March 2025 Stokes.pdf
PPTX
Locking Down Your MySQL Database.pptx
PPTX
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
PDF
MySQL Indexes and Histograms - RMOUG Training Days 2022
PDF
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
PDF
Windowing Functions - Little Rock Tech fest 2019
PDF
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
PPTX
Develop PHP Applications with MySQL X DevAPI
PDF
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
PDF
The Proper Care and Feeding of MySQL Databases
PDF
MySQL 8 Server Optimization Swanseacon 2018
PDF
MySQL Without The SQL -- Oh My! PHP[Tek] June 2018
PDF
Presentation Skills for Open Source Folks
PPTX
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
PPTX
ConFoo MySQL Replication Evolution : From Simple to Group Replication
PDF
Advanced MySQL Query Optimizations
PPTX
PHP Database Programming Basics -- Northeast PHP
PPTX
MySQL Replication Evolution -- Confoo Montreal 2017
PDF
MySQL's JSON Data Type and Document Store
PDF
Five Database Mistakes and how to fix them -- Confoo Vancouver
Valkey 101 - SCaLE 22x March 2025 Stokes.pdf
Locking Down Your MySQL Database.pptx
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
MySQL Indexes and Histograms - RMOUG Training Days 2022
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
Windowing Functions - Little Rock Tech fest 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
Develop PHP Applications with MySQL X DevAPI
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
The Proper Care and Feeding of MySQL Databases
MySQL 8 Server Optimization Swanseacon 2018
MySQL Without The SQL -- Oh My! PHP[Tek] June 2018
Presentation Skills for Open Source Folks
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
ConFoo MySQL Replication Evolution : From Simple to Group Replication
Advanced MySQL Query Optimizations
PHP Database Programming Basics -- Northeast PHP
MySQL Replication Evolution -- Confoo Montreal 2017
MySQL's JSON Data Type and Document Store
Five Database Mistakes and how to fix them -- Confoo Vancouver

Recently uploaded (20)

PPTX
innovation process that make everything different.pptx
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPTX
Introduction to Information and Communication Technology
PPTX
Digital Literacy And Online Safety on internet
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PDF
Paper PDF World Game (s) Great Redesign.pdf
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PDF
Testing WebRTC applications at scale.pdf
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PPTX
artificial intelligence overview of it and more
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
innovation process that make everything different.pptx
SASE Traffic Flow - ZTNA Connector-1.pdf
presentation_pfe-universite-molay-seltan.pptx
Introduction to Information and Communication Technology
Digital Literacy And Online Safety on internet
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PptxGenJS_Demo_Chart_20250317130215833.pptx
Slides PPTX World Game (s) Eco Economic Epochs.pptx
Paper PDF World Game (s) Great Redesign.pdf
Design_with_Watersergyerge45hrbgre4top (1).ppt
Slides PDF The World Game (s) Eco Economic Epochs.pdf
international classification of diseases ICD-10 review PPT.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx
Testing WebRTC applications at scale.pdf
SAP Ariba Sourcing PPT for learning material
Unit-1 introduction to cyber security discuss about how to secure a system
artificial intelligence overview of it and more
522797556-Unit-2-Temperature-measurement-1-1.pptx
Cloud-Scale Log Monitoring _ Datadog.pdf

MySQL as a Document Store

  • 1. MySQl aS A dOCUMENTSTORE David.Stokes@Oracle.com @ Stoker slideshare.net/davidmstokes elelphantanddolphin.blogger.com
  • 2. "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, AND TIMING OF ANY FEATURES OR FUNCTIONALITY DESCRIBED FOR ORACLE'S PRODUCTS REMAINS AT THE SOLE DISCRETION OF ORACLE." Safe Harbor 2
  • 3. Hello!I am Dave Stokes, MySQL Community Manager David.Stokes@Oracle.com @Stoker Slides at Slideshare.net/davidmstokes Blogs: Opensourcedba.wordpress.com elephantanddolphin.blogger.com 3
  • 4. 1. Json & json dATATYPE MySQL 5.7 4
  • 5. “JSON JavaScript Object Notation is a lightweight data-interchange format. It is built on two structures: A collection of name/vaule pairs and and ordered list of values.” 5
  • 6. MySQL JSON data type × Schemaless data × UTF8MB4 × It is just like a INT, or REAL, or TIMESTAMP. You can store an entire document in a column of a row of a table. You could store JSON in a CHAR/TEXT field but it is not sexy and you end up using REGEXP --- Ughhh! 6
  • 7. Big Very few developers are learning SQL (Structured Query Language), Relational Theory, Sets, data normalization or other database related subjects! 7
  • 8. SQL Data is normalized and formats are rigorously enforced. JOIN and logical operators allow complex queries. Costly to change schema. The big contest of 2014 In case you missed it NoSQL Schemaless, no need for relational overhead, changes as fast as the data, easy to implement. 8
  • 9. MySQl offers both SQL & NoSQL RDMS Good old relational database InnoDB/Memcached This plug-in allows direct access to InnoDB or NDB storage engine data using the memcached protocol, up to 9x faster JSON Data is stored in JSON format. No schema needed. Functions supplied for SQL based access & manipulation 9
  • 10. But what if you wanted a database but do not know sql? 10
  • 12. 12 JSON Documents and Collections A JSON document is a data structure composed of field/value pairs stored within a collection. The values of fields often contain other documents, arrays, and lists of documents.
  • 13. CRUD Operations Create, Read, Update and Delete (CRUD) operations are the four basic operations that can be performed on a database Collection or Table 13
  • 14. X Protocol The X Protocol supports both CRUD and SQL operations, authentication via SASL, allows streaming (pipelining) of commands and is extensible on the protocol and the message layer. 14
  • 15. Mysqlsh has three modes js -- JavaScript py -- python SQL -- SQL (like old Mysql shell) 15
  • 16. 1. MySQL 5.17.12 or later 2. Install X plugin a. mysql> INSTALL PLUGINmysqlx SONAME “mysqlx.so”; OR b. mysqlsh -u user -h localhost --classic --dba enableXProtocol 3. Install mysqlsh * 16 Installation needs ● Note: Installation of MySQL Shell using the MySQL APT repository is only supported on Ubuntu 14.04 LTS (“Trusty Tahr”) and Ubuntu 15.10 (“Wily Werewolf”).
  • 17. mysqlsh $ mysqlsh --uri dstokes:hidave@localhost world_x --node mysqlx: [Warning] Using a password on the command line interface can be insecure. Creating a Node Session to dstokes@localhost:33060/world_x Default schema `world_x` accessible through db. Welcome to MySQL Shell 1.0.5 Development Preview Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help', 'h' or '?' for help, type 'quit' or 'q' to exit. Currently in JavaScript mode. Use sql to switch to SQL mode and execute queries. mysql-js> 17
  • 18. MySQLSh shell Sessions MySQL Shell is a unified interface to operate MySQL Server through scripting languages such as JavaScript or Python. To maintain compatibility with previous versions, SQL can also be executed in certain modes. A connection to a MySQL server is required. In MySQL Shell these connections are handled by a Session object. 18 XSession: Use this session type for new application development. It offers the best integration with MySQL Server, and therefore, it is used by default. SQL execution is not supported Node Session: Use this session type for SQL execution on a MySQL Server with the X Protocol enabled. SQL execution is available Classic Session Use this session type to interact with MySQL Servers that do not have the X Protocol enabled ● --node creates a Node Session. ● --classic creates a Classic Session. ● --x creates an XSession.
  • 19. session mysql-js> session <NodeSession:dstokes@localhost:33060/world_x> mysql-js> The above tells us we are a JavaScript mode Account is dstokes@localhost on port 33060 The Database/schema is world_x 19
  • 20. session mysql-js> session <NodeSession:dstokes@localhost:33060/world_x> mysql-js> The above tells us we are a JavaScript mode Account is dstokes@localhost on port 33060 The Database/schema is world_x 20
  • 21. WHICH DATABASE AND WHICH COLLECTION mysql-js> db <Schema:world_x> mysql-js> db.getCollections() [ <Collection:countryinfo> ] mysql-js> 21 <-- The Database ← the table
  • 22. Batch Mode Example 22 echo "SELECT * FROM world_x.city LIMIT 2;" | mysqlsh --json=pretty --sqlc --uri user@host { "executionTime": "0.00 sec", "info": "", "rows": [ { "ID": 1, "Name": "Kabul", "CountryCode": "AFG", "District": "Kabol", "Info": "{"Population": 1780000}" }, { "ID": 2, "Name": "Qandahar", "CountryCode": "AFG", "District": "Qandahar", "Info": "{"Population": 237500}" } ], "warningCount": 0, "warnings": [], "hasData": true, "affectedRowCount": 0, "autoIncrementValue": 0 } This is a batch mode example where a query is sent to mysqlsh in sql mode and the output is produced in ‘pretty’ JSON format.
  • 23. Interactive help mysql-js> var mySchema = session.getSchema('Zendcon') The schema Zendcon does not exist, do you want to create it? [y/N]: y mysql-js> mySchema <Schema:Zendcon> mysql-js> Note the interactive error correction when a non existent schema is accessed. 23
  • 24. db 24 $ mysqlsh --uri dstokes:hidave@localhost world_x Default schema `world_x` accessible through db. ... mysql-js> db <Schema:world_x> mysql-js> db.getCollections() [ <Collection:countryinfo> ] mysql-js> db.countryinfo.find().limit(1) [ { "GNP": 828, "IndepYear": null, "Name": "Aruba" …. db is a global variable assigned to the current active schema that you specified on the command line.
  • 25. Basic Operators 25 db.name.add() The add() method inserts one document or a list of documents into the named collection. db.name.find() The find() method returns some or all documents in the named collection. db.name.modify() The modify() method updates documents in the named collection. db.name.remove() The remove() method deletes one document or a list of documents from the named collection.
  • 26. Create a Collection named ‘Zendcon’ and list all collections 26 mysql-js> db.createCollection("Zendcon") <Collection:Zendcon> mysql-js> db.getCollections() [ <Collection:Zendcon>, <Collection:countryinfo> ] mysql-js>
  • 27. mysql-js>db.countryinfo.add( { GNP: .6, IndepYear: 1967, Name: "Sealand", _id: "SEA", demographics: { LifeExpectancy: 79, Population: 27 }, geography: { Continent: "Europe", Region: "British Islands", SurfaceArea: 193 }, government: { GovernmentForm: "Monarchy", HeadOfState: "Michael Bates" } } ) 27 use world_x So lets use the world_x sample database and add a new record!
  • 28. 28 mysql-js> db.countryinfo.find("_id = 'USA'") [ { "GNP": 8510700, "IndepYear": 1776, "Name": "United States", "_id": "USA", "demographics": { "LifeExpectancy": 77.0999984741211, "Population": 278357000 }, "geography": { "Continent": "North America", "Region": "North America", "SurfaceArea": 9363520 }, "government": { "GovernmentForm": "Federal Republic", "HeadOfState": "Donald J Clinton" } } ] 1 document in set (0.00 sec) Use find to search All the old qualifiers are available for limit, sort, etc.
  • 29. 29 Examples mysql-js> db.countryinfo.find("GNP > 500000") mysql-js> db.countryinfo.find("GNP > 500000 and demographics.Population < 100000000") db.countryinfo.find("Name = :country").bind("country", "Italy")
  • 30. What if you want only certain tags? mysql-js> db.countryinfo.find("GNP > 5000000").fields(["GNP", "Name"]) [ { "GNP": 8510700, "Name": "United States" } ] 1 document in set (0.00 sec) mysql-js> 30
  • 31. More Examples db.countryinfo.find().limit(5) db.countryinfo.find().sort(["IndepYear desc"]).limit(8) db.countryinfo.find().sort(["IndepYear desc"]).limit(8).skip(1) db.countryinfo.modify("_id = 'SEA'"). set("demographics", {LifeExpectancy: 78, Population: 28}) db.countryinfo.modify("Name = 'Sealand'").unset("GNP") 31
  • 32. db.countryinfo.modify(). set("Airports", []) This will add a new array field named Airports In all records 32
  • 33. db.countryinfo.modify("Name = 'France'").arrayAppend("$.Airports", "ORY") db.countryinfo.find("Name = 'France'") [ { "Airports": [ "ORY" ], "GNP": 1424285, "IndepYear": 843, "Name": "France", "_id": "FRA", 33
  • 34. Remove examples db.countryinfo.remove("_id = 'SEA'") db.countryinfo.remove().limit(1) db.countryinfo.remove().sort(["Name desc"]).limit(1) 34
  • 35. Indexes db.countryinfo.createIndex("pop"). field("demographics.Population", "INTEGER", false).execute() 35 Indexes can be added -- this index is on population value under the demographics key, of type INTEGER, no NULLs allowed. There is also a dropIndex
  • 38. Basic table operations db.name.insert() The insert() method inserts one or more records into the named table. db.name.select() The select() method returns some or all records in the named table. db.name.update() The update() method updates records in the named table. db.name.delete() The delete() method deletes one or more records from the named table. 38
  • 39. Insert Example db.city.insert("ID", "Name", "CountryCode", "District", "Info"). values(null, "Olympia", "USA", "Washington", '{"Population": 5000}') 39
  • 40. SELECT Examples mysql-js> db.city.select().limit(1) +----+-------+-------------+----------+-------------------------+ | ID | Name | CountryCode | District | Info | +----+-------+-------------+----------+-------------------------+ | 1 | Kabul | AFG | Kabol | {"Population": 1780000} | +----+-------+-------------+----------+-------------------------+ 1 row in set (0.00 sec) mysql-js> db.city.select(["Name", "CountryCode"]) db.city.select(["Name", "CountryCode"]).where("Name like 'Z%'") 40
  • 41. UPdate & delete db.city.update().set("Name", "Beijing").where("Name = 'Peking'") db.city.delete().where("Name = 'Olympia'") 41
  • 42. Supported languages JavaScript, Python and SQL * ● More on the way 42
  • 43. THANKS!Any questions? You can find me at @stoker or david.stokes@oracle.com slideshare.net/davidmstokes Elephantanddolphin.blogger.com https://legacy.joind.in/talk/view/19538 43