`

MariaDB配置文件my.cnf

 
阅读更多
# MariaDB database server configuration file.
#
# You can copy this file to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
[client]
port		= 3306				#客户端连接通信端口
socket		= /var/run/mysqld/mysqld.sock	#socket文件是linux/unix系统特有的,用户在该环境下的客户端连接可以不通过tcp/ip网络,而直接使用socket文件连接
default-character-set = utf8mb4			#客户端默认连接字集集,若编译安装时已指定则不用填写

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket		= /var/run/mysqld/mysqld.sock
nice		= 0

[mysqld]
#
# * Basic Settings
#
character-set-client-handshake = FALSE
character-set-server = utf8mb4			#服务器和数据库默认字符集
collation-server = utf8mb4_unicode_ci		#客户端校正字符集
init_connect = 'SET NAMES utf8mb4'


user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid	#服务端pid进程文件,若丢失则重启Mysql重新生成,若重启失败,则可能由于mysqld进程未杀死,用pkill mysql后则能重启成功Mysql
socket		= /var/run/mysqld/mysqld.sock	#服务端通信文件,若丢失则重启Mysql重新生成,若重启失败,则可能由于mysqld进程未杀死,用pkill mysql后则能重启成功Mysql
port		= 3306				#Mysql服务端监听端口
basedir		= /usr				#Mysql软件目录
datadir		= /mnt/data/mysql		#Mysql数据库存放目录
tmpdir		= /tmp				#Mysql保存临时文件,如基于磁盘的大型排序,简单的临时表
lc_messages_dir	= /usr/share/mysql
lc_messages	= en_US
skip-external-locking				#防止外部锁定表,允许外部文件级别的锁. 打开文件锁会对性能造成负面影响
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address		= 112.124.37.33		#服务器多个IP时写上比较重要,只有被绑定的IP地址才能接收客户端的连接请求
#
# * Fine Tuning #微调
#
# 并发线程数,建议为CPU核心数乘以2:  CPU's*2 for thread_concurrency
#thread_concurrency = 8 			#属重点优化参数,该参数在MariaDB默认配置文件里未出现

max_connections         = 5000			#指定mysql服务所允许的最大连接进程数	
#connect_timeout         = 5
wait_timeout            = 600			#SQL语句最大执行时间
max_allowed_packet      = 16M			#设定在网络传输中一次可以传输消息的最大值,系统默认为1M,最大可以是1G
thread_cache_size       = 256			#设置threadcache池中可以缓存连接线程的最大数量,默认为0,该值表示可以重新利用保存在缓存中线程的数量,当断开连接时若缓存中还有空间,那么客户端的线程将被放到缓存中,如果线程重新被请求,那么请求将从缓存中读取,若果缓存中是空的或者是新的请求,那么线程将被重新创建。设置规律为:1G内存设置为8,2G内存设置为16,4G以上设置为64,如果有很多新的线程,增加这个值可以改善系统性能.通过比较 Connections 和 Threads_created 状态的变量,可以看到这个变量的作用
sort_buffer_size        = 4M			#排序缓冲区用来处理类似orderby以及groupby队列所引起的排序,系统默认大小为2M,该参数对应分配内存是每个连接独占的,若有100个连接,实际分配的排序缓冲区大小为6*100;推荐设置为6M-8M,并不是越大越好,由于是connection级的参数,过大的设置+高并发可能会耗尽系统内存资源。例如:500个连接将会消耗 500*sort_buffer_size(8M)=4G内存,超过2KB的时候,就会使用mmap() 而不是 malloc() 来进行内存分配,导致效率降低。属重点优化参数。
bulk_insert_buffer_size = 16M			#批量插入数据缓存大小,可以有效提高插入效率,默认为8M
tmp_table_size          = 64M			#内存临时表的大小,如果超过该值,会将临时表写入磁盘
max_heap_table_size     = 64M
#
# * MyISAM
#
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched. On error, make copy and try a repair.
myisam_recover          = BACKUP		#自动检查和修复没有适当关闭的 MyISAM 表
key_buffer_size         = 256M			#指定索引缓冲区的大小,它决定了数据库索引处理的速度,尤其是索引读的速度,内存为4G时设置为256M或384M
#open-files-limit       = 2000			#每个进程最多可打开的文件数,确认你已经将全系统限制设定的足够高,打开大量表需要将此值设大
table_open_cache        = 400			#参数设置表高速缓存的数目。每个连接进来,都会至少打开一个表缓存。因此table_cache 的大小应与 max_connections 的设置有关。对于 200 个#并行运行的连接,应该让表的缓存至少有 200 × N ,这里 N 是应用可以执行的查询#的一个联接中表的最大数量。此外,还需要为临时表和文件保留一些额外的文件描述符。
#当 Mysql 访问一个表时,如果该表在缓存中已经被打开,则可以直接访问缓存;如果#还没有被缓存,但是在 Mysql 表缓冲区中还有空间,那么这个表就被打开并放入表缓#冲区;如果表缓存满了,则会按照一定的规则将当前未用的表释放,或者临时扩大表缓存来存放,使用表缓存的好处是可以更快速地访问表中的内容。执行 flush tables 会#清空缓存的内容。一般来说,可以通过查看数据库运行峰值时间的状态值 Open_tables #和 Opened_tables ,判断是否需要增加 table_cache 的值(其中 open_tables 是当#前打开的表的数量, Opened_tables 则是已经打开的表的数量)。即如果open_tables接近table_cache的时候,并且Opened_tables这个值在逐步增加,那就要考虑增加这个#值的大小了。还有就是Table_locks_waited比较高的时候,也需要增加table_cache。
myisam_sort_buffer_size = 1024M			#MyISAM表发生变化时重新排序所需的缓冲
concurrent_insert       = 2
read_buffer_size        = 8M			#用来做MYISAM表全表扫描的缓冲大小,对表进行顺序扫描的请求将分配一个读入缓冲区,MySql会为它分配一段内存缓冲区。read_buffer_size变量控制这一缓冲区的大小。如果对表的顺序扫描请求非常频繁,并且你认为频繁扫描进行得太慢,可以通过增加该变量值以及内存缓冲区大小提高其性能。
read_rnd_buffer_size    = 8M			#随机读(查询操作)缓冲区大小。当按任意顺序读取行时(例如,按照排序顺序),将分配一个随机读缓存区。进行排序查询时,MySql会首先扫描一遍该缓冲,以避免磁盘搜索,提高查询速度,如果需要排序大量数据,可适当调高该值。但MySql会为每个客户连接发放该缓冲空间,所以应尽量适当设置该值,以避免内存开销过大。
#
# * Query Cache Configuration
#
# Cache only tiny result sets, so we can fit more in the query cache.
query_cache_limit               = 512K		#只有小于该值的结果才被缓冲,放置一个极大的结果将其他所有的查询结果都覆盖
query_cache_size                = 256M		#指定mysql查询缓冲区的大小,用来缓冲select的结果,并在下一次同样查询的时候不再执行查询而直接返回结果,根据Qcache_lowmem_prunes的大小,来查看当前的负载是否足够高,在数据库写入量或是更新量也比较大的系统,该参数不适合分配过大。而且在高并发,写入量大的系统,建议把该功能禁掉。属重点优化参数(主库 增删改-MyISAM)
# for more write intensive setups, set to DEMAND or OFF
#query_cache_type		= DEMAND	#如果是密集写入的,query_cahce已经失去意义,所以需要设置为OFF	
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.
#
# we do want to know about network errors and such
log_warnings		= 2					#将警告打印输出到错误log文件. 如果你对于MySQL有任何问题,你应该打开警告log并且仔细审查错误日志,查出可能的原因。
#
# Enable the slow query log to see queries with especially long duration
slow_query_log=1						#慢查询日志开关
#slow_query_log[={0|1}]
slow_query_log_file	= /var/log/mysql/mariadb-slow.log	#慢查询日志文件
long_query_time = 1						#慢查询时间
#log_slow_rate_limit	= 1000
log_slow_verbosity	= query_plan

#log-queries-not-using-indexes
#log_slow_admin_statements
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id		= 1					#唯一的服务辨识号,数值位于 1 到 2^32-1之间,在master和slave上都需要设置,如果 “master-host” 没有被设置,则默认为1, 但是如果忽略此选项,MySQL不会作为master生效
#report_host		= master1
#auto_increment_increment = 2
#auto_increment_offset	= 1
log_bin			= /var/log/mysql/mariadb-bin		#打开二进制日志功能
log_bin_index		= /var/log/mysql/mariadb-bin.index
# not fab for performance, but safer
#sync_binlog		= 1
expire_logs_days	= 10
max_binlog_size         = 100M
# slaves
#relay_log		= /var/log/mysql/relay-bin
#relay_log_index	= /var/log/mysql/relay-bin.index
#relay_log_info_file	= /var/log/mysql/relay-bin.info
#log_slave_updates						#在使用链式从服务器结构的复制模式 (A->B->C),你需要在服务器B上打开此项。此选项打开在从线程上重做过的更新的日志,并将其写入从服务器的二进制日志。
#read_only							#使得slave只读.只有用户拥有SUPER权限和在上面的slave线程能够修改数据,你可以使用此项去保证没有应用程序会意外的修改slave而不是master上的数据。
#
# If applications support it, this stricter sql_mode prevents some
# mistakes like inserting invalid dates etc.
#sql_mode		= NO_ENGINE_SUBSTITUTION,TRADITIONAL
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
default_storage_engine	= InnoDB		#创建表时默认使用的存储引擎
# you can't just change log file size, requires special procedure
#innodb_log_file_size	= 50M
innodb_buffer_pool_size	= 256M			#这对Innodb表来说非常重要。Innodb相比MyISAM表对缓冲更为敏感。MyISAM可以在默认的 key_buffer_size 设置下运行的可以,然而Innodb在默认的 innodb_buffer_pool_size 设置下却跟蜗牛似的。由于Innodb把数据和索引都缓存起来,无需留给操作系统太多的内存,因此如果只需要用Innodb的话则可以设置它高达 70-80% 的可用内存。一些应用于 key_buffer 的规则有 — 如果你的数据量不大,并且不会暴增,那么无需把 innodb_buffer_pool_size 设置的太大了。不要设置过大,否则,由于物理内存的竞争可能导致操作系统的换页颠簸。innodb_buffer_pool_size = 6G
innodb_log_buffer_size	= 8M			#此参数确定写日志文件所用的内存大小,以M为单位。缓冲区更大能提高性能,但意外的故障将会丢失数据,由于基本上每秒都会刷新一次,所以没有必要将此值设置的太大(甚至对于长事务而言),MySQL开发人员建议设置为1-8M之间
innodb_file_per_table	= 1
innodb_open_files	= 400
innodb_io_capacity	= 400
innodb_flush_method	= O_DIRECT		#InnoDB用来刷新日志的方法,表空间总是使用双重写入刷新方法,默认值是 “fdatasync”, 另一个是 “O_DSYNC”
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem



[mysqldump]
quick						#不要在将内存中的整个结果写入磁盘之前缓存. 在导出非常巨大的表时需要此项
quote-names
max_allowed_packet	= 16M			#设定在网络传输中一次可以传输消息的最大值,系统默认为1M,最大可以是1G

[mysql]
#no-auto-rehash	# faster start of mysql but no tab completition
default-character-set = utf8mb4

[isamchk]
key_buffer		= 16M

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics