+ All Categories
Home > Technology > Con4445 jesus

Con4445 jesus

Date post: 27-Jan-2015
Category:
Upload: paulo-jesus
View: 106 times
Download: 0 times
Share this document with a friend
Description:
 
Popular Tags:
50
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 1
Transcript
Page 1: Con4445 jesus

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

Page 2: Con4445 jesus

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

Developing withMySQL Connector/Python

Dr. Paulo Jesus

Software Developer

Page 3: Con4445 jesus

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

Program Agenda

Introduction of Connector/Python

Download and Installation

Coding

About Performance

Frameworks and Applications

Page 4: Con4445 jesus

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

Introduction

Page 5: Con4445 jesus

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

Introducing MySQL Connector/Python

MySQL Database Driver

Pure Python, no 3rd party dependencies

Python v2 and v3

For MySQL, by MySQL

Documentation

– http://dev.mysql.com/doc/connector-python/en/index.html

Page 6: Con4445 jesus

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

Pure Python

Written entirely using Python

No compiling against MySQL client libraries

Only Standard Library used

Developed for Python v2 and v3

PEP-249 compliant

Page 7: Con4445 jesus

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

Current Connector/Python Versions

v1.0

– GA since September 2012

– v1.0.12 released August 2013

v1.1

– Alpha since June 2013

– Some new features:

Prepared Statements and Pooling

Django backend included

Page 8: Con4445 jesus

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

Python Support

Overview of supported Python version

Connector/Python 2.4 2.5 2.6 2.7 3.0 3.1 3.2 3.3

v1.0 ? ?

v1.1

Page 9: Con4445 jesus

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

MySQL Server Support

MySQL v5.5 and higher

Known to work on earlier versions

– No old password hashing support

– MySQL v4.0 and older will not work

Page 10: Con4445 jesus

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

Download & Installation

Page 11: Con4445 jesus

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

Dependencies

For Windows

– Install Python from http://python.org

Other systems

– should have Python (2.6 or greater)

Python!

Page 12: Con4445 jesus

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

Download

MySQL Website

– http://dev.mysql.com/downloads/connector/python

Available as

– TAR/ZIP archive

– RPM and Debian package

– Windows Installer (MSI)

Launchpad

– https://launchpad.net/myconnpy

Page 13: Con4445 jesus

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

Download

Example file names:

– mysql-connector-python-1.1.1.zip

– mysql-connector-python-1.1.1-py2.7.msi

– mysql-connector-python-1.1.1.el6.noarch.rpm

Architecture independent

Page 14: Con4445 jesus

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

Installation: Using TAR/ZIP

Most common way

Works anywhere

– .. where Python is installed

For both Python v2 & v3

Running setup.py

Page 15: Con4445 jesus

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

Installation: Universal

shell> unzip mysql-connector-1.0.12.zip

shell> cd mysql-connector-1.0.12

shell> python setup.py install

# Or any other Python binary

shell> /opt/python/3.3/bin/python3 setup.py install

Using setup.py

Page 16: Con4445 jesus

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

Installation using RPM and Debian package

# On Oracle Linux

shell> rpm -i mysql-connector-python-1.1.1.el6.noarch.rpm

# On Ubuntu

shell> dpkg -i \

mysql-connector-python_1.0.12-1ubuntu12.04_all.deb

Page 17: Con4445 jesus

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

Installation: using PyPI

shell> easy_install mysql-connector-python

or

shell> pip install mysql-connector-python

Python Package Index

Page 18: Con4445 jesus

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

Install Inside Virtual Environment

# If not installed yet

shell> pip install virtualenv

shell> virtualenv ENV

shell> source ENV/bin/activate

shell> pip install mysql-connector-python

Useful for testing and new development

Page 19: Con4445 jesus

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

Coding

Page 20: Con4445 jesus

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

Classes and Objects

import mysql.connector

mysql.connector.connect()

– accepts numerous arguments

– returns a MySQLConnection

mysql.connector module

Page 21: Con4445 jesus

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

Classes and Objects

MySQLConnection

– connects with MySQL

– retrieve server information

– reconfigure and reconnect

– commit/rollback transactions

– create MySQLCursor objects

MySQLConnection

Page 22: Con4445 jesus

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

Classes and Objects

MySQLCursor

– fetch & manipulate data

– transactions

– different flavors (buffered, raw)

MySQLCursor

Page 23: Con4445 jesus

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

Connecting with MySQL

import mysql.connector

cnx = mysql.connector.connect(user='root',

host='prod.example.com')

print(cnx.connection_id)

cnx.close()

Basic connection

Page 24: Con4445 jesus

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

Connecting with MySQL

CONFIG = {

'user': 'root',

'host': '::1',

}

cnx = mysql.connector.connect(**CONFIG)

print(cnx.connection_id)

cnx.close()

Use a dict to hold configuration & use IPv6

Page 25: Con4445 jesus

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

Reconnecting with MySQL

cnx = mysql.connector.connect(**CONFIG)

cnx.config(user='joe', password='mysecret')

cnx.reconnect()

cnx.close()

Reconfigure and reconnect

Page 26: Con4445 jesus

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

Cursor methods

execute(query, data)

– data is list of values

fetchone()

– returns row as tuple

fetchmany(20)

– Get the next 20 rows (or less) as list of tuples

fetchall()

– Get all, or remaining, as list of tuples

MySQLCursor class

Page 27: Con4445 jesus

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

Fetching Data

cur = cnx.cursor()

