Python MySQLdb.OperationalError() Examples

The following are code examples for showing how to use MySQLdb.OperationalError(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: PythonClassBook   Author: PythonClassRoom   File: DBSetup.py    GNU General Public License v3.0 7 votes vote down vote up
def buildDatabase( databaseName ):

   username = raw_input( "Enter MySQL user name: " )
   password = getpass.getpass( "Enter user password: " )

   print "Creating database %s:" % databaseName

   # retrieve database description from file
   print "\tRetrieving database definition:",

   try:
      databaseDefinition = retrieveDatabaseDefinition( databaseName )
   except TypeError:
      sys.exit( "ERROR\nThe database definition in %s.def is invalid" % databaseName )
   else:
      print "DONE"

   # get a cursor for MySQL
   print "\tConnecting to MySQL:",
   try:
      cursor = MySQLdb.connect( user = username, passwd = password ).cursor()
   except MySQLdb.OperationalError, error:
      sys.exit( "ERROR\nCould not connect to MySQL (%s)" % error ) 
Example 2
Project: mysql_utils   Author: pinterest   File: mysql_lib.py    GNU General Public License v2.0 6 votes vote down vote up
def setup_audit_plugin(instance):
    """ Install the audit log plugin.  Similarly to semisync, we may
        or may not use it, but we need to make sure that it's available.
        Audit plugin is available on 5.5 and above, which are all the
        versions we support.

        Args:
        instance - A hostaddr object
    """
    return 
    # this is intentional; we may re-enable the audit log at some 
    # later date, but for now, we just exit.

    conn = connect_mysql(instance)
    cursor = conn.cursor()

    try:
        cursor.execute("INSTALL PLUGIN audit_log SONAME 'audit_log.so'")
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        # plugin already loaded, nothing to do.
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise
    conn.close() 
Example 3
Project: dataops   Author: xiaomatech   File: pyxbackup.py    Apache License 2.0 6 votes vote down vote up
def _purge_bitmaps_to(lsn):
    _say("Purging bitmap files to LSN: %s" % lsn)

    if not db_connect():
        _error("Failed to connect to server, unable to purge bitmaps.")
        _exit_code(XB_EXIT_BITMAP_PURGE_FAIL)
        return False

    try:
        cur = xb_mysqldb.cursor(MySQLdb.cursors.DictCursor)
        cur.execute("PURGE CHANGED_PAGE_BITMAPS BEFORE %s" % lsn)
    except MySQLdb.OperationalError, e:
        _error(
            "Got MySQL error %d, \"%s\" at execute" % (e.args[0], e.args[1]))
        _error("Failed to purge bitmaps!")
        _exit_code(XB_EXIT_BITMAP_PURGE_FAIL)
        return False 
Example 4
Project: sqlibrist   Author: condograde   File: engines.py    MIT License 6 votes vote down vote up
def apply_migration(self, name, statements, fake=False):
        import MySQLdb
        connection = self.get_connection()
        cursor = connection.cursor()

        try:
            if not fake and statements.strip():
                cursor.execute(statements)
        except (MySQLdb.OperationalError, MySQLdb.ProgrammingError) as e:
            print('\n'.join(map(str, e.args)))
            from sqlibrist.helpers import ApplyMigrationFailed

            raise ApplyMigrationFailed
        else:
            cursor.execute('INSERT INTO sqlibrist_migrations '
                           '(migration) VALUES (%s);',
                           [name.split('/')[-1]]) 
Example 5
Project: sqlibrist   Author: condograde   File: engines.py    MIT License 6 votes vote down vote up
def unapply_migration(self, name, statements, fake=False):
        import MySQLdb
        connection = self.get_connection()
        cursor = connection.cursor()

        try:
            if not fake:
                cursor.execute(statements)
        except (MySQLdb.OperationalError, MySQLdb.ProgrammingError) as e:
            print('\n'.join(map(str, e.args)))
            from sqlibrist.helpers import ApplyMigrationFailed

            raise ApplyMigrationFailed
        else:
            cursor.execute('DELETE FROM sqlibrist_migrations '
                           'WHERE migration = (%s); ', [name]) 
Example 6
Project: pykit   Author: bsc-s2   File: mysqlconnpool.py    MIT License 6 votes vote down vote up
def query(self, sql, use_dict=True, retry=0):

        if retry < 0:
            retry = 0

        retry = int(retry)

        # the first attempt does not count as 'retry'
        for i in range(retry + 1):

            try:
                with self() as conn:
                    return conn_query(conn, sql, use_dict=use_dict)

            except MySQLdb.OperationalError as e:
                if len(e.args) > 0 and e[0] in retriable_err:
                    logger.info(
                        repr(e) + " conn_query error {sql}".format(sql=sql))
                    continue
                else:
                    raise
        else:
            raise 
Example 7
Project: ops   Author: xiaomatech   File: mysqlops.py    MIT License 6 votes vote down vote up
def get_master_mysql_major_version(instance):
    """ Given an instance, determine the mysql major version for the master
        of the replica set.

    Args:
    instance - a hostaddr object

    Returns - A string similar to '5.5' or '5.6'
   """
    zk = MysqlZookeeper()
    master = zk.get_mysql_instance_from_replica_set(
        instance.get_zk_replica_set()[0], repl_type=REPLICA_ROLE_MASTER)
    try:
        mysql_version = get_global_variables(master)['version'][:3]
    except _mysql_exceptions.OperationalError:
        raise Exception('Could not connect to master server {instance} in '
                        'order to determine MySQL version to launch with. '
                        'Perhaps run this script from there? This is likely '
                        'due to firewall rules.'
                        ''.format(instance=instance.hostname))
    return mysql_version 
Example 8
Project: PythonClassBook   Author: PythonClassRoom   File: fig17_30.py    GNU General Public License v3.0 5 votes vote down vote up
def addAddress( self ):
      """Add address record to database"""

      if self.entries[ "LAST_NAME" ].get() != "" and \
         self.entries[ "FIRST_NAME"].get() != "":

         # create INSERT query command
         query = """INSERT INTO addresses (  
                 FIRST_NAME, LAST_NAME, ADDRESS, CITY,
                 STATE_PROVINCE, POSTAL_CODE, COUNTRY,
                 EMAIL_ADDRESS, HOME_PHONE, FAX_NUMBER
                 ) VALUES (""" + \
                 "'%s', " * 10 % \
                 ( self.entries[ "FIRST_NAME" ].get(),
                   self.entries[ "LAST_NAME" ].get(),
                   self.entries[ "ADDRESS" ].get(),
                   self.entries[ "CITY" ].get(),
                   self.entries[ "STATE_PROVINCE" ].get(),
                   self.entries[ "POSTAL_CODE" ].get(),
                   self.entries[ "COUNTRY" ].get(),
                   self.entries[ "EMAIL_ADDRESS" ].get(),
                   self.entries[ "HOME_PHONE" ].get(),
                   self.entries[ "FAX_NUMBER" ].get() )
         query = query[ :-2 ] + ")"

         # open connection, retrieve cursor and execute query
         try:      
            connection = MySQLdb.connect( db = "AddressBook" )
            cursor = connection.cursor()
            cursor.execute( query ) 
         except MySQLdb.OperationalError, message:
            errorMessage = "Error %d:\n%s" % \
               ( message[ 0 ], message[ 1 ] )
            showerror( "Error", errorMessage )
         else:
            cursor.close()
            connection.close()
	    self.clearContents() 
Example 9
Project: PythonClassBook   Author: PythonClassRoom   File: fig17_30.py    GNU General Public License v3.0 5 votes vote down vote up
def findAddress( self ):
      """Query database for address record and display results"""

      if self.entries[ "LAST_NAME" ].get() != "":

         # create SELECT query
         query = "SELECT * FROM addresses " + \
                 "WHERE LAST_NAME = '" + \
                 self.entries[ "LAST_NAME" ].get() + "'"

         # open connection, retrieve cursor and execute query
         try:      
            connection = MySQLdb.connect( db = "AddressBook" )
            cursor = connection.cursor()
            cursor.execute( query )
         except MySQLdb.OperationalError, message:
            errorMessage = "Error %d:\n%s" % \
               ( message[ 0 ], message[ 1 ] )
            showerror( "Error", errorMessage )
            self.clearContents()
         else:   # process results
            results = cursor.fetchall()
            fields = cursor.description

            if not results:   # no results for this person
               showinfo( "Not found", "Nonexistent record" )
            else:             # display person's info. in GUI
               self.clearContents()

               # display results
               for i in range( len( fields ) ):

                  if fields[ i ][ 0 ] == "ID":
                     self.IDEntry.set( str( results[ 0 ][ i ] ) )
                  else:
                     self.entries[ fields[ i ][ 0 ] ].insert( 
                        INSERT, str( results[ 0 ][ i ] ) )
            
            cursor.close()
            connection.close() 
Example 10
Project: PythonClassBook   Author: PythonClassRoom   File: fig17_30.py    GNU General Public License v3.0 5 votes vote down vote up
def updateAddress( self ):
      """Update address record in database"""

      if self.entries[ "ID" ].get() != "":

         # create UPDATE query command
         entryItems = self.entries.items()
         query = "UPDATE addresses SET"

         for key, value in entryItems:
            
            if key != "ID":
               query += " %s='%s'," % ( key, value.get() )

         query = query[ :-1 ] + " WHERE ID=" + self.IDEntry.get()

         # open connection, retrieve cursor and execute query
         try:      
            connection = MySQLdb.connect( db = "AddressBook" )
            cursor = connection.cursor()
            cursor.execute( query ) 
         except MySQLdb.OperationalError, message:
            errorMessage = "Error %d:\n%s" % \
               ( message[ 0 ], message[ 1 ] )
            showerror( "Error", errorMessage )
            self.clearContents()
         else:
            showinfo( "database updated", "Database Updated." )
            cursor.close()
            connection.close() 
Example 11
Project: PythonClassBook   Author: PythonClassRoom   File: fig17_29.py    GNU General Public License v3.0 5 votes vote down vote up
def submitQuery( self ):
      """Execute user-entered query agains database"""

      # open connection, retrieve cursor and execute query
      try:      
         connection = MySQLdb.connect( db = "Books" )
         cursor = connection.cursor()
         cursor.execute( self.query.get() )
      except MySQLdb.OperationalError, message:
         errorMessage = "Error %d:\n%s" % \
            ( message[ 0 ], message[ 1 ] )
         showerror( "Error", errorMessage )
         return 
Example 12
Project: chattR   Author: patrickstocklin   File: base.py    GNU General Public License v2.0 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 13
Project: chattR   Author: patrickstocklin   File: base.py    GNU General Public License v2.0 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 14
Project: tycho_mining   Author: cappsbryan   File: initialize_db.py    MIT License 5 votes vote down vote up
def create_tables():
    cursor = db_connection.cursor()
    sql_path = os.path.join(setup_dir, "create_tables.sql")
    with open(sql_path, 'rt') as sql_file:
        sql_file_string = sql_file.read()
        commands = sql_file_string.split(';')
        commands = commands[:-1]  # ignore the whitespace after the last statement
        for command in commands:
            try:
                cursor.execute(command)
            except MySQLdb.OperationalError as msg:
                print("Command failed:", msg)
                print("Exiting program")
                exit(1) 
Example 15
Project: centreon-discovery   Author: Centreon-Community   File: test_MySQLdb_nonstandard.py    GNU General Public License v2.0 5 votes vote down vote up
def test_affected_rows(self):
        self.assertEquals(self.conn.affected_rows(), 0,
                          "Should return 0 before we do anything.")


    #def test_debug(self):
        ## FIXME Only actually tests if you lack SUPER
        #self.assertRaises(MySQLdb.OperationalError,
                          #self.conn.dump_debug_info) 
Example 16
Project: mysql_utils   Author: pinterest   File: mysql_lib.py    GNU General Public License v2.0 5 votes vote down vote up
def setup_semisync_plugins(instance):
    """ Install the semi-sync replication plugins.  We may or may
        not actually use them on any given replica set, but this
        ensures that we've got them.  Semi-sync exists on all versions
        of MySQL that we might support, but we'll never use it on 5.5.

        Args:
        instance - A hostaddr object
    """
    conn = connect_mysql(instance)
    cursor = conn.cursor()

    version = get_global_variables(instance)['version']
    if version[0:3] == '5.5':
        return

    try:
        cursor.execute("INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'")
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise
        # already loaded, no work to do

    try:
        cursor.execute("INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'")
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise 
Example 17
Project: mysql_utils   Author: pinterest   File: mysql.py    GNU General Public License v2.0 5 votes vote down vote up
def query(self, sql):
        """ Executes the given SQL statement and returns a sequence of rows. """
        assert self.cursor, "%s already closed?" % (self,)
        try:
            self.cursor.execute(sql)
        except MySQLdb.OperationalError, (errcode, msg):
            if errcode != 2006:  # "MySQL server has gone away"
                raise
            self._reconnect() 
Example 18
Project: TESTGIT   Author: B-ROY   File: base.py    Apache License 2.0 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            if e.args[0] in (2006, 2013):
                from django.db import close_old_connections
                close_old_connections()

            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 19
Project: TESTGIT   Author: B-ROY   File: base.py    Apache License 2.0 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 20
Project: AneMo   Author: jspargo   File: base.py    GNU General Public License v2.0 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 21
Project: AneMo   Author: jspargo   File: base.py    GNU General Public License v2.0 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 22
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def create_marker_table(self):
        """Create marker table if it doesn't exist.

        Using a separate connection since the transaction might have to be
        reset.
        """
        connection = self.connect(autocommit=True)
        cursor = connection.cursor()
        try:
            cursor.execute(
                """ CREATE TABLE {marker_table} (
                        id            BIGINT(20)    NOT NULL AUTO_INCREMENT,
                        update_id     VARCHAR(128)  NOT NULL,
                        target_table  VARCHAR(128),
                        inserted      TIMESTAMP DEFAULT NOW(),
                        PRIMARY KEY (update_id),
                        KEY id (id)
                    )
                """
                .format(marker_table=self.marker_table)
            )
        except MySQLdb.OperationalError as e:
            if e[0] == errorcode.TABLE_EXISTS_ERROR:
                pass
            else:
                raise
        connection.close() 
Example 23
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def _runInteraction(self, interaction, *args, **kw):
        try:
            return adbapi.ConnectionPool._runInteraction(self, interaction, *args, **kw)
        except MySQLdb.OperationalError, e:
            if e[0] not in (2006, 2013):
                raise
            log.msg("RCP: got error %s, retrying operation" %(e))
            conn = self.connections.get(self.threadID())
            self.disconnect(conn)
            # try the interaction again
            return adbapi.ConnectionPool._runInteraction(self, interaction, *args, **kw) 
Example 24
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def do_command(command):
    while True:
        try:
            cursor.execute(command) ### Executes the command
            reply = cursor.fetchall() ### Grabs data
            conn.commit()
            break
        except (OperationalError, InterfaceError):
            print "Connection timed out ("+command+"). Reconnecting..." ### If MySQL went away, reconnect
            host = "www.sophosacademics.com"
            user = "sophos_vocuser"
            try:
                conn.close()
            except ProgrammingError: ### Try closing the connection, but catch an error if it's already closed
                pass
            try:
                global conn
                conn = MySQLdb.connect(host = host, user = user, passwd = sophos_password) ### Reconnect globally
                global cursor
                cursor = conn.cursor()

                cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED") ### Allow different reads based on in-moment changes
                cursor.execute("SET SESSION WAIT_TIMEOUT = 60")
                cursor.execute("USE %s" % db)
            except OperationalError:
                pass
            except ProgrammingError:
                pass

    return reply ### Return data


### There's a file called vocab-log.csv in the same folder, which tracks activity. Three columns: student name, file name (changes depending on what operation you're in, obviously) and timestamp 
Example 25
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def _runInteraction(self, interaction, *args, **kw):
        try:
            return self._myRunInteraction(interaction, *args, **kw)
        except MySQLdb.OperationalError, e:
            if e[0] not in (2006, 2013):
                raise
            log.msg("MySQLdb: got error %s, retrying operation" %(e))
            conn = self.connections.get(self.threadID())
            self.disconnect(conn)
            # try the interaction again
            return self._myRunInteraction(interaction, *args, **kw) 
Example 26
Project: GTDWeb   Author: lanbing510   File: base.py    GNU General Public License v2.0 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 27
Project: GTDWeb   Author: lanbing510   File: base.py    GNU General Public License v2.0 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 28
Project: liberator   Author: libscie   File: base.py    Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 29
Project: liberator   Author: libscie   File: base.py    Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 30
Project: banruo   Author: yingshang   File: test_django.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_mysql_errors(self):
        with patch_modules('MySQLdb'):
            import MySQLdb as mod
            mod.DatabaseError = Mock()
            mod.InterfaceError = Mock()
            mod.OperationalError = Mock()
            with self.fixup_context(self.app) as (f, _, _):
                self.assertIn(mod.DatabaseError, f.database_errors)
                self.assertIn(mod.InterfaceError, f.database_errors)
                self.assertIn(mod.OperationalError, f.database_errors)
        with mask_modules('MySQLdb'):
            with self.fixup_context(self.app):
                pass 
Example 31
Project: banruo   Author: yingshang   File: test_django.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_pg_errors(self):
        with patch_modules('psycopg2'):
            import psycopg2 as mod
            mod.DatabaseError = Mock()
            mod.InterfaceError = Mock()
            mod.OperationalError = Mock()
            with self.fixup_context(self.app) as (f, _, _):
                self.assertIn(mod.DatabaseError, f.database_errors)
                self.assertIn(mod.InterfaceError, f.database_errors)
                self.assertIn(mod.OperationalError, f.database_errors)
        with mask_modules('psycopg2'):
            with self.fixup_context(self.app):
                pass 
Example 32
Project: banruo   Author: yingshang   File: test_django.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_sqlite_errors(self):
        with patch_modules('sqlite3'):
            import sqlite3 as mod
            mod.DatabaseError = Mock()
            mod.InterfaceError = Mock()
            mod.OperationalError = Mock()
            with self.fixup_context(self.app) as (f, _, _):
                self.assertIn(mod.DatabaseError, f.database_errors)
                self.assertIn(mod.InterfaceError, f.database_errors)
                self.assertIn(mod.OperationalError, f.database_errors)
        with mask_modules('sqlite3'):
            with self.fixup_context(self.app):
                pass 
Example 33
Project: banruo   Author: yingshang   File: test_django.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_oracle_errors(self):
        with patch_modules('cx_Oracle'):
            import cx_Oracle as mod
            mod.DatabaseError = Mock()
            mod.InterfaceError = Mock()
            mod.OperationalError = Mock()
            with self.fixup_context(self.app) as (f, _, _):
                self.assertIn(mod.DatabaseError, f.database_errors)
                self.assertIn(mod.InterfaceError, f.database_errors)
                self.assertIn(mod.OperationalError, f.database_errors)
        with mask_modules('cx_Oracle'):
            with self.fixup_context(self.app):
                pass 
Example 34
Project: banruo   Author: yingshang   File: base.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                raise utils.IntegrityError(*tuple(e.args))
            raise 
Example 35
Project: Fun5G   Author: OpenWinCon   File: base.py    Apache License 2.0 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 36
Project: Fun5G   Author: OpenWinCon   File: base.py    Apache License 2.0 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 37
Project: sqlibrist   Author: condograde   File: engines.py    MIT License 5 votes vote down vote up
def apply_migration(self, name, statements, fake=False):
        import psycopg2
        connection = self.get_connection()
        with connection.cursor() as cursor:
            try:
                if not fake and statements.strip():
                    cursor.execute(statements)
            except (
                    psycopg2.OperationalError,
                    psycopg2.ProgrammingError) as e:
                connection.rollback()
                print(e.message)
                from sqlibrist.helpers import ApplyMigrationFailed

                raise ApplyMigrationFailed
            else:
                cursor.execute('INSERT INTO sqlibrist.migrations '
                               '(migration) VALUES (%s);',
                               [name.split('/')[-1]])
                connection.commit() 
Example 38
Project: sqlibrist   Author: condograde   File: engines.py    MIT License 5 votes vote down vote up
def unapply_migration(self, name, statements, fake=False):
        import psycopg2
        connection = self.get_connection()
        with connection.cursor() as cursor:
            try:
                if not fake:
                    cursor.execute(statements)
            except (
                    psycopg2.OperationalError,
                    psycopg2.ProgrammingError) as e:
                connection.rollback()
                print(e.message)
                from sqlibrist.helpers import ApplyMigrationFailed

                raise ApplyMigrationFailed
            else:
                cursor.execute('DELETE FROM sqlibrist.migrations '
                               'WHERE migration = (%s); ', [name])
                connection.commit() 
Example 39
Project: Vigtech-Services   Author: VigTech   File: base.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 40
Project: Vigtech-Services   Author: VigTech   File: base.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 41
Project: DroidPot   Author: WhySoGeeky   File: base.py    MIT License 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 42
Project: DroidPot   Author: WhySoGeeky   File: base.py    MIT License 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 43
Project: PopularityBased-SearchEngine   Author: rsunder10   File: base.py    MIT License 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 44
Project: PopularityBased-SearchEngine   Author: rsunder10   File: base.py    MIT License 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 45
Project: news_proj   Author: TheoElia   File: base.py    MIT License 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                raise utils.IntegrityError(*tuple(e.args))
            raise 
Example 46
Project: news_proj   Author: TheoElia   File: base.py    MIT License 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                raise utils.IntegrityError(*tuple(e.args))
            raise 
Example 47
Project: mudong_blog   Author: mudong1991   File: base.py    GNU General Public License v2.0 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 48
Project: mudong_blog   Author: mudong1991   File: base.py    GNU General Public License v2.0 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 49
Project: Bookmark   Author: AnkurRyder   File: base.py    MIT License 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 50
Project: Bookmark   Author: AnkurRyder   File: base.py    MIT License 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 51
Project: EvenniaPluginSampleProject   Author: castlelorestudios   File: base.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 52
Project: EvenniaPluginSampleProject   Author: castlelorestudios   File: base.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 53
Project: vue-django-element-django   Author: jonathanmusto   File: base.py    MIT License 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                raise utils.IntegrityError(*tuple(e.args))
            raise 
Example 54
Project: vue-django-element-django   Author: jonathanmusto   File: base.py    MIT License 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                raise utils.IntegrityError(*tuple(e.args))
            raise 
Example 55
Project: pykit   Author: bsc-s2   File: test_mysqlconnpool.py    MIT License 5 votes vote down vote up
def test_query_retry(self):

        pool = self.pool

        sql = (
            'set session wait_timeout=1;'
        )
        pool.query(sql)
        pool.query('show variables like "%timeout%";')

        with pool() as conn:
            time.sleep(2)
            with self.assertRaises(MySQLdb.OperationalError):
                print conn.query('show databases')

        # no error raise from above, thus a timed out conn has been left in
        # pool
        stat = pool('stat')
        dd('stat after timeout', stat)
        self.assertEqual(1, stat['create'], 'created 1 conn')

        # use previous conn, timed out and retry.
        pool.query('show databases', retry=1)

        stat = pool('stat')
        dd('stat after retry', stat)
        self.assertEqual(2, stat['create'], 'created another conn for retry') 
Example 56
Project: encarnia   Author: whitehorse-io   File: base.py    MIT License 5 votes vote down vote up
def execute(self, query, args=None):
        try:
            # args is None means no string interpolation
            return self.cursor.execute(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 57
Project: encarnia   Author: whitehorse-io   File: base.py    MIT License 5 votes vote down vote up
def executemany(self, query, args):
        try:
            return self.cursor.executemany(query, args)
        except Database.OperationalError as e:
            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.args[0] in self.codes_for_integrityerror:
                six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
            raise 
Example 58
Project: ops   Author: xiaomatech   File: mysqlops.py    MIT License 5 votes vote down vote up
def setup_semisync_plugins(instance):
    """ Install the semi-sync replication plugins.  We may or may
        not actually use them on any given replica set, but this
        ensures that we've got them.  Semi-sync exists on all versions
        of MySQL that we might support, but we'll never use it on 5.5.

        Args:
        instance - A hostaddr object
    """
    conn = connect_mysql(instance)
    cursor = conn.cursor()

    version = get_global_variables(instance)['version']
    if version[0:3] == '5.5':
        return

    try:
        cursor.execute(
            "INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'")
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise
            # already loaded, no work to do

    try:
        cursor.execute(
            "INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'")
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise 
Example 59
Project: tashi   Author: stroucki   File: resourcequerysql.py    Apache License 2.0 5 votes vote down vote up
def createConn(self):
		#  Connect to DB
		try:
			return MySQLdb.connect(host = self.host, port = self.port, user = self.user, passwd = self.passwd, db = self.db)
			#return MySQLdb.connect(host = self.config['dbHost'], port = self.config['dbPort'], user = self.config['dbUser'], passwd = self.config['dbPassword'], db = self.config['dbInst'])

		except MySQLdb.OperationalError, e:
			if e[0] == 2005:
				print "ERROR :" + str(e[1])
				return 
			else:
				print "ERROR : ", e
				return 
Example 60
Project: tashi   Author: stroucki   File: resourcequerysql.py    Apache License 2.0 5 votes vote down vote up
def __queryDb(self, query):
		self.conn.ping(True)
		cursor = self.conn.cursor()
		try:
			cursor.execute (query)
			self.conn.commit()
			row = cursor.fetchall()
			#desc = cursor.description
		except MySQLdb.OperationalError, e:
			msg = "%s : %s" % (e[1], query)
			self.log.error(msg)
			#traceback.print_exc(sys.exc_info()) 
Example 61
Project: tashi   Author: stroucki   File: resourcequerysql.py    Apache License 2.0 5 votes vote down vote up
def execQuery(self, query):
		self.conn.ping(True)
		cursor = self.conn.cursor()
		try:
			cursor.execute (query)
			self.conn.commit()
		#except Exception:
			#traceback.print_exc(sys.exc_info())
		except MySQLdb.OperationalError, e:
			msg = "%s : %s" % (e[1], query)
			self.log.error(msg)
			#traceback.print_exc(sys.exc_info())
			return 
Example 62
Project: tashi   Author: stroucki   File: resourcequerysql.py    Apache License 2.0 5 votes vote down vote up
def selectDb(self, query):
		self.conn.ping(True)
		cursor = self.conn.cursor()
		try:
			cursor.execute (query)
		#except Exception:
			#traceback.print_exc(sys.exc_info())
		except MySQLdb.OperationalError, e:
			msg = "SELECT Failed : %s : %s" % (e[1], query)
			self.log.error(msg)
			#traceback.print_exc(sys.exc_info())
			return -1 
Example 63
Project: tashi   Author: stroucki   File: resourcequerysql.py    Apache License 2.0 5 votes vote down vote up
def __deleteDb(self, query):
		self.conn.ping(True)
		cursor = self.conn.cursor()
		try:
			cursor.execute (query)
			self.conn.commit()
		except MySQLdb.OperationalError, e:
			msg = "DELETE Failed : %s : %s" % (e[1], query)
			sys.stderr.write(msg)
			self.log.error(msg)
			#traceback.print_exc(sys.exc_info())
			return -1 
Example 64
Project: tashi   Author: stroucki   File: resourcequerysql.py    Apache License 2.0 5 votes vote down vote up
def insertDb(self, query):
		self.conn.ping(True)
		cursor = self.conn.cursor()
		try:
			cursor.execute (query)
			self.conn.commit()
		#except Exception:
			#traceback.print_exc(sys.exc_info())
		except MySQLdb.OperationalError, e:
			msg = "INSERT Failed : %s : %s" % (e[1], query)
			self.log.error(msg)
			#traceback.print_exc(sys.exc_info())
			return -1 
Example 65
Project: tashi   Author: stroucki   File: m_extendDbSetup.py    Apache License 2.0 5 votes vote down vote up
def connectDb (host, port, user, passwd, db=None):
	#  Connect to DB
	try:
		if db:
			conn = MySQLdb.connect(host = host, port = port, user = user , passwd = passwd, db = db)
		else:
			conn = MySQLdb.connect(host = host, port = port, user = user , passwd = passwd)

	except MySQLdb.OperationalError, e:
		if e[0] == 2005:
			print "ERROR: %s" % str(e[1])
			exit(1)
		else:
			print "Connection Error : ", e
			exit(1) 
Example 66
Project: tashi   Author: stroucki   File: m_extendDbSetup.py    Apache License 2.0 5 votes vote down vote up
def execQuery(conn, query):
	cursor = conn.cursor()
	try:
		cursor.execute (query)
		conn.commit()
	except MySQLdb.OperationalError, e:
		sys.stdout.write("Fail\n")
		msg = "ERROR: %s" % e[1]
		sys.stderr.write(msg)
		exit() 
Example 67
Project: tashi   Author: stroucki   File: m_extensions.py    Apache License 2.0 5 votes vote down vote up
def createConn(self):
		try:
			return MySQLdb.connect(host = self.host, port = self.port, user = self.user, passwd = self.passwd, db = self.db)
		except MySQLdb.OperationalError, e:
			msg = "Error: %s" % str(e[1])
			self.log.error(msg)
			return 
Example 68
Project: tashi   Author: stroucki   File: m_extensions.py    Apache License 2.0 5 votes vote down vote up
def queryDb(self, thequery):
		self.conn.ping(True)
		cursor=self.conn.cursor()
		try:
			cursor.execute(thequery)
			self.conn.commit()
			row=cursor.fetchall()
		except MySQLdb.OperationalError, e:
			self.log.error("queryDb - %s", e)
			return -1 
Example 69
Project: tashi   Author: stroucki   File: m_extensions.py    Apache License 2.0 5 votes vote down vote up
def insertDb(self, thequery):
		self.conn.ping(True)
		cursor=self.conn.cursor()
		try:
			cursor.execute(thequery)
			self.conn.commit()
		except MySQLdb.OperationalError, e:
			self.log.error("insertDb - %s", e)
			return -1 
Example 70
Project: tashi   Author: stroucki   File: m_extensions.py    Apache License 2.0 5 votes vote down vote up
def delDb(self, thequery):
		self.conn.ping(True)
		cursor=self.conn.cursor()
		try:
			cursor.execute(thequery)
			self.conn.commit()
		except MySQLdb.OperationalError, e:
			self.log.error("delDb - %s", e)
			return -1 
Example 71
Project: mysql_utils   Author: pinterest   File: retirement_queue.py    GNU General Public License v2.0 4 votes vote down vote up
def process_mysql_shutdown(hostname=None, dry_run=False):
    """ Check stats, and shutdown MySQL instances"""
    zk = host_utils.MysqlZookeeper()
    username, password = mysql_lib.get_mysql_user_for_role('admin')
    shutdown_instances = get_retirement_queue_servers(SHUTDOWN_MYSQL)

    if hostname:
        if hostname in shutdown_instances:
            log.info('Only acting on {}'.format(hostname))
            shutdown_instances = {hostname: shutdown_instances[hostname]}
        else:
            log.info('Supplied host {} is not ready '
                     'for shutdown'.format(hostname))
            return

    for instance in shutdown_instances:
        if instance in get_protected_hosts('set'):
            log.warning('Host {hostname} is protected from '
                        'retirement'.format(hostname=hostname))
            remove_from_retirement_queue(hostname)
            continue
        for active_instance in zk.get_all_mysql_instances():
            if active_instance.hostname == instance:
                log.warning("It appears {instance} is in zk. This is "
                            "very dangerous! If you got to here, you may be "
                            "trying to turn down a replica set. Please remove "
                            "it from zk and try again"
                            "".format(instance=instance))
                continue

        if dry_run:
            log.info('In dry_run mode, not changing state')
            continue

        try:
            if check_for_user_activity(shutdown_instances[instance]):
                log.info('Activity detected on {}, removing from queue'
                         ''.format(instance))
                remove_from_retirement_queue(hostname)
                continue
            else:
                log.info('Shutting down mysql on {}'.format(instance))
                mysql_lib.shutdown_mysql(host_utils.HostAddr(instance))
        except MySQLdb.OperationalError as detail:
            (error_code, msg) = detail.args
            if error_code != mysql_lib.MYSQL_ERROR_CONN_HOST_ERROR:
                raise
            log.warning("Can't connect to MySQL on {}".format(instance))

        log_to_retirement_queue(instance,
                                shutdown_instances[instance]['instance_id'],
                                SHUTDOWN_MYSQL) 
Example 72
Project: mysql_utils   Author: pinterest   File: retirement_queue.py    GNU General Public License v2.0 4 votes vote down vote up
def terminate_instances(hostname=None, dry_run=False):
    zk = host_utils.MysqlZookeeper()
    username, password = mysql_lib.get_mysql_user_for_role('admin')
    terminate_instances = get_retirement_queue_servers(TERMINATE_INSTANCE)
    botoconn = boto.ec2.connect_to_region('us-east-1')

    if hostname:
        if hostname in terminate_instances:
            log.info('Only acting on {hostname}'.format(hostname=hostname))
            terminate_instances = {hostname: terminate_instances[hostname]}
        else:
            log.info('Supplied host {hostname} is not ready '
                     'for termination'.format(hostname=hostname))
            return

    for hostname in terminate_instances:
        if hostname in get_protected_hosts('set'):
            log.warning('Host {hostname} is protected from '
                        'retirement'.format(hostname=hostname))
            remove_from_retirement_queue(hostname)
            continue
        for instance in zk.get_all_mysql_instances():
            if instance.hostname == hostname:
                log.warning("It appears {instance} is in zk. This is "
                            "very dangerous!".format(instance=instance))
                remove_from_retirement_queue(hostname)
                continue

        log.info('Confirming mysql is down on '
                 '{hostname}'.format(hostname=hostname))

        try:
            with timeout.timeout(3):
                conn = MySQLdb.connect(
                    host=terminate_instances[hostname]['internal_ip'],
                    user=username,
                    passwd=password,
                    cursorclass=MySQLdb.cursors.DictCursor)
            log.error('Did not get MYSQL_ERROR_CONN_HOST_ERROR, removing {} '
                      'from queue'.format(hostname))
            conn.close()
            remove_from_retirement_queue(hostname)
            continue
        except MySQLdb.OperationalError as detail:
            (error_code, msg) = detail.args
            if error_code != mysql_lib.MYSQL_ERROR_CONN_HOST_ERROR:
                raise
            log.info('MySQL is down')
        log.info('Terminating instance ' '{instance}'.format(
            instance=terminate_instances[hostname]['instance_id']))
        if dry_run:
            log.info('In dry_run mode, not changing state')
        else:
            botoconn.terminate_instances(
                instance_ids=[
                    terminate_instances[hostname]['instance_id']])
            log_to_retirement_queue(hostname, terminate_instances[hostname][
                                    'instance_id'], TERMINATE_INSTANCE) 
Example 73
Project: mysql_utils   Author: pinterest   File: mysql_lib.py    GNU General Public License v2.0 4 votes vote down vote up
def setup_response_time_metrics(instance):
    """ Add Query Response Time Plugins

    Args:
    instance -  A hostaddr object
    """
    conn = connect_mysql(instance)
    cursor = conn.cursor()

    version = get_global_variables(instance)['version']
    if version[0:3] < '5.6':
        return

    try:
        cursor.execute("INSTALL PLUGIN QUERY_RESPONSE_TIME_AUDIT SONAME 'query_response_time.so'")
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise
        # already loaded, no work to do

    try:
        cursor.execute("INSTALL PLUGIN QUERY_RESPONSE_TIME SONAME 'query_response_time.so'")
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise

    try:
        cursor.execute("INSTALL PLUGIN QUERY_RESPONSE_TIME_READ SONAME 'query_response_time.so'")
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise

    try:
        cursor.execute("INSTALL PLUGIN QUERY_RESPONSE_TIME_WRITE SONAME 'query_response_time.so'")
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise
    cursor.execute("SET GLOBAL QUERY_RESPONSE_TIME_STATS=ON") 
Example 74
Project: mysql_utils   Author: pinterest   File: mysql_failover.py    GNU General Public License v2.0 4 votes vote down vote up
def is_master_alive(master, replicas):
    """ Determine if the master is alive

    The function will:
    1. Attempt to connect to the master via the mysql protcol. If successful
       the master is considered alive.
    2. If #1 fails, check the io thread of the replica instance(s). If the io
       thread is not running, the master will be considered dead. If step #1
       fails and step #2 succeeds, we are in a weird state and will throw an
       exception.

    Args:
    master - A hostaddr object for the master instance
    replicas -  A set of hostaddr objects for the replica instances

    Returns:
    A mysql connection to the master if the master is alive, False otherwise.
    """
    if len(replicas) == 0:
        raise Exception('At least one replica must be present to determine '
                        'a master is dead')
    try:
        master_conn = mysql_lib.connect_mysql(master)
        return master_conn
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != mysql_lib.MYSQL_ERROR_CONN_HOST_ERROR:
            raise
        master_conn = False
        log.info('Unable to connect to current master {master} from '
                 '{hostname}, will check replica servers beforce declaring '
                 'the master dead'.format(master=master,
                                          hostname=host_utils.HOSTNAME))
    except:
        log.info('This is an unknown connection error. If you are very sure '
                 'that the master is dead, please put a "return False" at the '
                 'top of is_master_alive and then send rwultsch a stack trace')
        raise

    # We can not get a connection to the master, so poll the replica servers
    for replica in replicas:
        # If replication has not hit a timeout, a dead master can still have
        # a replica which thinks it is ok. "STOP SLAVE; START SLAVE" followed
        # by a sleep will get us truthyness.
        mysql_lib.restart_replication(replica)
        try:
            mysql_lib.assert_replication_sanity(replica)
            raise Exception('Replica {replica} thinks it can connect to '
                            'master {master}, but failover script can not. '
                            'Possible network partition!'
                            ''.format(replica=replica,
                                      master=master))
        except:
            # The exception is expected in this case
            pass
        log.info('Replica {replica} also can not connect to master '
                 '{master}.'.format(replica=replica,
                                    master=master))
    return False 
Example 75
Project: djangocon_2015_talk   Author: npinchot   File: base.py    MIT License 4 votes vote down vote up
def get_new_connection(self, conn_params, **kwargs):
    try:
        # Get connection (call original get_new_connection method)
        conn = base.DatabaseWrapper._orig_get_new_conn(self, conn_params)
    except (django.db.OperationalError, MySQLdb.OperationalError):
        # Get read replica master DB alias (raise error if not a read replica)
        master_db = self.settings_dict.get('FAILOVER_MASTER', None)
        if not master_db:
            raise

        # DB alias from connection failure (default to current alias)
        alias = kwargs.get('failover', self.alias)

        logging.error(u'Connection error for {} ({})'.format(alias, master_db))

        # List of previously failed DBs + current failure
        failed_dbs = kwargs.get('failed_dbs', []) + [alias]

        # Get current failure retries (only increments for master)
        retries = kwargs.get('retries', 0)

        # Find a new read replica option, or default to master
        for db_alias in settings.DATABASES.keys():
            if db_alias not in failed_dbs and \
              settings.DATABASES[db_alias].get('FAILOVER_MASTER', None) == master_db:
                new_database = db_alias
                break
        else:
            # No other read replicas available - use master
            new_database = master_db

            # Master max retries on failure? Without this, we will keep retrying master until request is killed
            max_retries = settings.DATABASES[new_database].get('FAILOVER_RETRIES', None)
            if max_retries:
                # Too many failover retries? Raise error
                if retries >= max_retries:
                    raise

                retries += 1

        logging.debug(u'Custom backend chose new db: {}'.format(new_database))

        # Replace host in connection params to connect to alternate database
        conn_params['host'] = settings.DATABASES[new_database]['HOST']

        # Try new database connection
        conn = get_new_connection(self, conn_params, failover=new_database, failed_dbs=failed_dbs, retries=retries)

    return conn

# DatabaseWrapper needs to be defined so Django can import it 
Example 76
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 4 votes vote down vote up
def initialization_sequence():
    master_name = "vocabmaster" # the name of the master db of all the words and all the questions

    # as I mentioned earlier, we define a bunch of these global variables so that reconnecting is easier. it's
    # a very ugly solution, it works for now
    global host
    host = "www.sophosacademics.com" # our domain
    global user
    user = "sophos_vocuser" # the login name
    global sophos_password
    sophos_password = raw_input("Password: ") # the password (entered by the user)
    global db # the variable for the name of the database we're using (we have two: sophos_vocab and sophos_vocab2, the latter is used for testing and backup)
    if sophos_password[-2:] == 'dx': # a little shortcut i made for when we enter the password, if we add an extra x, it pre-unlocks a database (if you exited with an error)
        db = "sophos_vocab"
        sophos_password = sophos_password[:-1] # remove the x from the password
        pre_unlock = True
    elif sophos_password[-3:] == 'ddd': # another shortcut - an extra 'dd' at the end of the password switch to the alt database, for testing
        db = 'sophos_vocab2'
        sophos_password = sophos_password[:-2]
        pre_unlock = False
    else:
        db = "sophos_vocab" # otherwise, nothing special
        pre_unlock = False

    # two variables you need for a mysql connection. conn makes the connection and cursor, which is a function of conn, let's you send commands and receive replies
    global conn
    global cursor
    
    while True:
        try:
            conn = MySQLdb.connect(host = host, user = user, passwd = sophos_password) # make the connection
            cursor = conn.cursor() # create a cursor
            break
        except OperationalError: # if there's a problem
            print "Probably a wrong password or no internet connection or db is down." # probably one of these two things
            sophos_password = raw_input("Password or * to quit: ") # try again, or quit
            if sophos_password == '*':
                return False 

    cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED") # Allow different reads based on in-moment changes
    cursor.execute("SET SESSION WAIT_TIMEOUT = 60") # Set a longer timeout, though in fact playing with this variables doesn't seem to change much
    cursor.execute("USE %s" % db) # sets the db

    if not check_if_db_exists("vocabmaster"): # should never happen
        print "No master vocab."
        return False

    if pre_unlock: # if we're pre-unlocking, do that (note if the database is unlocked anyway, that won't make a problem)
        admin_master_unlock(master_name)

    return master_name

# this function asks the user to pick which student they want to work on 
Example 77
Project: banruo   Author: yingshang   File: django.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def __init__(self, app):
        self.app = app
        self.db_reuse_max = self.app.conf.get('CELERY_DB_REUSE_MAX', None)
        self._db = import_module('django.db')
        self._cache = import_module('django.core.cache')
        self._settings = symbol_by_name('django.conf:settings')

        # Database-related exceptions.
        DatabaseError = symbol_by_name('django.db:DatabaseError')
        try:
            import MySQLdb as mysql
            _my_database_errors = (mysql.DatabaseError,
                                   mysql.InterfaceError,
                                   mysql.OperationalError)
        except ImportError:
            _my_database_errors = ()      # noqa
        try:
            import psycopg2 as pg
            _pg_database_errors = (pg.DatabaseError,
                                   pg.InterfaceError,
                                   pg.OperationalError)
        except ImportError:
            _pg_database_errors = ()      # noqa
        try:
            import sqlite3
            _lite_database_errors = (sqlite3.DatabaseError,
                                     sqlite3.InterfaceError,
                                     sqlite3.OperationalError)
        except ImportError:
            _lite_database_errors = ()    # noqa
        try:
            import cx_Oracle as oracle
            _oracle_database_errors = (oracle.DatabaseError,
                                       oracle.InterfaceError,
                                       oracle.OperationalError)
        except ImportError:
            _oracle_database_errors = ()  # noqa

        try:
            self._close_old_connections = symbol_by_name(
                'django.db:close_old_connections',
            )
        except (ImportError, AttributeError):
            self._close_old_connections = None
        self.database_errors = (
            (DatabaseError, ) +
            _my_database_errors +
            _pg_database_errors +
            _lite_database_errors +
            _oracle_database_errors
        ) 
Example 78
Project: ops   Author: xiaomatech   File: mysqlops.py    MIT License 4 votes vote down vote up
def terminate_instances(hostname=None, dry_run=False):
    zk = MysqlZookeeper()
    username, password = get_mysql_user_for_role('admin')
    terminate_instances = get_retirement_queue_servers(TERMINATE_INSTANCE)
    conn = boto.ec2.connect_to_region('us-east-1')

    if hostname:
        if hostname in terminate_instances:
            log.info('Only acting on {hostname}'.format(hostname=hostname))
            terminate_instances = {hostname: terminate_instances[hostname]}
        else:
            log.info('Supplied host {hostname} is not ready '
                     'for termination'.format(hostname=hostname))
            return

    for hostname in terminate_instances:
        if hostname in get_protected_hosts('set'):
            log.warning('Host {hostname} is protected from '
                        'retirement'.format(hostname=hostname))
            remove_from_retirement_queue(hostname)
            continue
        for instance in zk.get_all_mysql_instances():
            if instance.hostname == hostname:
                log.warning("It appears {instance} is in zk. This is "
                            "very dangerous!".format(instance=instance))
                remove_from_retirement_queue(hostname)
                continue

        log.info('Confirming mysql is down on '
                 '{hostname}'.format(hostname=hostname))

        try:
            with timeout.timeout(3):
                conn = MySQLdb.connect(
                    host=terminate_instances[hostname]['internal_ip'],
                    user=username,
                    passwd=password,
                    cursorclass=MySQLdb.cursors.DictCursor)
            log.error('Did not get MYSQL_ERROR_CONN_HOST_ERROR')
            continue
        except MySQLdb.OperationalError as detail:
            (error_code, msg) = detail.args
            if error_code != MYSQL_ERROR_CONN_HOST_ERROR:
                raise
            log.info('MySQL is down')
        log.info('Terminating instance '
                 '{instance}'.format(instance=terminate_instances[hostname][
                     'instance_id']))
        if dry_run:
            log.info('In dry_run mode, not changing state')
        else:
            conn.terminate_instances(
                instance_ids=[terminate_instances[hostname]['instance_id']])
            log_to_retirement_queue(
                hostname, terminate_instances[hostname]['instance_id'],
                TERMINATE_INSTANCE) 
Example 79
Project: ops   Author: xiaomatech   File: mysqlops.py    MIT License 4 votes vote down vote up
def is_master_alive(master, replicas):
    """ Determine if the master is alive

    The function will:
    1. Attempt to connect to the master via the mysql protcol. If successful
       the master is considered alive.
    2. If #1 fails, check the io thread of the replica instance(s). If the io
       thread is not running, the master will be considered dead. If step #1
       fails and step #2 succeeds, we are in a weird state and will throw an
       exception.

    Args:
    master - A hostaddr object for the master instance
    replicas -  A set of hostaddr objects for the replica instances

    Returns:
    A mysql connection to the master if the master is alive, False otherwise.
    """
    if len(replicas) == 0:
        raise Exception('At least one replica must be present to determine '
                        'a master is dead')
    try:
        master_conn = connect_mysql(master)
        return master_conn
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_CONN_HOST_ERROR:
            raise
        master_conn = False
        log.info('Unable to connect to current master {master} from '
                 '{hostname}, will check replica servers beforce declaring '
                 'the master dead'.format(
                     master=master, hostname=HOSTNAME))
    except:
        log.info('This is an unknown connection error. If you are very sure '
                 'that the master is dead, please put a "return False" at the '
                 'top of is_master_alive and then send rwultsch a stack trace')
        raise

    # We can not get a connection to the master, so poll the replica servers
    for replica in replicas:
        # If replication has not hit a timeout, a dead master can still have
        # a replica which thinks it is ok. "STOP SLAVE; START SLAVE" followed
        # by a sleep will get us truthyness.
        restart_replication(replica)
        try:
            assert_replication_sanity(replica)
            raise Exception('Replica {replica} thinks it can connect to '
                            'master {master}, but failover script can not. '
                            'Possible network partition!'
                            ''.format(
                                replica=replica, master=master))
        except:
            # The exception is expected in this case
            pass
        log.info('Replica {replica} also can not connect to master '
                 '{master}.'.format(
                     replica=replica, master=master))
    return False 
Example 80
Project: ops   Author: xiaomatech   File: mysqlops.py    MIT License 4 votes vote down vote up
def setup_response_time_metrics(instance):
    """ Add Query Response Time Plugins

    Args:
    instance -  A hostaddr object
    """
    conn = connect_mysql(instance)
    cursor = conn.cursor()

    version = get_global_variables(instance)['version']
    if version[0:3] < '5.6':
        return

    try:
        cursor.execute(
            "INSTALL PLUGIN QUERY_RESPONSE_TIME_AUDIT SONAME 'query_response_time.so'"
        )
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise
            # already loaded, no work to do

    try:
        cursor.execute(
            "INSTALL PLUGIN QUERY_RESPONSE_TIME SONAME 'query_response_time.so'"
        )
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise

    try:
        cursor.execute(
            "INSTALL PLUGIN QUERY_RESPONSE_TIME_READ SONAME 'query_response_time.so'"
        )
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise

    try:
        cursor.execute(
            "INSTALL PLUGIN QUERY_RESPONSE_TIME_WRITE SONAME 'query_response_time.so'"
        )
    except MySQLdb.OperationalError as detail:
        (error_code, msg) = detail.args
        if error_code != MYSQL_ERROR_FUNCTION_EXISTS:
            raise
    cursor.execute("SET GLOBAL QUERY_RESPONSE_TIME_STATS=ON")