如何设置合理的mysql的参数?

如何设置合理的mysql的参数?,第1张

[client]\x0d\x0aport = 3306\x0d\x0asocket = /tmp/mysql.sock\x0d\x0a[mysqld]\x0d\x0aport = 3306\x0d\x0asocket = /tmp/mysql.sock\x0d\x0a\x0d\x0abasedir = /usr/local/mysql\x0d\x0adatadir = /data/mysql\x0d\x0apid-file = /data/mysql/mysql.pid\x0d\x0auser = mysql\x0d\x0abind-address = 0.0.0.0\x0d\x0aserver-id = 1 #表示是本机的序号为1,一般来讲就是master的意思\x0d\x0a\x0d\x0askip-name-resolve\x0d\x0a# 禁止MySQL对外部连接进行DNS解析,使用这一选项可以消除MySQL进行DNS解析的时间。但需要注意,如果开启该选项,\x0d\x0a# 则所有远程主机连接授权都要使用IP地址方式,否则MySQL将无法正常处理连接请求\x0d\x0a\x0d\x0a#skip-networking\x0d\x0a\x0d\x0aback_log = 600\x0d\x0a# MySQL能有的连接数量。当主要MySQL线程在一个很短时间内得到非常多的连接请求,这就起作用,\x0d\x0a# 然后主线程花些时间(尽管很短)检查连接并且启动一个新线程。back_log值指出在MySQL暂时停止回答新请求之前的短时间内多少个请求可以被存在堆栈中。\x0d\x0a# 如果期望在一个短时间内有很多连接,你需要增加它。也就是说,如果MySQL的连接数据达到max_connections时,新来的请求将会被存在堆栈中,\x0d\x0a# 以等待某一连接释放资源,该堆栈的数量即back_log,如果等待连接的数量超过back_log,将不被授予连接资源。\x0d\x0a# 另外,这值(back_log)限于您的 *** 作系统对到来的TCP/IP连接的侦听队列的大小。\x0d\x0a# 你的 *** 作系统在这个队列大小上有它自己的限制(可以检查你的OS文档找出这个变量的最大值),试图设定back_log高于你的 *** 作系统的限制将是无效的。\x0d\x0a\x0d\x0amax_connections = 1000\x0d\x0a# \x0d\x0aMySQL的最大连接数,如果服务器的并发连接请求量比较大,建议调高此值,以增加并行连接数量,当然这建立在机器能支撑的情况下,因为如果连接数越多,\x0d\x0a介于MySQL会为每个连接提供连接缓冲区,就会开销越多的内存,所以要适当调整该值,不能盲目提高设值。可以过'conn%'通配符查看当前状态的连接\x0d\x0a数量,以定夺该值的大小。\x0d\x0a\x0d\x0amax_connect_errors = 6000\x0d\x0a# 对于同一主机,如果有超出该参数值个数的中断错误连接,则该主机将被禁止连接。如需对该主机进行解禁,执行:FLUSH HOST。\x0d\x0a\x0d\x0aopen_files_limit = 65535\x0d\x0a# MySQL打开的文件描述符限制,默认最小1024当open_files_limit没有被配置的时候,比较max_connections*5和ulimit -n的值,哪个大用哪个,\x0d\x0a# 当open_file_limit被配置的时候,比较open_files_limit和max_connections*5的值,哪个大用哪个。\x0d\x0a\x0d\x0atable_open_cache = 128\x0d\x0a# MySQL每打开一个表,都会读入一些数据到table_open_cache缓存中,当MySQL在这个缓存中找不到相应信息时,才会去磁盘上读取。默认值64\x0d\x0a# 假定系统有200个并发连接,则需将此参数设置为200*N(N为每个连接所需的文件描述符数目);\x0d\x0a# 当把table_open_cache设置为很大时,如果系统处理不了那么多文件描述符,那么就会出现客户端失效,连接不上\x0d\x0a\x0d\x0amax_allowed_packet = 4M\x0d\x0a# 接受的数据包大小;增加该变量的值十分安全,这是因为仅当需要时才会分配额外内存。例如,仅当你发出长查询或MySQLd必须返回大的结果行时MySQLd才会分配更多内存。\x0d\x0a# 该变量之所以取较小默认值是一种预防措施,以捕获客户端和服务器之间的错误信息包,并确保不会因偶然使用大的信息包而导致内存溢出。\x0d\x0a\x0d\x0abinlog_cache_size = 1M\x0d\x0a# 一个事务,在没有提交的时候,产生的日志,记录到Cache中;等到事务提交需要提交的时候,则把日志持久化到磁盘。默认binlog_cache_size大小32K\x0d\x0a\x0d\x0amax_heap_table_size = 8M\x0d\x0a# 定义了用户可以创建的内存表(memory table)的大小。这个值用来计算内存表的最大行数值。这个变量支持动态改变\x0d\x0a\x0d\x0atmp_table_size = 16M\x0d\x0a# MySQL的heap(堆积)表缓冲大小。所有联合在一个DML指令内完成,并且大多数联合甚至可以不用临时表即可以完成。\x0d\x0a# 大多数临时表是基于内存的(HEAP)表。具有大的记录长度的临时表 (所有列的长度的和)或包含BLOB列的表存储在硬盘上。\x0d\x0a#\x0d\x0a \x0d\x0a如果某个内部heap(堆积)表大小超过tmp_table_size,MySQL可以根据需要自动将内存中的heap表改为基于硬盘的MyISAM表。\x0d\x0a还可以通过设置tmp_table_size选项来增加临时表的大小。也就是说,如果调高该值,MySQL同时将增加heap表的大小,可达到提高联接查\x0d\x0a询速度的效果\x0d\x0a\x0d\x0aread_buffer_size = 2M\x0d\x0a# MySQL读入缓冲区大小。对表进行顺序扫描的请求将分配一个读入缓冲区,MySQL会为它分配一段内存缓冲区。read_buffer_size变量控制这一缓冲区的大小。\x0d\x0a# 如果对表的顺序扫描请求非常频繁,并且你认为频繁扫描进行得太慢,可以通过增加该变量值以及内存缓冲区大小提高其性能\x0d\x0a\x0d\x0aread_rnd_buffer_size = 8M\x0d\x0a# MySQL的随机读缓冲区大小。当按任意顺序读取行时(例如,按照排序顺序),将分配一个随机读缓存区。进行排序查询时,\x0d\x0a# MySQL会首先扫描一遍该缓冲,以避免磁盘搜索,提高查询速度,如果需要排序大量数据,可适当调高该值。但MySQL会为每个客户连接发放该缓冲空间,所以应尽量适当设置该值,以避免内存开销过大\x0d\x0a\x0d\x0asort_buffer_size = 8M\x0d\x0a# MySQL执行排序使用的缓冲大小。如果想要增加ORDER BY的速度,首先看是否可以让MySQL使用索引而不是额外的排序阶段。\x0d\x0a# 如果不能,可以尝试增加sort_buffer_size变量的大小\x0d\x0a\x0d\x0ajoin_buffer_size = 8M\x0d\x0a# 联合查询 *** 作所能使用的缓冲区大小,和sort_buffer_size一样,该参数对应的分配内存也是每连接独享\x0d\x0a\x0d\x0athread_cache_size = 8\x0d\x0a# 这个值(默认8)表示可以重新利用保存在缓存中线程的数量,当断开连接时如果缓存中还有空间,那么客户端的线程将被放到缓存中,\x0d\x0a# 如果线程重新被请求,那么请求将从缓存中读取,如果缓存中是空的或者是新的请求,那么这个线程将被重新创建,如果有很多新的线程,\x0d\x0a# 增加这个值可以改善系统性能.通过比较Connections和Threads_created状态的变量,可以看到这个变量的作用。(_>表示要调整的值)\x0d\x0a# 根据物理内存设置规则如下:\x0d\x0a# 1G —>8\x0d\x0a# 2G —>16\x0d\x0a# 3G —>32\x0d\x0a# 大于3G —>64\x0d\x0a\x0d\x0aquery_cache_size = 8M\x0d\x0a#MySQL的查询缓冲大小(从4.0.1开始,MySQL提供了查询缓冲机制)使用查询缓冲,MySQL将SELECT语句和查询结果存放在缓冲区中,\x0d\x0a# 今后对于同样的SELECT语句(区分大小写),将直接从缓冲区中读取结果。根据MySQL用户手册,使用查询缓冲最多可以达到238%的效率。\x0d\x0a# 通过检查状态值'Qcache_%',可以知道query_cache_size设置是否合理:如果Qcache_lowmem_prunes的值非常大,则表明经常出现缓冲不够的情况,\x0d\x0a# 如果Qcache_hits的值也非常大,则表明查询缓冲使用非常频繁,此时需要增加缓冲大小;如果Qcache_hits的值不大,则表明你的查询重复率很低,\x0d\x0a# 这种情况下使用查询缓冲反而会影响效率,那么可以考虑不用查询缓冲。此外,在SELECT语句中加入SQL_NO_CACHE可以明确表示不使用查询缓冲\x0d\x0a\x0d\x0aquery_cache_limit = 2M\x0d\x0a#指定单个查询能够使用的缓冲区大小,默认1M\x0d\x0a\x0d\x0akey_buffer_size = 4M\x0d\x0a#指定用于索引的缓冲区大小,增加它可得到更好处理的索引(对所有读和多重写),到你能负担得起那样多。如果你使它太大,\x0d\x0a# 系统将开始换页并且真的变慢了。对于内存在4GB左右的服务器该参数可设置为384M或512M。通过检查状态值Key_read_requests和Key_reads,\x0d\x0a# 可以知道key_buffer_size设置是否合理。比例key_reads/key_read_requests应该尽可能的低,\x0d\x0a# 至少是1:100,1:1000更好(上述状态值可以使用SHOW STATUS LIKE 'key_read%'获得)。注意:该参数值设置的过大反而会是服务器整体效率降低\x0d\x0a\x0d\x0aft_min_word_len = 4\x0d\x0a# 分词词汇最小长度,默认4\x0d\x0a\x0d\x0atransaction_isolation = REPEATABLE-READ\x0d\x0a# MySQL支持4种事务隔离级别,他们分别是:\x0d\x0a# READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE.\x0d\x0a# 如没有指定,MySQL默认采用的是REPEATABLE-READ,ORACLE默认的是READ-COMMITTED\x0d\x0a\x0d\x0alog_bin = mysql-bin\x0d\x0abinlog_format = mixed\x0d\x0aexpire_logs_days = 30 #超过30天的binlog删除\x0d\x0a\x0d\x0alog_error = /data/mysql/mysql-error.log #错误日志路径\x0d\x0aslow_query_log = 1\x0d\x0along_query_time = 1 #慢查询时间 超过1秒则为慢查询\x0d\x0aslow_query_log_file = /data/mysql/mysql-slow.log\x0d\x0a\x0d\x0aperformance_schema = 0\x0d\x0aexplicit_defaults_for_timestamp\x0d\x0a\x0d\x0a#lower_case_table_names = 1 #不区分大小写\x0d\x0a\x0d\x0askip-external-locking #MySQL选项以避免外部锁定。该选项默认开启\x0d\x0a\x0d\x0adefault-storage-engine = InnoDB #默认存储引擎\x0d\x0a\x0d\x0ainnodb_file_per_table = 1\x0d\x0a# InnoDB为独立表空间模式,每个数据库的每个表都会生成一个数据空间\x0d\x0a# 独立表空间优点:\x0d\x0a# 1.每个表都有自已独立的表空间。\x0d\x0a# 2.每个表的数据和索引都会存在自已的表空间中。\x0d\x0a# 3.可以实现单表在不同的数据库中移动。\x0d\x0a# 4.空间可以回收(除drop table *** 作处,表空不能自已回收)\x0d\x0a# 缺点:\x0d\x0a# 单表增加过大,如超过100G\x0d\x0a# 结论:\x0d\x0a# 共享表空间在Insert *** 作上少有优势。其它都没独立表空间表现好。当启用独立表空间时,请合理调整:innodb_open_files\x0d\x0a\x0d\x0ainnodb_open_files = 500\x0d\x0a# 限制Innodb能打开的表的数据,如果库里的表特别多的情况,请增加这个。这个值默认是300\x0d\x0a\x0d\x0ainnodb_buffer_pool_size = 64M\x0d\x0a# InnoDB使用一个缓冲池来保存索引和原始数据, 不像MyISAM.\x0d\x0a# 这里你设置越大,你在存取表里面数据时所需要的磁盘I/O越少.\x0d\x0a# 在一个独立使用的数据库服务器上,你可以设置这个变量到服务器物理内存大小的80%\x0d\x0a# 不要设置过大,否则,由于物理内存的竞争可能导致 *** 作系统的换页颠簸.\x0d\x0a# 注意在32位系统上你每个进程可能被限制在 2-3.5G 用户层面内存限制,\x0d\x0a# 所以不要设置的太高.\x0d\x0a\x0d\x0ainnodb_write_io_threads = 4\x0d\x0ainnodb_read_io_threads = 4\x0d\x0a# innodb使用后台线程处理数据页上的读写 I/O(输入输出)请求,根据你的 CPU 核数来更改,默认是4\x0d\x0a# 注:这两个参数不支持动态改变,需要把该参数加入到my.cnf里,修改完后重启MySQL服务,允许值的范围从 1-64\x0d\x0a\x0d\x0ainnodb_thread_concurrency = 0\x0d\x0a# 默认设置为 0,表示不限制并发数,这里推荐设置为0,更好去发挥CPU多核处理能力,提高并发量\x0d\x0a\x0d\x0ainnodb_purge_threads = 1\x0d\x0a# InnoDB中的清除 *** 作是一类定期回收无用数据的 *** 作。在之前的几个版本中,清除 *** 作是主线程的一部分,这意味着运行时它可能会堵塞其它的数据库 *** 作。\x0d\x0a# 从MySQL5.5.X版本开始,该 *** 作运行于独立的线程中,并支持更多的并发数。用户可通过设置innodb_purge_threads配置参数来选择清除 *** 作是否使用单\x0d\x0a# 独线程,默认情况下参数设置为0(不使用单独线程),设置为 1 时表示使用单独的清除线程。建议为1\x0d\x0a\x0d\x0ainnodb_flush_log_at_trx_commit = 2\x0d\x0a# 0:如果innodb_flush_log_at_trx_commit的值为0,log buffer每秒就会被刷写日志文件到磁盘,提交事务的时候不做任何 *** 作(执行是由mysql的master thread线程来执行的。\x0d\x0a# 主线程中每秒会将重做日志缓冲写入磁盘的重做日志文件(REDO LOG)中。不论事务是否已经提交)默认的日志文件是ib_logfile0,ib_logfile1\x0d\x0a# 1:当设为默认值1的时候,每次提交事务的时候,都会将log buffer刷写到日志。\x0d\x0a# 2:如果设为2,每次提交事务都会写日志,但并不会执行刷的 *** 作。每秒定时会刷到日志文件。要注意的是,并不能保证100%每秒一定都会刷到磁盘,这要取决于进程的调度。\x0d\x0a# 每次事务提交的时候将数据写入事务日志,而这里的写入仅是调用了文件系统的写入 *** 作,而文件系统是有 缓存的,所以这个写入并不能保证数据已经写入到物理磁盘\x0d\x0a# 默认值1是为了保证完整的ACID。当然,你可以将这个配置项设为1以外的值来换取更高的性能,但是在系统崩溃的时候,你将会丢失1秒的数据。\x0d\x0a# 设为0的话,mysqld进程崩溃的时候,就会丢失最后1秒的事务。设为2,只有在 *** 作系统崩溃或者断电的时候才会丢失最后1秒的数据。InnoDB在做恢复的时候会忽略这个值。\x0d\x0a# 总结\x0d\x0a# 设为1当然是最安全的,但性能页是最差的(相对其他两个参数而言,但不是不能接受)。如果对数据一致性和完整性要求不高,完全可以设为2,如果只最求性能,例如高并发写的日志服务器,设为0来获得更高性能\x0d\x0a\x0d\x0ainnodb_log_buffer_size = 2M\x0d\x0a# 此参数确定些日志文件所用的内存大小,以M为单位。缓冲区更大能提高性能,但意外的故障将会丢失数据。MySQL开发人员建议设置为1-8M之间\x0d\x0a\x0d\x0ainnodb_log_file_size = 32M\x0d\x0a# 此参数确定数据日志文件的大小,更大的设置可以提高性能,但也会增加恢复故障数据库所需的时间\x0d\x0a\x0d\x0ainnodb_log_files_in_group = 3\x0d\x0a# 为提高性能,MySQL可以以循环方式将日志文件写到多个文件。推荐设置为3\x0d\x0a\x0d\x0ainnodb_max_dirty_pages_pct = 90\x0d\x0a# innodb主线程刷新缓存池中的数据,使脏数据比例小于90%\x0d\x0a\x0d\x0ainnodb_lock_wait_timeout = 120 \x0d\x0a# InnoDB事务在被回滚之前可以等待一个锁定的超时秒数。InnoDB在它自己的锁定表中自动检测事务死锁并且回滚事务。InnoDB用LOCK TABLES语句注意到锁定设置。默认值是50秒\x0d\x0a\x0d\x0abulk_insert_buffer_size = 8M\x0d\x0a# 批量插入缓存大小, 这个参数是针对MyISAM存储引擎来说的。适用于在一次性插入100-1000+条记录时, 提高效率。默认值是8M。可以针对数据量的大小,翻倍增加。\x0d\x0a\x0d\x0amyisam_sort_buffer_size = 8M\x0d\x0a# MyISAM设置恢复表之时使用的缓冲区的尺寸,当在REPAIR TABLE或用CREATE INDEX创建索引或ALTER TABLE过程中排序 MyISAM索引分配的缓冲区\x0d\x0a\x0d\x0amyisam_max_sort_file_size = 10G\x0d\x0a# 如果临时文件会变得超过索引,不要使用快速排序索引方法来创建一个索引。注释:这个参数以字节的形式给出\x0d\x0a\x0d\x0amyisam_repair_threads = 1\x0d\x0a# 如果该值大于1,在Repair by sorting过程中并行创建MyISAM表索引(每个索引在自己的线程内) \x0d\x0a\x0d\x0ainteractive_timeout = 28800\x0d\x0a# 服务器关闭交互式连接前等待活动的秒数。交互式客户端定义为在mysql_real_connect()中使用CLIENT_INTERACTIVE选项的客户端。默认值:28800秒(8小时)\x0d\x0a\x0d\x0await_timeout = 28800\x0d\x0a# 服务器关闭非交互连接之前等待活动的秒数。在线程启动时,根据全局wait_timeout值或全局interactive_timeout值初始化会话wait_timeout值,\x0d\x0a# 取决于客户端类型(由mysql_real_connect()的连接选项CLIENT_INTERACTIVE定义)。参数默认值:28800秒(8小时)\x0d\x0a# MySQL服务器所支持的最大连接数是有上限的,因为每个连接的建立都会消耗内存,因此我们希望客户端在连接到MySQL Server处理完相应的 *** 作后,\x0d\x0a# 应该断开连接并释放占用的内存。如果你的MySQL Server有大量的闲置连接,他们不仅会白白消耗内存,而且如果连接一直在累加而不断开,\x0d\x0a# 最终肯定会达到MySQL Server的连接上限数,这会报'too many connections'的错误。对于wait_timeout的值设定,应该根据系统的运行情况来判断。\x0d\x0a# 在系统运行一段时间后,可以通过show processlist命令查看当前系统的连接状态,如果发现有大量的sleep状态的连接进程,则说明该参数设置的过大,\x0d\x0a# 可以进行适当的调整小些。要同时设置interactive_timeout和wait_timeout才会生效。\x0d\x0a\x0d\x0a[mysqldump]\x0d\x0aquick\x0d\x0amax_allowed_packet = 16M #服务器发送和接受的最大包长度\x0d\x0a[myisamchk]\x0d\x0akey_buffer_size = 8M\x0d\x0asort_buffer_size = 8M\x0d\x0aread_buffer = 4M\x0d\x0awrite_buffer = 4M

