加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

[日常] PHP与Mysql测试kill慢查询并检验PDO的错误模式

发布时间:2020-05-25 03:12:34 所属栏目:PHP 来源:互联网
导读:?phptry{//1. pdo的错误模式,抛出异常,不记录到php的error日志,不影响代码继续运行,$opts=array(PDO::ATTR_ERRMODE = PDO::ERRMODE_EXCEPTION,);$pdo=new PDO(mysql:host=localhost;dbname=pan,root,xxxx,$opts);$pdo-query(set names utf8);$sth=$pdo-pre

<div class="cnblogs_Highlighter">
<pre class="brush:php;gutter:true;"><?php

try{

    //1. pdo的错误模式,抛出异常,不记录到php的error日志,不影响代码继续运行,$opts=array(
            PDO::ATTR_ERRMODE               => PDO::ERRMODE_EXCEPTION,);  

    $pdo=new PDO("mysql:host=localhost;dbname=pan","root","xxxx",$opts);
    $pdo->query('set names utf8');
    $sth=$pdo->prepare("select sleep(20) from texts where title like '%测试%'");
    $sth->execute(array("title"=>"测试"));
    $res=$sth->fetchAll(PDO::FETCH_ASSOC);
    print_r($res);

}catch(Exception $e){

    //2. code不是整型,如果不进行处理,会在此处报Fatal的500错误
    //Fatal error: Uncaught Error: Wrong parameters for MyDbException([string $message [,long $code [,Throwable $previous = NULL]]])
    throw new MyDbException($e->getMessage(),intval($e->getCode()));

}

class MyDbException extends Exception{

}

echo "1111111";

/*
3.测试慢查询
3.1 sql语句中使用 select sleep(20) from
3.2 开启慢查询日志:
配置文件:/etc/mysql/mysql.conf.d/mysqld.cnf
slow_query_log = ON
slow_query_log_file = /usr/local/mysql/data/slow.log
long_query_time = 1

            show variables like 'slow_query%';
            show variables like 'long_query_time';
    3.3 kill掉mysql的连接线程
            show full processlist;
            kill id号

*/

  

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读