Skip to navigation

DB Cluster Utils Server API

The db_server class is used to manage individual servers.  When an instance of db_server is created, the control node connects to one of the nodes in the cluster specified by its IP address. The connection is stored as a variable in the db_server class, and it's used to execute SQL queries and commands.

Attributes

conn - database connection object
curs - database cursor object
ip_addr - the IP address of the server

 

Methods

__init__ (self, host, rt_password)

Create a server object and connect to a physical server

Arguments

  • host - the IP address of the server to connect to
  • rt_password - the MySQL root user password

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (host, cluster.root_password)


__del__ (self)

Close the connection to the database.

 

init_server (self, db_name, usr_pass, slv_pass)

Initialize a MySQL server as either a master or slave.

Arguments

  • db_name - database name
  • usr_pass - user password
  • slv_pass - slave user password

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (host, cluster.root_password)
server.init_server ("some_db", "usrpword", "slvpword")

 

create_db (self, db_name)

Create a database.

Argument

  • db_name - database name

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (host, cluster.root_password)

server.create_db (db_name)

 

create_users (self, user_passwd, slave_passwd)

Create users for the database.  Each server has two users, db_user, which is for general data access, and slave_user, which is used for replication.

Arguments

  • user_passwd - the password for the database user
  • slave_passwd - the replication user password

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (host, cluster.root_password)
server.create_users (usr_pass, slv_pass)

 

create_user (self, user_name, pass_word)

Create a database user.

Arguments

  • user_name - user name
  • pass_word - pass word

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (host, cluster.root_password)
server.create_user ('slave_user', slave_passwd)

 

wipe (self, db_name)

Remove the database and users.

Argument

  • db_name - database name

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (slave, self.root_password)
server.stop_slave ()
server.wipe (db_name)
            

 

drop_user (self, username)

Delete a user from this server.

Argument

  • usesrname - user name to be dropped

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (slave, self.root_password)
server.drop_user (user_name)

 

drop_database (self, db_name)

Delete a datbase from this server.

Argument

  • db_name - database name

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (slave, self.root_password)
server.drop_database (db_name)

 

get_master_status (self)

Get the name of the log file and the log position of a server.

Return the name of the log file and the log position of a server.

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (ip_address, root_password)
server.init_server (cluster.database_name, cluster.db_user_pw, cluster.slave_user_pw)
mysqlbin, binlog = server.get_master_status ()

 

set_as_master (self, slave_list, slave_password)

Make a host a master.

Arguments

  • slave_list - a list of IP addresses of slave servers
  • slave_password - password for the replication user

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (ip_address, root_password)
server.set_as_master (cluster.slave_ip_list, cluster.slave_user_pw)

 

grant_replication (self, user_name, ip, password)

Grant replication privileges on the master to a slave.

Arguments

  • user_name - the name of the user that will be granted replication privileges
  • ip - the IP address of the host that will be granted replication privileges
  • password - the password of the replication user

Example:

import db_cluster_utils

master_server = db_cluster_utils.db_server (host, cluster.root_password)
master_server.grant_replication ('slave_user', host, cluster.slave_user_pw)

 

demote_master (self, slave_list, slave_password)

Make a host a slave.

Arguments

  • slave_list - a list of IP addresses of slave servers
  • slave_password - password for the replication user

Example:

import db_cluster_utils

master_server = db_cluster_utils.db_server (cluster.master_ip, cluster.root_password)
master_server.demote_master (cluster.slave_ip_list, cluster.slave_user_pw)

 

revoke_replication (self, user_name, ip)

Revoke slave replication privileges on the master.

Arguments

  • user_name - the user name of the replication user
  • ip - the IP address of the the host whose replication privileges are being revoked

Example:

import db_cluster_utils

master_server = db_cluster_utils.db_server (host, cluster.root_password)
master_server.revoke_replication ('slave_user', host)

 

set_read_only (self)

Make a database server read only.

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (host, cluster.root_password)
server.set_read_only()

 

clear_read_only (self)

Clear read only status from database.

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (host, cluster.root_password)
server.clear_read_only()

 

use_master (self, master_ip_addr, slave_user_pword, mysqlbin_str, binlog_str)

Tell a slave which server to use as a master.

Arguments

  • master_ip_addr - the IP address of the master server
  • slave_user_pword - the password of the replication user
  • mysqlbin_str - the name of the MySQL log file on the master
  • binlog_str - the position of the master's log file

Example:

import db_cluster_utils

slave_server = db_cluster_utils.db_server (host, cluster.root_password)
slave_server.use_master (cluster.master_ip, cluster.slave_user_pw, cluster.master_mysqlbin, cluster.master_binlog)

 

start_slave (self)

Start the slave thread.

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (host, root_password)
server.start_slave ()

 

stop_slave (self)

Stop the slave thread.

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (host, root_password)
server.stop_slave ()

 

reset_slave (self)

Reset the slave.

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (host, cluster.root_password)
server.reset_slave ()

 

import_db (self, file_name, db_name)

Import data from a file.

Arguments

  • file_name - the name of the file to import
  • db_name - the name of the database to import data into.

Example:

import db_cluster_utils

server = db_cluster_utils.db_server (host, cluster.root_password)
server.import_db(dump_file, self.database_name)

Share this page:

comments powered by Disqus