当前位置: 首页>>代码示例>>Python>>正文


Python Server.exec_query方法代码示例

本文整理汇总了Python中mysql.utilities.common.server.Server.exec_query方法的典型用法代码示例。如果您正苦于以下问题:Python Server.exec_query方法的具体用法?Python Server.exec_query怎么用?Python Server.exec_query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mysql.utilities.common.server.Server的用法示例。


在下文中一共展示了Server.exec_query方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _bulk_insert

# 需要导入模块: from mysql.utilities.common.server import Server [as 别名]
# 或者: from mysql.utilities.common.server.Server import exec_query [as 别名]
    def _bulk_insert(self, rows, new_db, destination=None):
        """Import data using bulk insert

        Reads data from a table and builds group INSERT statements for writing
        to the destination server specified (new_db.name).

        This method is designed to be used in a thread for parallel inserts.
        As such, it requires its own connection to the destination server.

        Note: This method does not print any information to stdout.

        rows[in]           a list of rows to process
        new_db[in]         new database name
        destination[in]    the destination server
        """
        if self.dest_vals is None:
            self.dest_vals = self.get_dest_values(destination)

        # Spawn a new connection
        server_options = {
            'conn_info': self.dest_vals,
            'role': "thread",
        }
        dest = Server(server_options)
        dest.connect()

        # Issue the write lock
        lock_list = [("%s.%s" % (new_db, self.q_tbl_name), 'WRITE')]
        my_lock = Lock(dest, lock_list, {'locking': 'lock-all', })

        # First, turn off foreign keys if turned on
        dest.disable_foreign_key_checks(True)

        if self.column_format is None:
            self.get_column_metadata()

        data_lists = self.make_bulk_insert(rows, new_db)
        insert_data = data_lists[0]
        blob_data = data_lists[1]

        # Insert the data first
        for data_insert in insert_data:
            try:
                dest.exec_query(data_insert, self.query_options)
            except UtilError, e:
                raise UtilError("Problem inserting data. "
                                "Error = %s" % e.errmsg)
开发者ID:akhan786,项目名称:LaunchDevelopment,代码行数:49,代码来源:table.py

示例2: do_command

# 需要导入模块: from mysql.utilities.common.server import Server [as 别名]
# 或者: from mysql.utilities.common.server.Server import exec_query [as 别名]
    def do_command(self):
        """ Check and execute the audit log command (previously set by the the
        options of the object constructor).
        """
        # Check for valid command
        command = self.options.get("command", None)
        if not command in VALID_COMMANDS:
            raise UtilError("Invalid command.")

        command_value = self.options.get("value", None)
        # Check for valid value if needed
        if (command_requires_value(command)
           and not check_command_value(command, command_value)):
            raise UtilError("Please provide the correct value for the %s "
                            "command." % command)

        # Copy command does not need the server
        if command == "COPY":
            self._copy_log()
            return True

        # Connect to server
        server = Server({'conn_info': self.options.get("server_vals", None)})
        server.connect()

        # Now execute the command
        print "#\n# Executing %s command.\n#\n" % command
        try:
            if command == "POLICY":
                server.exec_query("SET @@GLOBAL.audit_log_policy = %s" %
                                  command_value)
            elif command == "ROTATE":
                self._rotate_log(server)
            else:  # "ROTATE_ON_SIZE":
                server.exec_query("SET @@GLOBAL.audit_log_rotate_on_size = %s"
                                  % command_value)
        finally:
            server.disconnect()

        return True
开发者ID:akhan786,项目名称:LaunchDevelopment,代码行数:42,代码来源:audit_log.py

示例3: load_test_data

# 需要导入模块: from mysql.utilities.common.server import Server [as 别名]
# 或者: from mysql.utilities.common.server.Server import exec_query [as 别名]
def load_test_data(server, db_num=1):
    """Load/insert data into the test databases.

    A considerable amount of data should be considered in order to take some
    time to load, allowing mysqlrplsync to be executed at the same time the
    data is still being inserted.

    server[in]      Target server to load the test data.
    db_num[in]      Number of databases to load the data (by default: 1).
                    It is assumed that a matching number of test databases
                    have been previously created.

    Note: method prepared to be invoked by a different thread.
    """
    # Create a new server instance with a new connection (for multithreading).
    srv = Server({'conn_info': server})
    srv.connect()

    for db_index in xrange(db_num):
        db_name = '`test_rplsync_db{0}`'.format(
            '' if db_num == 1 else db_index
        )
        # Insert random data on all tables.
        random_values = string.letters + string.digits
        for _ in xrange(TEST_DB_NUM_ROWS):
            columns = []
            values = []
            for table_index in xrange(TEST_DB_NUM_TABLES):
                columns.append('rnd_txt{0}'.format(table_index))
                rnd_text = "".join(
                    [random.choice(random_values) for _ in xrange(20)]
                )
                values.append("'{0}'".format(rnd_text))
                insert = ("INSERT INTO {0}.`t{1}` ({2}) VALUES ({3})"
                          "").format(db_name, table_index, ', '.join(columns),
                                     ', '.join(values))
                srv.exec_query(insert)
                srv.commit()
