博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
my.cnf 自动生成脚本
阅读量:4070 次
发布时间:2019-05-25

本文共 4148 字,大约阅读时间需要 13 分钟。

利用python 写了一个自动生成my.cnf 的脚本,写的很简单,运行结果如下:

(yuhuashi) [root@yuhuashi opt]# python auto_mycnf.py MySQL安装文件路径:/opt/mysql    MySQL数据文件路径:/database/mysqlMySQL监听端口:3306日志保留天数:7

脚本代码如下:

#/usr/bin/python# -*- coding: UTF-8 -*-import osimport configparserimport socketimport psutildef get_hostname():    return socket.gethostname()def get_meminfo():    memory = psutil.virtual_memory()    ##memory_total = memory.total    #memory_free = memory.free    return memory.freedef get_cpu():     return psutil.cpu_count(logical=False)def get_ip():    info = psutil.net_if_addrs()    ip_addr='127.0.0.1'    for k,v in info.items():        for item in v:          if item[0] == 2 and not item[1]=='127.0.0.1':            ip_addr=item[1]    return ip_addrdef auto_mycnf(mysql_port,mysql_install_dir,datadir,innodb_buffer_pool_size,expire_days,cpu_cores,local_ip):    config= configparser.ConfigParser()    config['client']={        'port':mysql_port,        'socket': mysql_install_dir+'/mysql.sock'    }    config['mysqld']={        'port':mysql_port,        'socket':mysql_install_dir+'/mysql.sock',        'basedir':mysql_install_dir,        'datadir':datadir,        'server_id':mysql_port,        'report_host':local_ip,        'user':'mysql',        'federated':'',        'key_buffer_size':'512M',        'read_buffer_size':'8M',        'read_rnd_buffer_size':'8M',        'join_buffer_size':'8M',        'sort_buffer_size':'8M',        'myisam_sort_buffer_size':'64M',        'max_heap_table_size':'128M',        'tmp_table_size':'128M',        'max_allowed_packet':'16M',        'table_open_cache':'4096',        'thread_cache_size':'4096',        'thread_concurrency':'38',        'back_log':'1024',        'max_connections':'6000',        'max_user_connections':'2000',        'max_connect_errors':'10',        'wait_timeout':'60',        'lower_case_table_names':'1',        'innodb_data_file_path':'ibdata1:2000M;ibdata2:10M:autoextend',        'innodb_buffer_pool_size':innodb_buffer_pool_size+'G',        'innodb_additional_mem_pool_size':'10M',        'innodb_log_file_size':'256M',        'innodb_log_buffer_size':'16M',        'innodb_flush_log_at_trx_commit':'1',        'innodb_lock_wait_timeout':'50',        'innodb_file_per_table':'1',        'innodb_log_files_in_group':'10',        'innodb_flush_method':'O_DIRECT',        'innodb_open_file':'1024',        'innodb_purge_threads':'4',        'innodb_write_io_threads':cpu_cores,        'innodb_read_io_threads':cpu_cores,        'log_error':mysql_install_dir+'/data/error-log',        'pid-file':'mysqld.pid',        'log_bin':mysql_install_dir+'/data/mysql-bin',        'max_binlog_size':'1G',        'max_binlog_cache_size':'2G',        'binlog_format':'mixed',        'expire_logs_days':expire_days,        'general_log_file':mysql_install_dir+'/data/general.log',        'relay_log':mysql_install_dir+'/data/relay-bin',        'log_slave_updates':'1',        'slave_compressed_protocol':'1',        'slow_query_log':'1',        'slow_query_log':mysql_install_dir+'/data/slow-query.log',        'long_query_time':'1',        'log_queries_not_using_indexes':'0',        'event_scheduler':'on',        'log_slave-_updates':'on',        'skip_external_locking':'ON',        'skip_name_resolve':'ON',    }    with open('my.cnf', 'w') as configfile:      config.write(configfile)if __name__=='__main__':    mysql_install_dir=input('MySQL安装文件路径:')    while len(mysql_install_dir.strip())==0:         buffer_pool_size=input('MySQL安装文件路径:')      datadir=input('MySQL数据文件路径:')    while len(datadir.strip())==0:         buffer_pool_size=input('MySQL数据文件路径:')         mysql_port=input('MySQL监听端口:')    while len(mysql_port.strip())==0 or mysql_port.isdigit()==False:         mysql_port=input('MySQL监听端口:')    expire_days=input('日志保留天数:')    while len(expire_days.strip())==0 or expire_days.isdigit()==False:         expire_days=input('日志保留天数:')    cpu_cores = get_cpu()    innodb_buffer_pool_size=get_meminfo()/1024/1024/1024*0.8    local_ip=get_ip()    server_id=local_ip+mysql_port    auto_mycnf(mysql_port,mysql_install_dir,datadir,str(int(innodb_buffer_pool_size)),expire_days,cpu_cores,local_ip)

 

转载地址:http://czhji.baihongyu.com/

你可能感兴趣的文章
SIGN UP BEC2
查看>>
S3C2440中对LED驱动电路的理解
查看>>
《天亮了》韩红
查看>>
Windows CE下USB摄像头驱动开发(以OV511为例,附带全部源代码以及讲解) [转]
查看>>
出现( linker command failed with exit code 1)错误总结
查看>>
iOS开发中一些常见的并行处理
查看>>
iOS获取手机的Mac地址
查看>>
ios7.1发布企业证书测试包的问题
查看>>
如何自定义iOS中的控件
查看>>
iOS 开发百问
查看>>
Mac环境下svn的使用
查看>>
github简单使用教程
查看>>
如何高效利用GitHub
查看>>
环境分支-git版本管理
查看>>
uni-app 全局变量
查看>>
js判断空对象的几种方法
查看>>
java 不用递归写tree
查看>>
springboot2 集成Hibernate JPA 用 声明式事物
查看>>
fhs-framework jetcache 缓存维护之自动清除缓存
查看>>
SpringBoot 动态编译 JAVA class 解决 jar in jar 的依赖问题
查看>>