mysql耗内存吗?很多人都说MySQL占用了很大的虚拟内存,那么这个问题应该怎么解决呢?下面是我收集整理的一些方法,现在分享给大家!

解决mysql耗内存的具体方法一:

在分析的过程中发现最耗内存的是MySQL,其中近1GB的内存被它吞了,而且不在任务管理器体现出来。这个数据库软件是EMS要用到了,所以必须要运行。这个软件在安装的时候会根据机器的实际内存自动进行配置,PC机物理内存越多,它默认占有的内存就越多,难怪3GB的内存被它给吞了近1GB。

优化方法:

1. 退出EMS client&server

2. 在CMD里运行:net stop mysql

3. 找到MySQL\MySQL Server的安装目录,里面有个my.ini文件,参考附件的配置对参数query_cache_size tmp_table_size myisam_sort_buffer_size key_buffer_size innodb_buffer_pool_size进行修改,注意不要改动innodb_log_file_size,修改前备份my.ini

4. 在CMD里运行:net start mysql,如果提示成功,则说明修改的参数没有什么问题,如果失败,重新调整一下上面的参数

5. 找到EMS 安装目录runGUI.bat runServer.bat脚本,找到-Xmx700m,改为-Xmx256m,注意修改前备份这两个文件,感谢Liping Sun提供帮助

6. 重新运行EMS

前后对比,对于3GB的PC,发现可以节省近1GB的内存。对于2GB的PC,也可以节省600-800MB。优化后发现EMS启动稍微慢一些,但是其它的软件运行速度提高了很多,不在经常出现卡机现象了。如果在运行过程中发现EMS特别慢的话,自己也可以适当放大上面提到的一些参数。

my.ini

# MySQL Server Instance Configuration File

# ----------------------------------------------------------------------

# Generated by the MySQL Server Instance Configuration Wizard

#

#

# Installation Instructions

# ----------------------------------------------------------------------

#

# On Linux you can copy this file to /etc/my.cnf to set global options,

# mysql-data-dir/my.cnf to set server-specific options

# (@localstatedir@ for this installation) or to

# ~/.my.cnf to set user-specific options.

#

# On Windows you should keep this file in the installation directory

# of your server (e.g. C:\Program Files\MySQL\MySQL Server X.Y). To

# make sure the server reads the config file use the startup option

# "--defaults-file".

#

# To run run the server from the command line, execute this in a

# command line shell, e.g.

# mysqld --defaults-file="C:\Program Files\MySQL\MySQL Server X.Y\my.ini"

#

# To install the server as a Windows service manually, execute this in a

# command line shell, e.g.

# mysqld --install MySQLXY --defaults-file="C:\Program Files\MySQL\MySQL Server X.Y\my.ini"

