9951 explained code solutions for 126 technologies


python-mysqlHow can I set up MySQL replication using Python?


MySQL replication using Python can be set up by using the MySQL Connector/Python library. This library provides access to the MySQL server and allows for the execution of SQL statements.

The following example code block uses the MySQL Connector/Python library to create a replication slave connection and set up the replication:

import mysql.connector

# Create connection to the master database
master_cnx = mysql.connector.connect(user='user', password='password',
                                    host='master-host', database='master-db')

# Create connection to the slave database
slave_cnx = mysql.connector.connect(user='user', password='password',
                                    host='slave-host', database='slave-db')

# Set up replication on the slave
slave_cursor = slave_cnx.cursor()
slave_cursor.execute("CHANGE MASTER TO MASTER_HOST='master-host', "
                     "MASTER_USER='user', MASTER_PASSWORD='password'")
slave_cursor.execute("START SLAVE")

This code will set up replication on the slave database. The master_cnx and slave_cnx variables are used to store the connections to the master and slave databases respectively. The CHANGE MASTER TO command is then used to configure the replication on the slave and the START SLAVE command is used to start the replication.

The following list explains the parts of the code:

  1. import mysql.connector - Imports the MySQL Connector/Python library
  2. master_cnx = mysql.connector.connect() - Establishes a connection to the master database
  3. slave_cnx = mysql.connector.connect() - Establishes a connection to the slave database
  4. slave_cursor.execute("CHANGE MASTER TO") - Configures the replication on the slave
  5. slave_cursor.execute("START SLAVE") - Starts the replication on the slave

Helpful links

Edit this code on GitHub