MySQL性能指标

1. MySQL Connections
数据库连接数分为Max Connections, Max Used Connections 和 Connections
即:最大连接数, 最近期间使用的最大连接数 和 当前实时连接数!
在使用MySQL数据库的时候,经常会遇到这么一个问题,就是“Can not connect to MySQL server. Too many connections”-mysql 1040错误,这是因为访问MySQL且还未释放的连接数目已经达到MySQL的上限。通常,msql的最大连数默认是151(ubuntu), 100(windows)。

mysql>mysql -u  user -p  password(命令行登录MySQL)
mysql>show variables like 'max_connections';(查可以看当前的最大连接数)
msyql>set global max_connections=1000;(设置最大连接数为1000,可以再次查看是否设置成功)
mysql>exit(推出)

这种方式有个问题,就是设置的最大连接数只在mysql当前服务进程有效,一旦mysql重启,又会恢复到初始状态。因为mysql启动后的初始化工作是从其配置文件中读取数据的,而这种方式没有对其配置文件做更改。

问题:MySQL的最大连接数设置为多少比较合适?这个值是不是越大越好?

这个参数的大小要综合很多因素来考虑,比如使用的平台所支持的线程库数量(windows只能支持到2048)、服务器的配置(特别是内存大小)、每个连接占用资源(内存和负载)的多少、系统需要的响应时间等。可以在global或session范围内修改这个参数。连接数的增加会带来很多连锁反应,需要在实际中避免由此引发的负面影响。
详细参考:MySQL最大连接数设置

ubuntu设置mysql的最大连接数:
cd /etc/mysql/mysql.conf.d 修改mysqld.cnf文件,在[mysqld]中添加或修改

cd /etc/mysql/mysql.conf.d
vi mysqld.cnf
max_connections=99

然后重新启动mysql
[root@localhost ~]# service mysql restart //重启mysql

查看当前系统下MySQL设置的最大连接数

[root@localhost ~]# /usr/bin/mysqladmin -u root  -p  variables | grep  max_connections
| max_connections                 | 99   

2. MySQL Active Threads
Threads Connected , Thread Running 即连接的线程数和运行的线程数。
可以用多线程去测试!

  1. MySQL Questions
    4. MySQL Thread Cache
    • Thread Cache Size
    • Thread Cached
    • Thread Ctreated
      MySQL服务器的线程数查看方法:
mysql> show  global status like 'Thread%';
Variable_nameValuesmeaning
Threads_cached57The number of threads in the thread cache
Threads_connected1268The number of currently open connections.
Threads_created31715The number of threads created to handle connections.
Threads_running1The number of threads that are not sleeping.

thread_cache_size的意义:每建立一个连接,都需要一个线程来与之匹配,此参数用来缓存空闲的线程,以至不被销毁,如果线程缓存中有空闲线程,这时候如果建立新连接,MYSQL就会很快的响应连接请求。
mysql建立连接非常消耗资源,所以就有了thread_cache,当已有连接不再使用之后,mysql server不是直接断开连接,而是将已有连接转入到thread_cache中,以便下次在有create thread的需求时,可以在cache中复用,提高性能,降低资源消耗。
threads_cached :代表当前此时此刻线程缓存中有多少空闲线程。
Threads_connected :代表当前已建立连接的数量,因为一个连接就需要一个线程,所以也可以看成当前被使用的线程数
Threads_created :代表从最近一次服务启动,已创建线程的数量。
Threads_running :代表当前激活的(非睡眠状态)线程数。并不是代表正在使用的线程数,有时候连接已建立,但是连接处于sleep状态,这里相对应的线程也是sleep状态。
四者之间的关系:
running和其他三个状态关系不大,但肯定不会超过thread_connected
(new_con-old_con)=create+(old_cache-new_cache)
从上面公式可以看出,如果create等于0,那么thread_connected减少的和thread_cached增加的相等,thread_connected增加的和thread_cached减少的相等。(其实这也就是thread_cached存在的意义,资源可以复用)
我们来看眼影响thread_cached的参数thread_cache_size
How many threads the server should cache for reuse. When a client disconnects, the client’s threads are put in the cache if there are fewer than thread_cache_size threads there. Requests for threads are satisfied by reusing threads taken from the cache if possible, and only when the cache is empty is a new thread created. This variable can be increased to improve performance if you have a lot of new connections. Normally, this does not provide a notable performance improvement if you have a good thread implementation. However, if your server sees hundreds of connections per second you should normally set thread_cache_size high enough so that most new connections use cached threads. By examining the difference between the Connections and Threads_created status variables, you can see how efficient the thread cache is. For details, see Section 5.1.6, “Server Status Variables”.
参考:MySQL之thread cache
ubuntu修改thread_cache_size