#

# And then execute this in a command line shell to start the server, e.g.

# net start MySQLXY

#

#

# Guildlines for editing this file

# ----------------------------------------------------------------------

#

# In this file, you can use all long options that the program supports.

# If you want to know the options a program supports, start the program

# with the "--help" option.

#

# More detailed information about the individual options can also be

# found in the manual.

#

#

# CLIENT SECTION

# ----------------------------------------------------------------------

#

# The following options will be read by MySQL client applications.

# Note that only client applications shipped by MySQL are guaranteed

# to read this section. If you want your own MySQL client program to

# honor these values, you need to specify it as an option during the

# MySQL client library initialization.

#

[client]

port=3306

[mysql]

default-character-set=utf8

# SERVER SECTION

# ----------------------------------------------------------------------

#

# The following options will be read by the MySQL Server. Make sure that

# you have installed the server correctly (see above) so it reads this

# file.

#

[mysqld]

# The TCP/IP Port the MySQL Server will listen on

port=3306

#Path to installation directory. All paths are usually resolved relative to this.

basedir="D:/Program Files/MySQL/MySQL Server 5.1/"

#Path to the database root

datadir="C:/Documents and Settings/All Users/Application Data/MySQL/MySQL Server 5.1/Data/"

# The default character set that will be used when a new schema or table is

