zoukankan      html  css  js  c++  java
  • php脚本记录每秒的连接数和活跃连接数

    <?php
    date_default_timezone_set('Asia/Shanghai');
    set_time_limit(0);
    //连接数据库
    $con = new mysqli(localhost,user,password,db,port);
    //如果状态码不为0,则打印出错误信息
    if (mysqli_connect_errno()){ 
    die('Unable to connect!'). mysqli_connect_error(); 
    }

    $json = array(); //模拟监控十分钟的数据库状态 for($i=0;$i<600;$i++) { $time = date('Y-m-d H:i:s',time()); $res = get_mysql_status($con); //服务器启动后已经同时使用的连接的最大数量 $json['max_connections'] = $res['Max_used_connections']; //当前连接数 $json['connections'][$time]['threads_connected'] = $res['Threads_connected']; //活跃连接数 $json['connections'][$time]['threads_running'] = $res['Threads_running']; //模拟每秒执行一次 sleep(1); } //转换成JSON $json = json_encode($json,JSON_UNESCAPED_UNICODE); //生成文件 file_put_contents('stat.json',$json); //请求数据库状态的函数 function get_mysql_status($con) { $arr = array(); //查询语句 $sql = "show global status"; $result = $con->query($sql); while($data = $result->fetch_assoc()) { $arr[$data['Variable_name']] = $data['Value']; } return $arr; } ?>

       用sysbench模拟对mysql数据库并发操作后运行这个脚本,会记录各个时间段的情况.

  • 相关阅读:
    C#中委托和事件的区别
    Linux centos7 计划任务与日志的管理
    linux shell检查字符串是否是IP
    Linux脚本中$#、$0、$1、$@、$*、$$、$?
    Linux命令学习之shift命令
    CentOS7编写systemd服务脚本
    java数据类型转换
    centos7 升级openssh到openssh-8.3p1版本
    oracle的簇的创建
    oracle 分区表的维护
  • 原文地址:https://www.cnblogs.com/tudou1223/p/4450413.html
Copyright © 2011-2022 走看看