MySQL Utilities 1.4 Manual  /  ...  /  Requesting a Fabric Connection

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:

  • host: The host to connect to (required).

  • port: The TCP/IP port number to connect to on the specified host (optional; default 32274).

  • username: The user name of the account to use (optional).

  • password: The password of the account to use (optional).

  • connect_attempts: The number of connection attempts to make before giving up (optional; default 3).

  • connect_delay: The delay in seconds between attempts (optional; default 1).

  • report_errors: Whether to report errors to Fabric while accessing a MySQL instance. (optional; default False).

  • ssl_ca: The file containing the SSL certificate authority (optional).

  • ssl_cert: The file containing the SSL certificate file (optional).

  • ssl_key: The file containing the SSL key (optional).

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.

  • The fabric dictionary argument to the connect() method accepts a report_errors value. Its default value is False; pass a value of True to enable error reporting to Fabric.

  • To define which errors to report, use the extra_failure_report() function:

    from mysql.connector.fabric import extra_failure_report
    extra_failure_report([error_code_0, error_code_1, ...])
    

User Comments
Sign Up Login You must be logged in to post a comment.