# created and no character set is defined

character-set-server=utf8

# The default storage engine that will be used when create new tables when

default-storage-engine=INNODB

# Set the SQL mode to strict

sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

# The maximum amount of concurrent sessions the MySQL server will

# allow. One of these connections will be reserved for a user with

# SUPER privileges to allow the administrator to login even if the

# connection limit has been reached.

max_connections=1510

# Query cache is used to cache SELECT results and later return them

# without actual executing the same query once again. Having the query

# cache enabled may result in significant speed improvements, if your

# have a lot of identical queries and rarely changing tables. See the

# "Qcache_lowmem_prunes" status variable to check if the current value

# is high enough for your load.

# Note: In case your tables change very often or if your queries are

# textually different every time, the query cache may result in a

# slowdown instead of a performance improvement.

query_cache_size=16M

# The number of open tables for all threads. Increasing this value

# increases the number of file descriptors that mysqld requires.

# Therefore you have to make sure to set the amount of open files

# allowed to at least 4096 in the variable "open-files-limit" in

# section [mysqld_safe]

table_cache=3020

# Maximum size for internal (in-memory) temporary tables. If a table

# grows larger than this value, it is automatically converted to disk

# based table This limitation is for a single table. There can be many

# of them.

tmp_table_size=4M

# How many threads we should keep in a cache for reuse. When a client

# disconnects, the client's threads are put in the cache if there aren't

# more than thread_cache_size threads from before. This greatly reduces

# the amount of thread creations needed if you have a lot of new

# connections. (Normally this doesn't give a notable performance

# improvement if you have a good thread implementation.)

thread_cache_size=64

#*** MyISAM Specific options

# The maximum size of the temporary file MySQL is allowed to use while

# recreating the index (during REPAIR, ALTER TABLE or LOAD DATA INFILE.

# If the file-size would be bigger than this, the index will be created

# through the key cache (which is slower).

myisam_max_sort_file_size=100G

# If the temporary file used for fast index creation would be bigger

# than using the key cache by the amount specified here, then prefer the

# key cache method. This is mainly used to force long character keys in

# large tables to use the slower key cache method to create the index.

myisam_sort_buffer_size=4M

# Size of the Key Buffer, used to cache index blocks for MyISAM tables.

# Do not set it larger than 30% of your available memory, as some memory

# is also required by the OS to cache rows. Even if you're not using

# MyISAM tables, you should still set it to 8-64M as it will also be

# used for internal temporary disk tables.

key_buffer_size=16M

# Size of the buffer used for doing full table scans of MyISAM tables.

# Allocated per thread, if a full scan is needed.

read_buffer_size=64K

read_rnd_buffer_size=256K

# This buffer is allocated when MySQL needs to rebuild the index in

# REPAIR, OPTIMZE, ALTER table statements as well as in LOAD DATA INFILE

# into an empty table. It is allocated per thread so be careful with

# large settings.

sort_buffer_size=256K

#*** INNODB Specific options ***

# Use this option if you have a MySQL server with InnoDB support enabled

# but you do not plan to use it. This will save memory and disk space

# and speed up some things.

#skip-innodb

# Additional memory pool that is used by InnoDB to store metadata

# information. If InnoDB requires more memory for this purpose it will

# start to allocate it from the OS. As this is fast enough on most

# recent operating systems, you normally do not need to change this

# value. SHOW INNODB STATUS will display the current amount used.

innodb_additional_mem_pool_size=9M

# If set to 1, InnoDB will flush (fsync) the transaction logs to the

# disk at each commit, which offers full ACID behavior. If you are

# willing to compromise this safety, and you are running small

# transactions, you may set this to 0 or 2 to reduce disk I/O to the

# logs. Value 0 means that the log is only written to the log file and

# the log file flushed to disk approximately once per second. Value 2

# means the log is written to the log file at each commit, but the log

# file is only flushed to disk approximately once per second.

innodb_flush_log_at_trx_commit=1

# The size of the buffer InnoDB uses for buffering log data. As soon as

# it is full, InnoDB will have to flush it to disk. As it is flushed

# once per second anyway, it does not make sense to have it very large

# (even with long transactions).

innodb_log_buffer_size=5M

# InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and

# row data. The bigger you set this the less disk I/O is needed to

