mysql调优

MySQL的优化我分为两个部分:硬件的优化、mysql自身(my.cnf)的优化
----------------------------------------------------------------
[root@localhost ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M    #联合查询操作所能使用的缓冲区大小,和sort_buffer_size一样,该参数对应的分配内存也是每连接独享。
# sort_buffer_size = 2M        #查询排序时所能使用的缓冲区大小。注意:该参数对应的分配内存是每连接独占,如果有100个连接,那么实际分配的总共排序缓冲区大小为100 × 6 = 600MB。所以,对于内存在4GB左右的服务器推荐设置为6-8M。
# read_rnd_buffer_size = 2M    #默认为256K
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@localhost ~]#
-------------------
1、
磁盘I/O使用RAID-0+1磁盘阵列
CPU_4U的服务器
内存

2、mysql优化:
未配置主从同步,可以把 bin-log 功能关闭,减少磁盘 i/o
在 my.cnf 中加上 skip-name-resolve ,这样可以避免由于解析主机名延迟造成 mysql 执行慢


最大连接数为:16834(默认为100,不准超过16834)
    show variables like 'max_connections';       //查看数据库的最大连接数
    set global max_connections=10000;       //修改数据库的最大连接数


    alter table ku.biao disable keys;       //禁用索引(公司量少:1-9万之间)
    set unique_check=0               //禁用外键
    set foreign_key_checks=0            //禁用外键健康检查
    set autocommit=0;


MySQL_调整几个关键的 buffer 和 cache
query_cache_size=8192M  //缓存区的大小
query_cache_type=1      //缓存的类型
    0   不开启缓存区
    1:no_sql_cache  所有的都得进入缓存查询
    2:sql_cache 所有的东西默认的在硬盘中找
key_buffer_size = 16M       //索引缓存区的大小(取决于内存的大小,内存的十分之一)
table_open_cache=64     //同时打开的表的个数(根据自己公司的表的个数来定。也可以是table_cache=64)
sort_buffer_size= 512K      //排序缓存区的大小(数越大,速度越快。数据库里面的数据往出读的时候需要排序。默认是2M。200次排序约等于1K。)
read_buffer_size=256K       //每个表分配缓存区的大小(当有线程连续扫描该表时,才会使用这个缓冲区,不读就释放该大小。)
read_rnd_buffer_size=512K       //连续扫描或连续读取,并且有特定的排序的时候才会使用这个
innodb_buffer_pool_size=16M //innodb引擎的时候调这个
myisam_sort_buffer_size=8M
max_connections=10000
innodb_fush_log_at_trx_commit=0    
    0   每隔1秒    什么时候把缓冲区的数据写入到日志当中,并把日志写入到硬盘里(缺点:不安全。优点:速度快)
    1   依照事务的提交写入日志(缺点:速度慢。优点:安全)
    2   每次提交事务写入日志,每隔1秒钟写入硬盘(缺点:丢失1到2秒钟的数据)
back_log=8192       //多少个请求可以放到列队当中(公司在某一时间的有大的访问的时候使用)
interactive_timeout=10      //关闭服务器连接时,等待的时长
thread_buffer_size=512K //每个需要排队线程分配的缓冲区的大小
wait_timeout=6000               //关闭一个连接等待的时间(默认28800s)







posted on 2017-12-12 17:45  ln822  阅读(126)  评论(0编辑  收藏  举报