Skip to content

Instantly share code, notes, and snippets.

@jonathanvx
Created February 7, 2017 13:02
Show Gist options
  • Save jonathanvx/ac3f7824b7c415d0f754bf744d312ecc to your computer and use it in GitHub Desktop.
Save jonathanvx/ac3f7824b7c415d0f754bf744d312ecc to your computer and use it in GitHub Desktop.
Self correcting my.cnf for MySQL
#!/bin/bash
(cat <<EOF
[mysqld]
## Performance Specific ##
##########################
innodb_buffer_pool_size = 12G ### How much innodb data to store in memory. Higher = faster performance
innodb_file_per_table = 1 ### Each innodb table is its own file on disk.
innodb_flush_log_at_trx_commit = 2 ### Flushes to disk in batches instead of per change
innodb_io_capacity = 800 ### Min IO speed expected to write to disk.
read_buffer_size = 2M ### Helps with reading temp tables faster, bulk inserts and nested queries
read_rnd_buffer_size = 8M ### Helps with multi range queries and ORDER Bys.
sort_buffer_size = 8M ### Helps with ORDER BY queries, which we have a lot of.
table_open_cache = 4096 ### Cache data about open tables. Saves reopening tables.
thread_cache_size = 256 ### How many threads to cache. Can help reduce thread count.
thread_stack = 256k ### How much cache each thread can have. Higher = more complicated results are cached
tmp_table_size = 64M ### Max size a tmp table can be.
innodb_flush_method = O_DIRECT ## Innodb flushes to table files directly
innodb_log_file_size = 512M ##
innodb_log_files_in_group = 3 ##
innodb_read_io_threads = 8 ## innodb background threads for reads. Helps with reads
join_buffer_size = 2M ## Helps when a join doesn’t have an index or an index wont be more helpful than a full table scan
max_heap_table_size = 64M ## Max size a tmp table can be in memory
query_cache_limit = 2M ##
query_cache_min_res_unit = 2048 ##
query_cache_size = 196M ##
query_cache_type = 1 ## Caches complete query results. Data gets invalidated when underlying tables are changed.
table_open_cache_instances = 8 ## Divides the table cache to reduce contention
table-definition-cache = 4096 ## Stores table definitions - .frm files
key_buffer_size = 16M # As there are no MyISAM tables, this variable helps with MyISAM temp tables.
back_log = 1500 # When MySQL gets a lot of connections in a short time, it will keep them in a back log
innodb_purge_threads = 2 # May reduce locks for when updates/deletes are run often
innodb_sort_buffer_size = 2M # Helps with index changes and create table statements
#innodb_page_cleaners = 4 # Will help with start up and shutdown in 5.7
binlog_cache_size = 128K # May speed up transactions
binlog_stmt_cache_size = 1M # May speed up transactions
## General ##
#############
port=3306
default-storage-engine = InnoDB
innodb_file_format = Barracuda # The newer version of Innodb.
innodb_open_files = 131072 # Max number of open files innodb can have - Safety feature
interactive_timeout = 28800
max_allowed_packet = 64M # Current setting. Need to evaluate if can go down to 16M
max_connections = 780 # Maximum number of connections the server can have before not allowing anymore
#max_user_connections
max-connect-errors = 1000000
min_examined_row_limit = 1 # Slow log setting
open-files-limit = 65535
sql_mode = NO_ENGINE_SUBSTITUTION
sync_binlog = 0 # Moves to commiting in batches instead of per command
wait_timeout = 28800
## Existing Replication Settings ##
###################################
EOF
) > my.cnf
mysql -e'show variables where variable_name like "server_id" or variable_name like "log_bin%" or variable_name like "relay_log%" or variable_name like "log_slave%" or variable_name like "%info%" or variable_name like "auto_increment%" or variable_name like "expire_logs_days" or variable_name like "binlog_format";' -NB | sed 's/\t/ = /g' | sed 's/ON/1/g' | sed 's/OFF/0/g' | grep -ve " = $" | egrep -v '(sync|recovery|space|purge)' | grep -v 'relay_log = ' | grep -v 'log_bin = ' | sed 's/_basename//g' >>my.cnf
(cat <<EOF
## UTF-8 compatibility ##
#########################
character-set-server = utf8
init_connect = 'SET collation_connection = utf8_general_ci'
character-set-server = utf8
collation-server = utf8_general_ci
init-connect = 'SET NAMES utf8'
[client]
#loose-default-character-set = utf8
[mysqld_safe]
EOF
) >> my.cnf
less my.cnf
echo 'Created my.cnf file in current directory'
echo "to use your new config file on this server, run: cp my.cnf /etc/my.cnf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment