欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-11 17:33:24
PDO::ERRMODE_EXCEPTION, ); $pdo=new PDO("mysql:host=localhost;dbname=pan","root","xxxx",$opts); $pdo->query('set names utf8'); $sth=$pdo->prepare("sel... ......
<?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号
*/