zoukankan      html  css  js  c++  java
  • python—mariadb自动部署主从

    import configparser
    import os
    def config_mariadb_yum():
        exists = os.path.exists('/etc/yum.repos.d/mariadb.repo')
        if exists:
            print('mariadb.repo文件已经存在')
            yum_install_mariadb()
        else:
            config = configparser.ConfigParser()
            config.read('/etc/yum.repos.d/mariadb.repo', encoding='utf-8')
            config.add_section('mariadb')
            config.set('mariadb', 'name', 'MariaDB')
            config.set('mariadb', 'baseurl', 'http://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64/')
            config.set('mariadb', 'gpgkey', 'http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB')
            config.set('mariadb', 'gpgcheck', '1')
            config.write(open("/etc/yum.repos.d/mariadb.repo", "w"))
            yum_install_mariadb()
    
    def yum_install_mariadb():
            res1 = os.system('yum install MariaDB -y > /dev/null 2&>1')
            if res1 == 0:
                config = configparser.ConfigParser()
                config.read('/etc/my.cnf.d/server.cnf', encoding='utf-8')
                config.set('server','server_id','1')
                config.set('server','log-bin','mysql-bin')
                config.write(open("/etc/my.cnf.d/server.cnf", "w"))
                res2 = os.system('service mariadb restart')
                if res2 == 0:
                    os.system('mysql_secure_installation')
                    os.system('''mysql -uroot -proot -e "grant replication slave on *.* to 'slave'@'%' identified by 'slave'"''')
                    os.system("mysql -uroot -proot -e 'show master status'")
    
    def main():
        config_mariadb_yum()
    if __name__ == '__main__':
        main()
    
    
    
    slave
    
    import configparser
    import os
    master_ip = input('master_ip:').strip()
    log_file = input('log_file:').strip()
    pos = input('pos:').strip()
    def config_mariadb_yum():
        exists = os.path.exists('/etc/yum.repos.d/mariadb.repo')
        if exists:
            print('mariadb.repo文件已经存在')
            yum_install_mariadb()
        else:
            config = configparser.ConfigParser()
            config.read('/etc/yum.repos.d/mariadb.repo', encoding='utf-8')
            config.add_section('mariadb')
            config.set('mariadb', 'name', 'MariaDB')
            config.set('mariadb', 'baseurl', 'http://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64/')
            config.set('mariadb', 'gpgkey', 'http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB')
            config.set('mariadb', 'gpgcheck', '1')
            config.write(open("/etc/yum.repos.d/mariadb.repo", "w"))
            yum_install_mariadb()
    
    def yum_install_mariadb():
            res1 = os.system('yum install MariaDB -y > /dev/null 2&>1')
            if res1 == 0:
                config = configparser.ConfigParser()
                config.read('/etc/my.cnf.d/server.cnf', encoding='utf-8')
                config.set('server','server_id','2')
                config.write(open("/etc/my.cnf.d/server.cnf", "w"))
                res2 = os.system('service mariadb restart')
                if res2 == 0:
                    os.system('mysql_secure_installation')
                    os.system('''mysql -uroot -proot -e "CHANGE MASTER TO MASTER_HOST='%s', MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s"''' % (master_ip,log_file,pos))
                    os.system("mysql -uroot -proot -e 'start slave;'")
    
    def main():
        config_mariadb_yum()
    if __name__ == '__main__':
        main()
    

      

  • 相关阅读:
    KMP的next[]数组
    [Gauss]HDOJ3976 Electric resistance
    [Gauss]POJ2065 SETI
    [Gauss]POJ2947 Widget Factory
    更好的 SQL 模式的 10 条规则
    BZOJ2460: [BeiJing2011]元素
    BZOJ2115: [Wc2011] Xor
    洛谷P3164 [CQOI2014]和谐矩阵
    POJ1222熄灯问题
    POJ1830开关问题
  • 原文地址:https://www.cnblogs.com/twoo/p/11750571.html
Copyright © 2011-2022 走看看