开发者ID:abooitt,项目名称:mysql-utilities,代码行数:40,代码来源:rpl_sync.py

示例4: BinaryLogPurge

# 需要导入模块: from mysql.utilities.common.server import Server [as 别名]
# 或者: from mysql.utilities.common.server.Server import exec_query [as 别名]

#.........这里部分代码省略.........
            "{0}.{1}".format(binlog_file_name,
                             repr(index_last_not_in_use).zfill(z_len))
        )

        if server_is_master:
            self._report("# Latest binlog file replicated by all slaves: "
                         "{0}".format(last_binlog_not_in_use))

        if target_binlog_index is None:
            # Purge to latest binlog not in use
            if self.verbosity > 0:
                self._report("# Latest not active binlog"
                             " file: {0}".format(last_binlog_not_in_use))

            # last_binlog_not_in_use
            purge(server, purge_to_binlog, server_binlogs_list,
                  reporter=self._report, dryrun=self.dry_run,
                  verbosity=self.verbosity)
        else:
            purge_to_binlog = (
                "{0}.{1}".format(binlog_file_name,
                                 repr(target_binlog_index).zfill(z_len))
            )
            if purge_to_binlog not in server_binlogs_list:
                self._report(
                    _COULD_NOT_FIND_BINLOG.format(bin_name=self.to_binlog_name,
                                                  server_name=server_name,
                                                  host=server.host,
                                                  port=server.port))
                return

            if target_binlog_index > index_last_in_use:
                self._report("WARNING: The given binlog name: '{0}' is "
                             "required for one or more slaves, the Utilitiy "
                             "will purge to binlog '{1}' instead."
                             "".format(self.to_binlog_name,
                                       last_binlog_not_in_use))
                target_binlog_index = last_binlog_not_in_use

            # last_binlog_not_in_use
            purge(server, purge_to_binlog, server_binlogs_list,
                  reporter=self._report, dryrun=self.dry_run,
                  verbosity=self.verbosity)

        server_binlogs_list_after = server.get_server_binlogs_list()
        if self.verbosity >= 1:
            _report_binlogs(server_binlogs_list_after, self._report)
        for binlog in server_binlogs_list_after:
            if binlog in server_binlogs_list:
                server_binlogs_list.remove(binlog)
        if self.verbosity >= 1 and server_binlogs_list:
            _report_binlogs(server_binlogs_list, self._report, removed=True)

    def purge(self):
        """The purge method for a standalone server.

        Determines the latest log file to purge, which becomes the target
        file to purge binary logs to in case no other file is specified.
        """
        # Connect to server
        self.server = Server({'conn_info': self.server_cnx_val})
        self.server.connect()

        # Check required privileges
        check_privileges(self.server, BINLOG_OP_PURGE,
                         ["SUPER", "REPLICATION SLAVE"],
                         BINLOG_OP_PURGE_DESC, self.verbosity, self._report)

        # retrieve active binlog info
        binlog_file_name, active_binlog_file, index_last_in_use = (
            get_binlog_info(self.server, reporter=self._report,
                            server_name="server", verbosity=self.verbosity)
        )

        # Verify this server is not a Master.
        processes = self.server.exec_query("SHOW PROCESSLIST")
        binlog_dump = False
        for process in processes:
            if process[4] == "Binlog Dump":
                binlog_dump = True
                break
        hosts = self.server.exec_query("SHOW SLAVE HOSTS")
        if binlog_dump or hosts:
            if hosts and not self.verbosity:
                msg_v = " For more info use verbose option."
            else:
                msg_v = ""
            if self.verbosity >= 1:
                for host in hosts:
                    self._report("# WARNING: Slave with id:{0} at {1}:{2} "
                                 "is connected to this server."
                                 "".format(host[0], host[1], host[2]))
            raise UtilError("The given server is acting as a master and has "
                            "slaves connected to it. To proceed please use the"
                            " --master option.{0}".format(msg_v))

        target_binlog_index = self.get_target_binlog_index(binlog_file_name)

        self._purge(index_last_in_use, active_binlog_file, binlog_file_name,
                    target_binlog_index)
