"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
“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
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.
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.
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
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
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