cur.execute("SELECT NOW()")

result= cur.fetchone()[0]

print(result)

cur.close()

# Result

(datetime.datetime(2013, 9, 2, 10, 41, 58))

Fetch a row

Page 28: Con4445 jesus

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

Fetching Data

query = ("SELECT id, last_name FROM employees ""WHERE id > %s AND first_name LIKE %s"

)

cur.execute(query, (500, 'J%'))

for row in cur:

print("{id} {name}".format(id=row[0], name=row[1]))

Going through results using iterator

Page 29: Con4445 jesus

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

Manipulating data

insert_stmt = (

"INSERT INTO employees (first_name, last_name) "

"VALUES (%s, %s)

)

cur = cnx.cursor()

cur.execute(insert_stmt, ('John', 'Doe'))

print("ID of new employee: {id}".format(id=cur.lastrowid))

cnx.commit()

Inserting, updating and deleting

Page 30: Con4445 jesus

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

Change Many Records

data = [

('John', 'Doe'),

('Jane', 'Doe'),

]

cur.executemany(insert_stmt, data)

cnx.commit()

MySQLCursor.executemany()

Page 31: Con4445 jesus

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

Execute Multiple Statements

Separated by semicolon ;

Could return one or more results

Example:

– "SELECT NOW(); SELECT CURRENT_USER()"

Needs multi argument set to True

– Raises error if not

– cur.execute(query, multi=True)

Page 32: Con4445 jesus

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

Executing Multiple Statements

cur = cnx.cursor()

queries = "SELECT NOW(); SELECT CURRENT_USER()"

for mcur in cur.execute(query, multi=True):

if mcur.with_rows:

print(mcur.fetchall())

Example

Page 33: Con4445 jesus

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

Retrying a Transaction (1/2)

tries = 10

while tries:

tries -= 1

try:

if not cnx.is_connected():

cnx.reconnect()

cur = cnx.cursor()

cur.execute(insert, ('Joe',))

Using reconnect()

Page 34: Con4445 jesus

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

Retrying a Transaction (2/2)

cur.execute(insert, ('Joe',))

except mysql.connector.Error as err:

if not tries:

raise

else:

cnx.commit()

break

Using reconnect()

Page 35: Con4445 jesus

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

Some Performance Tips

Page 36: Con4445 jesus

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

Push Work to Server

Use MySQL functions

– Date/Time calculation,

– String operations, etc..

Sorting using ORDER BY in queries

Select what you need

– Don't SELECT *

General tips

Page 37: Con4445 jesus

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

Push Work to Server: Example

query =

"SELECT DATEDIFF( end, start) FROM events WHERE id = %s"

cur.execute(query, (1234,))

event_length = cur.fetchone()[0]

Date calculation done by MySQL server

Page 38: Con4445 jesus

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

Raw Data

Use a raw cursor

– cnx.cursor(raw=True)

No MySQL to Python conversion

– data are all strings

It is faster in most cases

– dumping to CSV or create XML

– handle conversion elsewhere

Page 39: Con4445 jesus

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

Bulk Load Using CSV

Use LOAD DATA LOCAL INFILE

Server configuration has to allow it

– local_infile=1

Client flag has to be set

Page 40: Con4445 jesus

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

Using LOAD DATA LOCAL INFILE

CONFIG = {

'user': 'root', 'host': 'fk.example.com','client_flags': [mysql.connector.ClientFlag.LOCAL_FILES]

}

sql = "LOAD DATA LOCAL INFILE %s INTO TABLE employees"

cur.execute(sql, ('employees.csv',))

Page 41: Con4445 jesus

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

Frameworks and Applications

Page 42: Con4445 jesus

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

MySQL Projects

MySQL Utilities

– Command line utilities

– Managing, dumping data, failover, ..

MySQL Fabric

– Data distribution

– High-availability for groups of MySQL servers

Both pure Python & no 3rd party modules

Depending on Connector/Python

Page 43: Con4445 jesus

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

SQLAlchemy

SQL Toolkit and ORM (Object Relational Mapper)

Pythonic database access

Used by lots of other (web) frameworks

Connector/Python dialect included

– Since SQLAlchemy 0.6

Page 44: Con4445 jesus

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

SQLAlchemy: Usage

from sqlalchemy import create_engine

DB_URI =

"mysql+mysqlconnector://{user}:{pwd}@{host}:{port}/{db}"

engine = create_engine(DB_URI.format(

user='joe', pwd='mysecret', db='employees',

host='127.0.0.1', port=3306)

)

Connect String

Page 45: Con4445 jesus

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

Django

Web framework

ORM and database API

No support for Connector/Python

But Connector/Python 1.1.1 supports Django

– Support features as microsecond precision

– Works with Django 1.4 and 1.5

– Python 3 support

Page 46: Con4445 jesus

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

Django: Configuration

# settings.py

DATABASES = {

'default': {

'ENGINE': 'mysql.connector.django',

}

}

Using Connector/Python backend

Page 47: Con4445 jesus

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

Q&A

Page 48: Con4445 jesus

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

Q&A

Documentation & Examples

– http://dev.mysql.com/doc/connector-python/en/index.html

Download

– http://dev.mysql.com/downloads/connector/python/#downloads

Blog

– http://geert.vanderkelen.org

Feedback & Forum

– http://forums.mysql.com/list.php?50

– http://bugs.mysql.com/

More Information

Page 49: Con4445 jesus

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

Graphic Section Divider

Page 50: Con4445 jesus

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


Recommended