##introduce## This my.cnf for MySQL 5.7## Server configure is CPU 40C,MEM 256G ,Disk SSD + HHD. U should modify this cnf according to your configure.## By aijie@jd.com#适用于所有客户端程序读取[client]port = 3306socket = /ssd_data/mysql/data/mysql.sock#适用于mysql客户端程序[mysql]no_auto_rehash # 当表多时,自动避免收集很多元数据信息,不能自动补全prompt = "\\R:\\m:\\s\\d>" #H:M:S:DBpager = "more" default-character-set = utf8mb4 [mysqld_safe] malloc-lib=tcmalloc # 使用tcmalloc的内存管理机制避免表多导致的information_schema库引起的OOM问题[mysqldump] quick #备份时逐行读取表而非一次全部行后缓冲在内存中,大表有效single-transaction # 一致性备份#适用于 mysqld 服务器[mysqld]##BASE_SETTING##port = 3306socket = /ssd_data/mysql/data/mysql.sockserver-id = 171109report_host = 10.17.1.109 # show slave status 中主机IPbasedir = /ssd_data/mysql datadir = /ssd_data/mysql/data tmpdir = /data/mysql/mysql_tmp user = mysql # 为避免用户--user=root向my.cnf文件添加选项ssl-mode = disabledinnodb_file_per_table = 1 # 独立表空间lower_case_table_names = 1 # 不区分大小写,库名、表名、表别名、变量名将以小写处理skip_external_locking = 1 # 默认禁用外部(系统)锁,只影响MyiSAM表访问,否则容易死锁character_set_server = utf8mb4 collation_server = utf8mb4_bin transaction-isolation = READ-COMMITTED # 设置RC隔离级别sysdate-is-now # SYSDATE()默认情况下返回它执行的时间,而不是它开始执行的语句的时间。sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION" # 老业务就按以前的配#sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"#explicit_defaults_for_timestamp = 1 # 明确timestamp默认值,新业务启用,老业务继承#optimizer_switch = 'index_merge= off' ##CONNECTION##interactive_timeout = 1800 # 影响 wait_timeout参数wait_timeout = 1800 # 服务器在关闭之前等待非交互式连接上的活动的秒数,默认28800lock_wait_timeout = 1800 # 指定尝试获取元数据锁定的超时时间skip_name_resolve = 1 # 不解析客户端连接主机名,,授权表只用IP地址或localhostmax_connections = 1500 # 最大连接数max_allowed_packet = 64M # 数据包最大大小#max_user_connections = 256 # 同一用户最大连接数,适合于so的多源从库max_connect_errors = 1000000##ENGINE##default_storage_engine = InnoDB # 默认存储引擎,默认innodbdisabled_storage_engines = ARCHIVE,BLACKHOLE,EXAMPLE,FEDERATED,MEMORY,MERGE,NDB #禁用其他存储引擎innodb_page_size = 16384innodb_buffer_pool_size = 160G # buffer pool大小innodb_buffer_pool_instances = 16 # 将缓冲池划分区域,较少池内争用innodb_buffer_pool_load_at_startup = 1 # MySQL服务器关闭时记录InnoDB 缓冲池中缓存的页面 innodb_buffer_pool_dump_at_shutdown = 1 # MySQL服务器启动时加载缓存的Innodb页面innodb_flush_log_at_trx_commit = 1 # 默认1,0提交时不写日志,每秒写日志且刷盘,1提交时写日志并刷盘,2提交时写日志,每秒刷盘innodb_max_dirty_pages_pct = 75 # 脏页比例innodb_lru_scan_depth = 4096 # lru扫描力度innodb_lock_wait_timeout = 5 # 行锁等待时间,默认50s,等待超时当前语句回滚而事务不会滚innodb_data_file_path = ibdata1:1G:autoextend # 共享表空间数目和大小innodb_undo_tablespaces = 3 # 5.7.21弃用innodb_undo_logs = 128 # undo 段innodb_undo_log_truncate = 1 # 开启undo清理innodb_max_undo_log_size = 3G # undo清理阈值innodb_purge_rseg_truncate_frequency= 128 # 默认128,清理undo log频率innodb_log_files_in_group = 3 # redo log文件个数innodb_log_file_size = 8G # redo log文件大小innodb_log_buffer_size = 64M # 日志缓冲区innodb_io_capacity = 4000 # IO性能,SSD raid10在2w innodb_io_capacity_max = 8000 # IO 最大性能innodb_flush_method = O_DIRECT # 默认null,使用O_DIRECT打开数据文件,并fsync刷新数据innodb_large_prefix = 1 # InnoDB使用DYNAMIC 或 COMPRESSED 行格式的表 ,索引键前缀长度超过767个字节innodb_print_all_deadlocks = 1 # 默认1,将死锁信息打印到error loginnodb_sort_buffer_size = 32M # 用于在创建InnoDB索引期间对数据进行排序的排序缓冲区的大小innodb_thread_concurrency = 48 # Innodb尝试保持小于此值得线程数,超过将处于搁置等待状态innodb_purge_threads = 4 # purge线程数目,默认4,减少内部征用,提高多表DML操作效率innodb_read_io_threads = 20 # 读线程数目innodb-write-io-threads = 20 # 写线程数目innodb_stats_persistent_sample_pages= 64 # 默认20,统计信息抽样的索引页的数量,增大提高explain准确性innodb_autoinc_lock_mode = 2 # 自增锁锁定模式,默认1对于insert bluk持有读取执行释放,2持有读取释放执行sqlinnodb_online_alter_log_max_size = 1G # 默认128M,DDL期间临时日志文件大小innodb_open_files = 4096 # 一次可以保持打开最大ibd文件数量innodb_stats_on_metadata = 0 # 默认0,保持禁用提高具有大量数据表或索引的访问速度loose_innodb_numa_interleave = 1 #V5.7.9需在numa开启的linux编译MySQLinnodb_buffer_pool_dump_pct = 40innodb_page_cleaners = 20#innodb_strict_mode = 1 # 启用Innodb严格模式,返回错误而不是警告,新业务开启##REPLICATION##relay_log_info_repository = TABLE # 将复制信息存于表中,更安全和容易读取master_info_repository = TABLE sync_binlog = 1 relay_log = /data/mysql/mysql_relay # 设置relay log名称,放在HHD,刷盘不影响SSD性能relay_log_recovery = 1 # 开启新中继日志,从SQL执行位置重新拉取,清理老relay logsync_relay_log = 1 # relay log的刷盘方式,0依赖OS,n,n个事件写入relay log后刷盘relay_log_purge = 1 # relay log 的清理,不然relay_log_recovery不生效read_only = 0 skip_slave_start = 1 # 避免从库slave线程自动启动slave_net_timeout = 3600 # 主从断开后,从库重新连接主库等待事件#slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'##parallel##slave_parallel_type = LOGICAL_CLOCK #基于组提交的并行复制方式slave_parallel_workers = 16 # 16个并发线程#slave_preserve_commit_order = 1 #和主句的提交顺序相同#slave_transaction_retries = 128 #并行意味着有锁的问题,锁超时之后,事物回滚前,尝试再次执行128次##GTID###gtid_mode = ON # 开启GTID#enforce_gtid_consistency = 1 # GTID强一致#binlog_gtid_simple_recovery = 1 # 5.7.7 之后默认值为on,只从最新的最老的binlogflie中计算得到gtid_purged and gtid_executed的值。#gtid-executed-compression-period = 1000 #默认1000,gtid_execute压缩周期##SEMISYNC###plugin_load = "rpl_semi_sync_master= semisync_master.so;rpl_semi_sync_slave= #semisync_slave.so" #rpl_semi_sync_master_wait_point = AFTER_SYNC #5.7默认模式,5.6AFTER_COMMIT#rpl_semi_sync_master_enabled = 1 # 启用主库半同步插件#rpl_semi_sync_slave_enabled = 0 # 启用从库半同步插件#rpl_semi_sync_master_timeout = 300000 # 从半同步转为异步超时时间,单位ms5min#rpl_semi_sync_master_wait_no_slave = 1 # 默认1,表示从库数目低于rpl_semi_sync_master_wait_for_slave_count 主库不等待#rpl_semi_sync_master_wait_for_slave_count = 1 # N个slave接收到relay log 发回ack的个数##REPLICATION_FILTER##replicate-do-db = so1 # 复制过滤,只复制哪个DB,多库写多条replicate-do-db = so2replicate-do-db = so3replicate-do-db = so4replicate-do-db = so5replicate-do-db = so6replicate-ignore-table = so1.so_item_history_bak # 复制过滤,复制忽略哪个表,多表多条replicate-ignore-table = so1.so_history_bakreplicate-ignore-table = so1.so_item_history_bakreplicate-ignore-table = so1.so_history_bakreplicate-ignore-table = so1.so_item_history_bakreplicate-ignore-table = so1.so_history_bakreplicate-ignore-table = so1.so_item_history_bakreplicate-ignore-table = so1.so_history_bakreplicate-ignore-table = so1.so_item_history_bakreplicate-ignore-table = so1.so_history_bakreplicate-ignore-table = so1.so_item_history_bakreplicate-ignore-table = so1.so_history_bak##新增PS参数###配置一个mem计数工具,控制PS库使用的内存上线以及SQL的显示宽度#performance-schema-instrument ='memory/%=COUNTED' performance_schema_digests_size = 40000performance_schema_max_table_instances = 40000performance_schema_max_sql_text_length = 4096performance_schema_max_digest_length = 4096##CACHE##key_buffer_size = 1G # MyiSAM索引块缓冲,所有线程共享myisam_sort_buffer_size = 128M # MyiSAM表:REPAIR TABLE对索引排序、Create index、Alter index时的缓冲区 max_heap_table_size = 64M # 新建MEMORY表允许增长的最大大小 ##TABLE_CACHE_PERFORMANCE_SETTINGS## open_files_limit = 65535 # 操作系统允许mysqld打开的文件数量 table_open_cache = 4096 # 默认2000,所有线程的打开表的数量 # 如果值Opened_tables较大,应增加此值,参考值max_connections*查询用到表数table_definition_cache = 4096 # frm表可被缓存数量。400+(table_open_cache / 2),上限2000table_open_cache_instances = 64 # 打开表缓存实例的数量,减少争用# session memory settings # read_buffer_size = 16M # 线程为顺序扫描的MyiSAM表分配的缓冲区read_rnd_buffer_size = 32M # MyiSAM表读取数据缓冲区sort_buffer_size = 32M # 会话级别排序缓冲区tmp_table_size = 64M # 内存临时表的大小tmp_table_size和max_heap_table_size中较小决定 join_buffer_size = 128M # 会话级别表连接的空间thread_cache_size = 64 # 应缓存多少线程复用,当线程数小于此值就会被缓存# log settings ###Error log##log_error = error.loglog_error_verbosity = 2 # 5.7.2 添加优先于log-warnings,记录错误1、警告2、注释3##binlog##log-bin = /ssd_data/mysql/arch/mysql-binbinlog_rows_query_log_events = 1 # 在binlog中记录原生SQLlog_bin_trust_function_creators = 1 # 默认关闭,将function写入binlog,避免默认情况下回阻止function的创建binlog_format = row # 行模式复制max_binlog_size = 1G # binlog文件大小binlog_error_action = ABORT_SERVER # 默认ABORT_SERVER,当无法写入刷新同步二进制日志时,关闭二进制日志,建议support_xa开启#expire_logs_days = 90 # 使用脚本进行删除#log_slave_updates = 1 # 执行relay log,并生成binlog,用于级联 ##log format##log-output = TABLE,FILE # slow log 和general log 的输出方式, 需要启用general_log_file和 slow_query_log_filegeneral_log_file = general.log log_timestamps = system # 默认UTC,控制写入错误日志的消息中的时间戳的时区 ##Slow log##slow_query_log = 1 # 开启慢查询slow_query_log_file = slow.log # 慢查询日志名称log_throttle_queries_not_using_indexes = 10 # 限制每分钟10条not_using_indexeslog_queries_not_using_indexes = 1 # 记录不使用索引的SQL到慢日志long_query_time = 2 # 慢查询时间阈值log_slow_admin_statements = 1 # 记录管理性慢语句log_slow_slave_statements = 1 # 在slave记录超过long_query_time到从库慢日志min_examined_row_limit = 100 # 检查少于此行数的查询不会记录到慢速查询日志中。 #CLIENT_DEPRECATE_EOF session_track_schema = 1 # 是否跟踪当前会话中对默认模式(数据库)名称的更改,并在发生更改时使此信息可供客户端使用session_track_state_change = 1 # 控制服务器是否跟踪对当前会话状态的更改,并在更改时通知客户端#session_track_system_variables = '*' # 控制服务器是否跟踪对会话系统变量的更改,并在发生更改时将此信息提供给客户端##MGR_Setting###plugin-load = "group_replication.so;" # validate_password.so#loose_group_replication_start_on_boot = 0 # 在mysqld启动时不自动启动组复制#group_replication_single_primary_mode = off # 默认单主模式,单主还是多主模式#binlog_checksum = NONE # 默认CRC32,only for group replication,#transaction-write-set-extraction = XXHASH64 # MGR中开启主键信息采集功能#loose_group_replication = FORCE_PLUS_PERMANENT#loose_group_replication_group_name = "aaaa-aaaa-aaaa-aaaa-aaaa" # 此服务器实例所属的组的名称。必须是有效的UUID#loose-group_replication_local_address = 'node2:33061' # 本地成员的地址,node2+port,节点之间通过这个端口号进行通信#loose-group_replication_group_seeds = "127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903" #loose_group_replication_compression_threshold = 100#loose_group_replication_flow_control_mode = 0#loose_group_replication_single_primary_mode = 0#loose_group_replication_transaction_size_limit = 10485760#loose_group_replication_enforce_update_everywhere_checks = 1 #单主关闭,多主开启,多主模式限制检测#loose_group_replication_unreachable_majority_timeout = 120## 弃用###innodb_locks_unsafe_for_binlog # 5.6.3弃用#metadata_locks_cache_size # 5.7.4 MDL锁定实现变化弃用#metadata_locks_hash_instances = 64 # 5.7.4弃用#innodb_support_xa # 5.7.10 弃用#query_cache_size = 0 # 查询缓存大小#query_cache_type = 0 # MySQL 5.7.20弃用。0不缓存结果,关闭查询缓存,1全开,2部分开#MyISAM表维护实用程序[myisamchk]key_buffer_size = 20Msort_buffer_size = 20Mread_buffer = 2Mwrite_buffer = 2M
部分选项详细解释
binlog_error_action= ABORT_SERVER -- 在MySQL 5.7.7和更高版本中,这个变量默认为 ABORT_SERVER,这会使服务器停止日志记录,并在二进制日志遇到这样的错误时关闭。在服务器重新启动时,所有先前准备好的和二进制记录的事务都被提交,而任何已准备好但没有由于错误而被二进制记录的事务被中止。-- 应该配合xa参数binlog_rows_query_log_events= 1 --将原生SQL以注释方式写入master-info-repository=TABLE relay-log-info-repository=TABLE 当从库意外停止后,重新启动需要一些信息:I/O线程接收到多少事务,SQL线程执行了哪些事务,默认这些信息存储在file中,但这样有因为从库执行事务不同阶段失去和主机同步的风险,甚至文件损坏等情况。 设置 relay_log_info_repository和 master_info_repository到 TABLE。 服务器将在mysql.slave_master_info表中恢复I / O线程所需的信息以及在mysql.slave_relay_log_info表中恢复SQL线程所需的信息 sync_binlog=1relay_log_recovery=1--当服务器启动立即自动恢复中继日志,过程是:1创建新中继日志,SQL线程位置初始化为此中继日志(Relay_Log_File + Relay_Log_Pos ),2IO线程初始化为SQL线程位置并开始拉取日志。--当revovery启动,relay_log_purge关闭,可能造成从未清除文件中读取中继日志