开发者ID:abooitt,项目名称:mysql-utilities,代码行数:104,代码来源:binlog_admin.py

示例5: clone_server

# 需要导入模块: from mysql.utilities.common.server import Server [as 别名]
# 或者: from mysql.utilities.common.server.Server import exec_query [as 别名]
def clone_server(conn_val, options):
    """Clone an existing server

    This method creates a new instance of a running server using a datadir
    set to the new_data parametr, with a port set to new_port, server_id
    set to new_id and a root password of root_pass. You can also specify
    additional parameters for the mysqld command line as well as turn on
    verbosity mode to display more diagnostic information during the clone
    process.

    The method will build a new base database installation from the .sql
    files used to construct a new installation. Once the database is
    created, the server will be started.

    dest_val[in]        a dictionary containing connection information
                        including:
                        (user, password, host, port, socket)
    options[in]         dictionary of options:
      new_data[in]        An existing path to create the new database and use
                          as datadir for new instance
                          (default = None)
      new_port[in]        Port number for new instance
                          (default = 3307)
      new_id[in]          Server_id for new instance
                          (default = 2)
      root_pass[in]       Password for root user on new instance (optional)
      mysqld_options[in]  Additional command line options for mysqld
      verbosity[in]       Print additional information during operation
                          (default is 0)
      quiet[in]           If True, do not print messages.
                          (default is False)
      cmd_file[in]        file name to write startup command
      start_timeout[in]   Number of seconds to wait for server to start
    """
    new_data = os.path.abspath(options.get('new_data', None))
    new_port = options.get('new_port', '3307')
    root_pass = options.get('root_pass', None)
    verbosity = options.get('verbosity', 0)
    user = options.get('user', 'root')
    quiet = options.get('quiet', False)
    cmd_file = options.get('cmd_file', None)
    start_timeout = int(options.get('start_timeout', 10))
    mysqld_options = options.get('mysqld_options', '')
    force = options.get('force', False)
    quote_char = "'" if os.name == "posix" else '"'

    if not check_port_in_use('localhost', int(new_port)):
        raise UtilError("Port {0} in use. Please choose an "
                        "available port.".format(new_port))

    # Check if path to database files is greater than MAX_DIR_SIZE char,
    if len(new_data) > MAX_DATADIR_SIZE and not force:
        raise UtilError("The --new-data path '{0}' is too long "
                        "(> {1} characters). Please use a smaller one. "
                        "You can use the --force option to skip this "
                        "check".format(new_data, MAX_DATADIR_SIZE))

    # Clone running server
    if conn_val is not None:
        # Try to connect to the MySQL database server.
        server1_options = {
            'conn_info': conn_val,
            'role': "source",
        }
        server1 = Server(server1_options)
        server1.connect()
        if not quiet:
            print "# Cloning the MySQL server running on %s." % \
                conn_val["host"]

        # Get basedir
        rows = server1.exec_query("SHOW VARIABLES LIKE 'basedir'")
        if not rows:
            raise UtilError("Unable to determine basedir of running server.")
        basedir = os.path.normpath(rows[0][1])

    # Cloning downed or offline server
    else:
        basedir = os.path.abspath(options.get("basedir", None))
        if not quiet:
            print "# Cloning the MySQL server located at %s." % basedir

    new_data_deleted = False
    # If datadir exists, has data, and user said it was Ok, delete it
    if os.path.exists(new_data) and options.get("delete", False) and \
       os.listdir(new_data):
        new_data_deleted = True
        shutil.rmtree(new_data, True)

    # Create new data directory if it does not exist
    if not os.path.exists(new_data):
        if not quiet:
            print "# Creating new data directory..."
        try:
            os.mkdir(new_data)
        except OSError as err:
            raise UtilError("Unable to create directory '{0}', reason: {1}"
                            "".format(new_data, err.strerror))

    # After create the new data directory, check for free space, so the errors
#.........这里部分代码省略.........
开发者ID:fengshao0907,项目名称:mysql-utilities,代码行数:103,代码来源:serverclone.py

示例6: exit

# 需要导入模块: from mysql.utilities.common.server import Server [as 别名]
# 或者: from mysql.utilities.common.server.Server import exec_query [as 别名]
# Get list of databases from the server if not specified in options
print "# Getting databases..."
db_list = []
if opt.dbs_to_copy is None:
    for db in server1.get_all_databases():
        db_list.append((db[0], None))
