MySQL MGR集群单主模式的自动搭建和自动化故障修(4)

'''
reset master
'''
def reset_master(conn_dict):
    try:
        execute_noquery(conn_dict, "reset master;")
    except:
        raise


def install_group_replication_plugin(conn_dict):
    get_plugin_sql = "SELECT name,dl FROM mysql.plugin WHERE name = 'group_replication';"
    install_plugin_sql = '''install plugin group_replication soname 'group_replication.so'; '''
    try:
        result = execute_query(conn_dict, get_plugin_sql)
        if not result:
            execute_noquery(conn_dict, install_plugin_sql)
    except:
        raise


def create_mgr_repl_user(conn_master_dict,user,password):
    try:
        reset_master(conn_master_dict)
        sql_exists_user = '''select user from mysql.user where user = '{0}'; '''.format(user)
        user_list = execute_query(conn_master_dict,sql_exists_user)
        if not user_list:
            create_user_sql = '''create user {0}@'%' identified by '{1}'; '''.format(user,password)
            grant_privilege_sql = '''grant replication slave on *.* to {0}@'%';'''.format(user)
            execute_noquery(conn_master_dict,create_user_sql)
            execute_noquery(conn_master_dict, grant_privilege_sql)
            execute_noquery(conn_master_dict, "flush privileges;")
    except:
        raise


def set_super_read_only_off(conn_dict):
    super_read_only_off = '''set global super_read_only = 0;'''
    execute_noquery(conn_dict, super_read_only_off)


def open_group_replication_bootstrap_group(conn_dict):
    sql = '''select variable_name,variable_value from performance_schema.global_variables where variable_name = 'group_replication_bootstrap_group';'''
    result = execute_query(conn_dict, sql)
    open_bootstrap_group_sql = '''set @@global.group_replication_bootstrap_group=on;'''
    if result and result[0]['variable_value']=="OFF":
        execute_noquery(conn_dict, open_bootstrap_group_sql)


def close_group_replication_bootstrap_group(conn_dict):
    sql = '''select variable_name,variable_value from performance_schema.global_variables where variable_name = 'group_replication_bootstrap_group';'''
    result = execute_query(conn_dict, sql)
    close_bootstrap_group_sql = '''set @@global.group_replication_bootstrap_group=off;'''
    if result and result[0]['variable_value'] == "ON":
        execute_noquery(conn_dict, close_bootstrap_group_sql)


def start_group_replication(conn_dict):
    start_group_replication = '''start group_replication;'''
    group_replication_status = get_group_replication_status(conn_dict)
    if not (group_replication_status[0]['MEMBER_STATE'] == 'ONLINE'):
        execute_noquery(conn_dict, start_group_replication)


def connect_to_group(conn_dict,repl_user,repl_password):
    connect_to_group_sql = '''change master to
                                    master_user='{0}',
                                    master_password='{1}'
                                    for channel 'group_replication_recovery'; '''.format(repl_user,repl_password)
    try:
        execute_noquery(conn_dict, connect_to_group_sql)
    except:
        raise

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/ad335d474e4dadec631e368ca9b0561e.html