cd /etc/mysql/mysql.conf.d
sudo vi mysqld.cnf
thread_cache_size = 48 //修改为48
  1. MySQL Temporary Objects
    • Create Tmp Tables
    • Create Tmp Disk Tables
    • Create Tmp Files
  2. MySQL Select Types
    • Select Scan:通过对第一个数据表进行全表扫描而完成的多数据表联接操作的次数.
    • Select Range Check:该变量记录了在联接时,对每一行数据重新检查索引的查询计划的数量,它的开销很大.如果该值较高或正在增加,说明一些查询没有找到好索引.
    • Select Range:利用第一个数据表上的某个区间而完成的多数据表联接操作的次数.
    • Select Full Range Join:利用一个辅助性的参照表(reference table)上的区间搜索
      (range search)操作而完成的多数据表联接操作的次数.该值表示使用了范围查询联接表的次数.
    • Select Full Join:没有使用索引而完成的多表联接操作的次数.这种情况是性能杀手,最好去优化sql.
  3. MySQL Sorts
    • Sort Scan:利用一次全表扫作而完成的排序操作的次数
    • Sort Merge Passes:查询导致了文件排序的次数.可以优化sql或者适当增加sort_buffer_size变量
    • Sort Range:利用一个区间进行的排序操作的次数
    • Sort Rows:对多少行排序
  4. MySQL Slow Queries
    • Slow Queries:慢查询的次数(执行时间超过long_query_time值) 参考链接
  5. MySQL Aborted Connections
    • Aborted Connects (attempts)
    • Aborted Clients (timeout)
  6. MySQL Table Locks
    • Table Locks Immediate:无需等待就能够立刻得到满足的数据表锁定请求的个数
    • Table Locks Waited:显示了有多少表被锁住了并且导致服务器级的锁等待(存储引擎级的锁,如InnoDB行级锁,不会使该变量增加).如果这个值比较高或者正在增加,那么表明存在严重的并发瓶颈.
  7. MySQL Network Traffic
    • OutBound
    • Inbound
  8. MySQL Internal Memory Overview
    • System Memory :操作系统总内存
    • Query Cache Size :mysql配置文件设置的大小
    • Key Buffer Size :mysql配置文件设置大小。
    • InnoDB Log Buffer Size
    • InnoDB Buffer Pool Data
  9. Top Command Counters
show session status like 'com_delete';
show session status like 'com_insert';
show session status like 'com_update';
show session status like 'com_select';

com_insert、com_delete,com_update分别是查询本次会话中“增删改” 了多少次.
com_select的一种解释为com_select 变量记录的是无缓存的查询次数+错误查询+权限检查查询。如果select有缓存,数值不增加,如果命中缓存,缓存命中的次数加1,也就是Qcache_hits变量的值加1.

  1. MySQL Handlers
    • read_rnd_next:读取下一个数据行的请求的个数.如果这个数字很高,就说明有很多语句需要通过全表扫描才能完成或有很多查询没有使用适当的索引
    • write:向数据表里插入一个数据行的请求的个数
    • external_lock
    • read_key:根据一个索引值而读取一个数据行的请求的个数
    • update:对数据表里的一个数据行进行修改的请求的个数
    • read_rnd:根据某个数据行的位置而读取该数据行的请求的个数
    • read_prev:按索引逆序读取前一个数据行的请求的个数
    • read_next:按索引顺序读取下一个数据行的请求的个数
    • read_last
    • read_first:读取索引中第一个索引项的请求的个数
    • mrr_init
    • discover
    • delete:从数据表删除一个数据行的请求的个数
  2. MySQL Transaction Handlers
    commit :提交一个事务的请求的个数
    prepare
    rollback :回滚一个事务的请求的个数
    savepoint :创建一个事务保存点的请求个数
    savepoint_rollback :回滚到一个事务保存点的请求的个数
  3. Process States
  4. Top Process States Hoursly
  5. MySQL Query Cache Memory
    • Query Cache Size
    • Free Memory
  6. MySQL Query Cache Activity
    • Not Cached
    • Queries in Cache
    • Prunes
    • Inserts
    • Hits
  7. MySQL File Openings
    • Openings
  8. MySQL Open Files
    • Open Files 打开的文件数
    • Open Files Limits 文件的上限
    • InnoDB Open File InnoDB打开的文件数
  9. MySQL Table Open Status
    • Hits
    • Misses
    • Opennings
    • Misses due to Overflows
    • Table Open Cache Hit Ratio
  10. MySQL Open Tables
    • Table Open Cache
    • Open Tables
  11. MySQL Table Definition Cache
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值