26
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Creating Python application with MySQL High Availability Database Service Ivan Ma [email protected] 2016-10-29 Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

20161029 py con-mysq-lv3

  • Upload
    ivan-ma

  • View
    77

  • Download
    0

Embed Size (px)

Citation preview

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Creating Python application with MySQL High Availability Database Service Ivan Ma [email protected] 2016-10-29

Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Safe Harbor Statement

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.

2

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Program Agenda

3

MySQL High Availability Intro

Python – Connecting to Database

Making Query to Database

Python – Failover (HA) connection to Database

Flask – Web App Sample

1

2

3

4

5

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Overview

• MySQL as Database – Highly Available Configuration and Setup

• Python – The Programming Language

– Using Framework (e.g. FLASK )

– Database Connection

– Query

– Row Result Processing

– Error Handling

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Today • Introducing – MySQL Group Replication – MySQL Replication – MySQL Router – MySQL InnoDB Cluster

• MySQL Connector/Python – Connecting to MySQL – SELECT statements – Looping Result – Executing Stored Procedure – Error Handling in Python with MySQL – multi-host connection with MySQL

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

A Quick Walk thru of MySQL HA

• MySQL High Availability – Redundancy is the Key

• MySQL Replication

• MySQL Semi-Sync Replication

• MySQL Group Replication

• Shared Storage with OS Clustering

• MySQL Cluster

• Application Connection to MySQL

– Connector / Driver • Connector / Python

• Connector / Java

• Connector / .NET, PHP, etc…

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

A Quick Walk thru of MySQL HA

• MySQL High Availability – Redundancy is the Key

• MySQL Replication

• MySQL Semi-Sync Replication

• MySQL Group Replication (RC)

• Shared Storage with OS Clustering – MySQL service

• MySQL Cluster

• Application Connection to MySQL

– Connector / Driver • Connector / Python

• Connector / Java

• Connector / .NET, PHP, etc…

MySQL Router

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

MySQL Group Replication (Release Candidate) Natively distributed and highly available replica sets

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

MySQL Group Replication: What Does It Provide?

• A highly available distributed MySQL database service – Removes the need for manually handling server fail-over

– Provides distributed fault tolerance

– Enables Active/Active update anywhere setups

– Automates reconfiguration (adding/removing nodes, crashes, failures)

– Automatically detects and handles conflicts

9

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

MySQL Server: Core

• Server calls into the plugin through a generic interface – Most server internals are hidden from the plugin

• Plugin interacts with the server through a generic interface – Replication plugin determines the fate of the commit

operation through a well defined server interface

– The plugin makes use of the relay log infrastructure to inject changes in the receiving server

10

GCS API

Replication Plugin

Plugin API

MySQL Server

Group Com. Engine

GCS API

Replication Plugin

Plugin API

MySQL Server

Group Com. Engine

Network

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

MyQL Connector / Python

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

MySQL Connector / Python

• To download the Connetor/Python, – https://dev.mysql.com/downloads/connector/python/

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Connecting MySQL

• import mysql.connector

• import sys

• _pass = ''

• if len(sys.argv) > 1 :

• _pass = sys.argv[1]

• cnx = mysql.connector.connect(user='root', password=_pass, host='127.0.0.1', port=3306, database='mysql')

• cnx.close()

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 14

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

“connection” – MySQLConnection Class • from mysql.connector import (connection)

• import sys

• _pass='';

• if len(sys.argv) > 1 :

• _pass=sys.argv[1]

• cnx = connection.MySQLConnection(user='root', password=_pass, host='127.0.0.1', port=3306, database='mysql')

• cnx.close()

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Connection Pooling

16

Implicit

Explicit

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Connecting MySQL using Dictionary config

• import mysql.connector

• import sys

• _pass='';

• if len(sys.argv) > 1 :

• _pass=sys.argv[1]

• config = { 'user': 'root', 'password': '', 'host': '127.0.0.1', 'database': 'mysql',

• 'raise_on_warnings': True, }

• cnx = mysql.connector.connect(**config)

• cnx.close()

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Error Handling in Connection (try …. except…finally) • try:

• cnx = mysql.connector.connect(user='root', host='127.0.0.1', port=3306,

• password=_pass,

• database=_db)

• print("connect success")

• except mysql.connector.Error as err:

• if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:

• print("Something is wrong with your user name or password")

• elif err.errno == errorcode.ER_BAD_DB_ERROR:

• print("Database does not exist")

• else:

• print(err)

• finally:

• print ("Finally - close connection")

• cnx.close()

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

SELECT • cnx = mysql.connector.connect(**config)

• operation = “select ……. from …..; select …. from ….”

• cursor = cnx.cursor()

• try:

• ……

• except mysql.connector.Error as err:

• print(err.msg)

• cursor.close()

• cnx.close()

19

for result in cursor.execute(operation, multi=True): if result.with_rows: print("rows producted by statement '{}':".format(result.statement)) row = cursor.fetchone() while row: print(row) row = cursor.fetchone() else: print("Number of rows affeted by statement '{}':{}".format(result.statement, result.rowcount))

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Failover Connection Property • operation = 'SELECT @@hostname, @@port, employees.* from employees limit 10‘

• try:

• for result in cursor.execute(operation, multi=True):

• if result.with_rows:

• print("rows producted by statement '{}':".format(result.statement))

• row = cursor.fetchone()

• while row:

• print(row)

• row = cursor.fetchone()

• else:

• print("Number of rows affeted by statement '{}':{}".format(result.statement, result.rowcount))

• except mysql.connector.Error as err:

• print(err.msg)

• cursor.close()

• cnx.close()

20

config = { 'raise_on_warnings': True, 'failover' : [ { 'user': 'root‘, 'password': '‘, 'host': '127.0.0.1‘, 'port': 3306, 'database': 'employees', }, { 'user': 'root‘, 'password': '‘, 'host': '127.0.0.1‘, 'port': 3306, 'database': 'employees',} ] } cnx = mysql.connector.connect(**config)

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Program Agenda

21

MySQL High Availability Intro

Python – Connecting to Database

Making Query to Database

Python – Failover (HA) connection to Database

Flask – Web App Sample

1

2

3

4

5

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 22

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Home Page with Flask • from flask import Flask, render_template, request, json

• import mysql.connector

• from mysql.connector import errorcode

• app = Flask(__name__)

• @app.route("/")

• @app.route("/main")

• def main():

• return render_template('03-index.html')

23

config = { 'user': 'root', 'password': '', 'host': '127.0.0.1', 'port': 3316, 'database': 'pycon2016hk', 'raise_on_warnings': True, }

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

Register – Calling Stored Procedure • @app.route("/Register", methods=["POST","GET"])

• def register():

• try:

• _name = request.form['inputName']

• _password = request.form['inputPassword']

• # validate the values

• if _name and _password:

• else:

• return '<p>Please Enter the required fields!!</p>'

• except Exception as e:

• return '<p><span>DB Error' + str(e) +'</spand></p>'

• finally:

******************************************

24

conn = mysql.connector.connect(**config) cursor = conn.cursor() cursor.callproc("sp_user", [ _name, _password]) if len(list(cursor.stored_results())) is 0: conn.commit() return 'Message:User created successfully!‘ else: data = [] for result in cursor.stored_results(): data.append(str(result.fetchall())) return '<p><span>Error:' + str(data) + '</span></p>'

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |

THANK YOU!

25