else:
    for db in opt.dbs_to_copy.split(","):
        db_list.append((db, None))

# Get list of all users from the server
print "# Getting users..."
user_list=[]
if opt.users_to_copy is None:
    users = server1.exec_query("SELECT user, host "
                               "FROM mysql.user "
                               "WHERE user != 'root' and user != ''")
    for user in users:
        user_list.append(user[0]+'@'+user[1])
else:
    for user in opt.users_to_copy.split(","):
        user_list.append(user)

# Clone the server
print "# Cloning server instance..."
try:
    res = serverclone.clone_server(conn, opt.new_data, opt.new_port,
                                    opt.new_id, "root", None, False, True)
except exception.UtilError, e:
    print "ERROR:", e.errmsg
    exit(1)
开发者ID:LinhDart,项目名称:mysql-utilities,代码行数:33,代码来源:copy_server.py

示例7: clone_server

# 需要导入模块: from mysql.utilities.common.server import Server [as 别名]
# 或者: from mysql.utilities.common.server.Server import exec_query [as 别名]
def clone_server(conn_val, options):
    """Clone an existing server

    This method creates a new instance of a running server using a datadir
    set to the new_data parametr, with a port set to new_port, server_id
    set to new_id and a root password of root_pass. You can also specify
    additional parameters for the mysqld command line as well as turn on
    verbosity mode to display more diagnostic information during the clone
    process.

    The method will build a new base database installation from the .sql
    files used to construct a new installation. Once the database is
    created, the server will be started.

    dest_val[in]        a dictionary containing connection information
                        including:
                        (user, password, host, port, socket)
    options[in]         dictionary of options:
      new_data[in]        An existing path to create the new database and use
                          as datadir for new instance
                          (default = None)
      new_port[in]        Port number for new instance
                          (default = 3307)
      new_id[in]          Server_id for new instance
                          (default = 2)
      root_pass[in]       Password for root user on new instance (optional)
      mysqld_options[in]  Additional command line options for mysqld
      verbosity[in]       Print additional information during operation
                          (default is 0)
      quiet[in]           If True, do not print messages.
                          (default is False)
      cmd_file[in]        file name to write startup command
    """

    from mysql.utilities.common.server import Server
    from mysql.utilities.exception import UtilError
    from mysql.utilities.common.tools import get_tool_path

    new_data = options.get("new_data", None)
    new_port = options.get("new_port", "3307")
    root_pass = options.get("root_pass", None)
    verbosity = options.get("verbosity", 0)
    quiet = options.get("quiet", False)
    cmd_file = options.get("cmd_file", None)

    # Clone running server
    if conn_val is not None:
        # Try to connect to the MySQL database server.
        server1_options = {"conn_info": conn_val, "role": "source"}
        server1 = Server(server1_options)
        server1.connect()

        if not quiet:
            print "# Cloning the MySQL server running on %s." % conn_val["host"]

        basedir = ""
        # Get basedir
        rows = server1.exec_query("SHOW VARIABLES LIKE 'basedir'")
        if not rows:
            raise UtilError("Unable to determine basedir of running server.")
        basedir = rows[0][1]

    # Cloning downed or offline server
    else:
        basedir = options.get("basedir", None)
        if not quiet:
            print "# Cloning the MySQL server located at %s." % basedir

    # If datadir exists, delete it
    if os.path.exists(new_data):
        shutil.rmtree(new_data, True)

    # Create new data directory if it does not exist
    if not quiet:
        print "# Creating new data directory..."
    if not os.path.exists(new_data):
        try:
            res = os.mkdir(new_data)
        except:
            raise UtilError("Unable to create directory '%s'" % new_data)

    if not quiet:
        print "# Configuring new instance..."
        print "# Locating mysql tools..."

    mysqld_path = get_tool_path(basedir, "mysqld")
    mysqladmin_path = get_tool_path(basedir, "mysqladmin")
    mysql_basedir = get_tool_path(basedir, "share/english/errgmsg.sys", False, False)
    mysql_basedir = basedir
    if os.path.exists(os.path.join(basedir, "local/mysql/share/")):
        mysql_basedir = os.path.join(mysql_basedir, "local/mysql/")
    # for source trees
    elif os.path.exists(os.path.join(basedir, "/sql/share/english/")):
        mysql_basedir = os.path.join(mysql_basedir, "/sql/")
    system_tables = get_tool_path(basedir, "mysql_system_tables.sql", False)
    system_tables_data = get_tool_path(basedir, "mysql_system_tables_data.sql", False)
    test_data_timezone = get_tool_path(basedir, "mysql_test_data_timezone.sql", False)
    help_data = get_tool_path(basedir, "fill_help_tables.sql", False)

    if verbosity >= 3 and not quiet:
#.........这里部分代码省略.........
开发者ID:dannykopping,项目名称:deploi,代码行数:103,代码来源:serverclone.py

示例8: clone_server

# 需要导入模块: from mysql.utilities.common.server import Server [as 别名]
# 或者: from mysql.utilities.common.server.Server import exec_query [as 别名]
def clone_server(conn_val, options):
    """Clone an existing server

    This method creates a new instance of a running server using a datadir
    set to the new_data parametr, with a port set to new_port, server_id
    set to new_id and a root password of root_pass. You can also specify
    additional parameters for the mysqld command line as well as turn on
    verbosity mode to display more diagnostic information during the clone
    process.

    The method will build a new base database installation from the .sql
    files used to construct a new installation. Once the database is
    created, the server will be started.

    dest_val[in]        a dictionary containing connection information
                        including:
                        (user, password, host, port, socket)
    options[in]         dictionary of options:
      new_data[in]        An existing path to create the new database and use
                          as datadir for new instance
                          (default = None)
      new_port[in]        Port number for new instance
                          (default = 3307)
      new_id[in]          Server_id for new instance
                          (default = 2)
      root_pass[in]       Password for root user on new instance (optional)
      mysqld_options[in]  Additional command line options for mysqld
      verbosity[in]       Print additional information during operation
                          (default is 0)
      quiet[in]           If True, do not print messages.
                          (default is False)
      cmd_file[in]        file name to write startup command
      start_timeout[in]   Number of seconds to wait for server to start
    """
    new_data = os.path.abspath(options.get('new_data', None))
    new_port = options.get('new_port', '3307')
    root_pass = options.get('root_pass', None)
    verbosity = options.get('verbosity', 0)
    user = options.get('user', 'root')
    quiet = options.get('quiet', False)
    cmd_file = options.get('cmd_file', None)
    start_timeout = int(options.get('start_timeout', 10))
    mysqld_options = options.get('mysqld_options', '')

    if not check_port_in_use('localhost', int(new_port)):
        raise UtilError("Port {0} in use. Please choose an "
                        "available port.".format(new_port))

    # Clone running server
    if conn_val is not None:
        # Try to connect to the MySQL database server.
        server1_options = {
            'conn_info': conn_val,
            'role': "source",
        }
        server1 = Server(server1_options)
        server1.connect()
        if not quiet:
            print "# Cloning the MySQL server running on %s." % \
                conn_val["host"]

        basedir = ""
        # Get basedir
        rows = server1.exec_query("SHOW VARIABLES LIKE 'basedir'")
        if not rows:
            raise UtilError("Unable to determine basedir of running server.")
        basedir = os.path.normpath(rows[0][1])

    # Cloning downed or offline server
    else:
        basedir = os.path.abspath(options.get("basedir", None))
        if not quiet:
            print "# Cloning the MySQL server located at %s." % basedir

    # If datadir exists, has data, and user said it was Ok, delete it
    if os.path.exists(new_data) and options.get("delete", False) and \
       os.listdir(new_data):
        shutil.rmtree(new_data, True)

    # Create new data directory if it does not exist
    if not os.path.exists(new_data):
        if not quiet:
            print "# Creating new data directory..."
        try:
            os.mkdir(new_data)
        except:
            raise UtilError("Unable to create directory '%s'" % new_data)

    if not quiet:
        print "# Configuring new instance..."
        print "# Locating mysql tools..."

    mysqld_path = get_tool_path(basedir, "mysqld")
    mysqladmin_path = get_tool_path(basedir, "mysqladmin")
    mysql_basedir = get_tool_path(basedir, "share/english/errgmsg.sys",
                                  False, False)
    mysql_basedir = basedir
    if os.path.exists(os.path.join(basedir, "local/mysql/share/")):
        mysql_basedir = os.path.join(mysql_basedir, "local/mysql/")
    # for source trees
#.........这里部分代码省略.........
开发者ID:akhan786,项目名称:LaunchDevelopment,代码行数:103,代码来源:serverclone.py


注:本文中的mysql.utilities.common.server.Server.exec_query方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。