# access data in tables. On a dedicated database server you may set this

# parameter up to 80% of the machine physical memory size. Do not set it

# too large, though, because competition of the physical memory may

# cause paging in the operating system. Note that on 32bit systems you

# might be limited to 2-3.5G of user level memory per process, so do not

# set it too high.

innodb_buffer_pool_size=32M

# Size of each log file in a log group. You should set the combined size

# of log files to about 25%-100% of your buffer pool size to avoid

# unneeded buffer pool flush activity on log file overwrite. However,

# note that a larger logfile size will increase the time needed for the

# recovery process.

innodb_log_file_size=88M

# Number of threads allowed inside the InnoDB kernel. The optimal value

# depends highly on the application, hardware as well as the OS

# scheduler properties. A too high value may lead to thread thrashing.

innodb_thread_concurrency=8

   解决mysql耗内存的具体方法二:

更改后如下:

innodb_buffer_pool_size=576M ->256M InnoDB引擎缓冲区占了大头,首要就是拿它开刀

query_cache_size=100M ->16M 查询缓存

tmp_table_size=102M ->64M 临时表大小

key_buffer_size=256m ->32M

重启mysql服务后,虚拟内存降到200以下.

另外mysql安装目录下有几个文件:my-huge.ini 、my-large.ini、my-medium.ini...这几个是根据内存大小作的建议配置,新手在设置的时候也可以参考一下。

2G内存的MYSQL数据库服务器 my.ini优化 (my.ini)

2G内存,针对站少,优质型的设置,试验特:

table_cache=1024 物理内存越大,设置就越大.默认为2402,调到512-1024最佳

innodb_additional_mem_pool_size=8M 默认为2M

innodb_flush_log_at_trx_commit=0 等到innodb_log_buffer_size列队满后再统一储存,默认为1

innodb_log_buffer_size=4M 默认为1M

innodb_thread_concurrency=8 你的服务器CPU有几个就设置为几,默认为8

key_buffer_size=256M 默认为218 调到128最佳

tmp_table_size=64M 默认为16M 调到64-256最挂

read_buffer_size=4M 默认为64K

read_rnd_buffer_size=16M 默认为256K

sort_buffer_size=32M 默认为256K

max_connections=1024 默认为1210

试验一:

table_cache=512或1024

innodb_additional_mem_pool_size=2M

innodb_flush_log_at_trx_commit=0

innodb_log_buffer_size=1M

innodb_thread_concurrency=8 你的服务器CPU有几个就设置为几,默认为8

key_buffer_size=128M

tmp_table_size=128M

read_buffer_size=64K或128K

read_rnd_buffer_size=256K

sort_buffer_size=512K

max_connections=1024

试验二:

table_cache=512或1024

innodb_additional_mem_pool_size=8M

innodb_flush_log_at_trx_commit=0

innodb_log_buffer_size=4M

innodb_thread_concurrency=8

key_buffer_size=128M

tmp_table_size=128M

read_buffer_size=4M

read_rnd_buffer_size=16M

sort_buffer_size=32M

max_connections=1024

一般:

table_cache=512

innodb_additional_mem_pool_size=8M

innodb_flush_log_at_trx_commit=0

innodb_log_buffer_size=4M

innodb_thread_concurrency=8

key_buffer_size=128M

tmp_table_size=128M

read_buffer_size=4M

read_rnd_buffer_size=16M

sort_buffer_size=32M

max_connections=1024

经过测试.没有特殊情况,最好还是用默认的.

2G内存,针对站多,抗压型的设置,最佳:

table_cache=1024 物理内存越大,设置就越大.默认为2402,调到512-1024最佳

innodb_additional_mem_pool_size=4M 默认为2M

innodb_flush_log_at_trx_commit=1

(设置为0就是等到innodb_log_buffer_size列队满后再统一储存,默认为1)

innodb_log_buffer_size=2M 默认为1M

innodb_thread_concurrency=8 你的服务器CPU有几个就设置为几,建议用默认一般为8

key_buffer_size=256M 默认为218 调到128最佳

tmp_table_size=64M 默认为16M 调到64-256最挂

read_buffer_size=4M 默认为64K

read_rnd_buffer_size=16M 默认为256K

sort_buffer_size=32M 默认为256K

max_connections=1024 默认为1210

thread_cache_size=120 默认为60

query_cache_size=64M

优化mysql数据库性能的十个参数

(1)、max_connections:

允许的同时客户的数量。增加该值增加 mysqld 要求的文件描述符的数量。这个数字应该增加,否则,你将经常看到 too many connections 错误。 默认数值是100,我把它改为1024 。

(2)、record_buffer:

每个进行一个顺序扫描的线程为其扫描的每张表分配这个大小的一个缓冲区。如果你做很多顺序扫描,你可能想要增加该值。默认数值是131072(128k),我把它改为16773120 (16m)

