8.8.2 Requesting a Fabric Connection

The modules related to Fabric are under mysql.connector.fabric. Importing fabric provides access to everything needed to use Fabric:

import mysql.connector
from mysql.connector import fabric

Traditionally, a MySQL connection is set up using the mysql.connector.connect() method using the connection arguments described at Connector/Python Connection Arguments, and the connection is established immediately.

A request for a Fabric connection, by contrast, does not immediately connect. Instead, pass a fabric argument to the connect() call. This argument must be a dictionary. When Fabric connects to the MySQL server that it provides, it uses the connection arguments other than the fabric argument (except that the unix_socket connection argument is not supported).

To prepare a connection with Fabric, do this:

fabric_config = {
  'host': 'fabric.example.com',
}
fcnx = mysql.connector.connect(fabric=fabric_config, user='webuser',
                               password='webpass', database='employees')

If you prefer to pass a dictionary to connect(), do this:

config = {
  'fabric': {
    'host': 'fabric.example.com',
  },
  'user': 'webuser',
  'password': 'webpass',
  'database': 'employees',
}
fcnx = mysql.connector.connect(**config)

The fabric dictionary argument permits these values:

The username, password, report_errors, ssl_ca, ssl_cert and ssl_key options were added in Connector/Python 1.2.1. It is possible to establish an SSL connection using only the ssl_ca argument. The ssl_key and ssl_cert arguments are optional. However, when either is given, both must be given or an AttributeError is raised.

It is also possible to pass a Fabric() object instance as the fabric argument:

fabric_config = {
  'host': 'fabric.example.com',
}
fabinst = Fabric(**fabric_config)
fcnx = mysql.connector.connect(fabric=fabinst, user='webuser',
                               password='webpass', database='employees')

Or:

fabric_config = {
  'host': 'fabric.example.com',
}
fabinst = Fabric(**fabric_config)
config = {
  'fabric': fabinst,
  'user': 'webuser',
  'password': 'webpass',
  'database': 'employees',
}
fcnx = mysql.connector.connect(**config)

Once a Fabric() object is used, it is cached and reused.

Another (less preferred) way to establish a Fabric connection is pass configuration information to the MySQLFabricConnection class to create a connection with a Fabric node. This is similar to using the mysql.connector.connect() method or MySQLConnection() class with the addition of the required fabric argument:

config = {
  'fabric': {
    'host': 'fabric.example.com',
  },
  'user': 'webuser',
  'password': 'webpass',
}

fcnx = fabric.MySQLFabricConnection(**config)

Error Reporting

Connector/Python can report errors to Fabric that occur while accessing a MySQL instance. The information can be used to update the backing store and trigger a failover operation, provided that the instance is a primary server and Fabric has received a sufficient number of problem reports from different connectors.