(3)、key_buffer_size:

索引块是缓冲的并且被所有的线程共享。key_buffer_size是用于索引块的缓冲区大小,增加它可得到更好处理的索引(对所有读和多重写),到你能负担得起那样多。如果你使它太大,系统将开始换页并且真的变慢了。默认数值是8388600(8m),我的mysql主机有2gb内存,所以我把它改为 402649088(400mb)。

4)、back_log:

要求 mysql 能有的连接数量。当主要mysql线程在一个很短时间内得到非常多的连接请求,这就起作用,然后主线程花些时间(尽管很短)检查连接并且启动一个新线程。

back_log 值指出在mysql暂时停止回答新请求之前的短时间内多少个请求可以被存在堆栈中。只有如果期望在一个短时间内有很多连接,你需要增加它,换句话说,这值对到来的tcp/ip连接的侦听队列的大小。你的 *** 作系统在这个队列大小上有它自己的限制。试图设定back_log高于你的 *** 作系统的限制将是无效的。

当你观察你的主机进程列表,发现大量 264084 | unauthenticated user | xxx.xxx.xxx.xxx | null | connect | null | login | null 的待连接进程时,就要加大 back_log 的值了。默认数值是50,我把它改为500。

(5)、interactive_timeout:

服务器在关闭它前在一个交互连接上等待行动的秒数。一个交互的客户被定义为对 mysql_real_connect()使用 client_interactive 选项的客户。 默认数值是28800,我把它改为7200。

(6)、sort_buffer:

每个需要进行排序的线程分配该大小的一个缓冲区。增加这值加速order by或group by *** 作。默认数值是2097144(2m),我把它改为 16777208 (16m)。

(7)、table_cache:

为所有线程打开表的数量。增加该值能增加mysqld要求的文件描述符的数量。mysql对每个唯一打开的表需要2个文件描述符。默认数值是64,我把它改为512。

(8)、thread_cache_size:

可以复用的保存在中的线程的数量。如果有,新的线程从缓存中取得,当断开连接的时候如果有空间,客户的线置在缓存中。如果有很多新的线程,为了提高性能可以这个变量值。通过比较 connections 和 threads_created 状态的变量,可以看到这个变量的作用。我把它设置为 80。

(9)mysql的搜索功能

用mysql进行搜索,目的是能不分大小写,又能用中文进行搜索

只需起动mysqld时指定 --default-character-set=gb2312

(10)、wait_timeout:

服务器在关闭它之前在一个连接上等待行动的秒数。 默认数值是28800,我把它改为7200。

注:参数的调整可以通过修改 /etc/my.cnf 文件并重启 mysql 实现。这是一个比较谨慎的工作,上面的结果也仅仅是我的一些看法,你可以根据你自己主机的硬件情况(特别是内存大小)进一步修改。

我们仍然使用两个会话,一个会话 run,用于运行主 SQL;另一个会话 ps,用于进行 performance_schema 的观察:

主会话线程号为 29,

将 performance_schema 中的统计量重置,

临时表的表大小限制取决于参数  tmp_table_size 和 max_heap_table_size 中较小者,我们实验中以设置 max_heap_table_size 为例。

我们将会话级别的临时表大小设置为 2M(小于上次实验中临时表使用的空间),执行使用临时表的 SQL:

查看内存的分配记录:

会发现内存分配略大于 2M,我们猜测临时表会比配置略多一点消耗,可以忽略。

查看语句的特征值:

可以看到语句使用了一次需要落磁盘的临时表。

那么这张临时表用了多少的磁盘呢?

我们开启 performance_schema 中 waits 相关的统计项:

重做实验,略过。

再查看 performance_schema 的统计值:

可以看到几个现象:

1. 临时表空间被写入了 7.92MiB 的数据。

2. 这些数据是语句写入后,慢慢逐渐写入的。

来看看这些写入 *** 作的特征,该方法我们在 实验 03 使用过:

可以看到写入的线程是 page_clean_thread,是一个刷脏 *** 作,这样就能理解数据为什么是慢慢写入的。

也可以看到每个 IO *** 作的大小是 16K,也就是刷数据页的 *** 作。

结论:

我们可以看到,

1. MySQL 会基本遵守 max_heap_table_size 的设定,在内存不够用时,直接将表转到磁盘上存储。

2. 由于引擎不同(内存中表引擎为 heap,磁盘中表引擎则跟随 internal_tmp_disk_storage_engine 的配置),本次实验写磁盘的数据量和 实验 05 中使用内存的数据量不同。

3. 如果临时表要使用磁盘,表引擎配置为 InnoDB,那么即使临时表在一个时间很短的 SQL 中使用,且使用后即释放,释放后也会刷脏页到磁盘中,消耗部分 IO。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11170579.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存