MySql 报 could not be resolved: Temporary failure in name resolution
原因:MYSQL Server在本地内存中维护了一个非本地的Client TCP cache, 这个cache中包含了远程Client的登录信息, 比如IP地址、hostname等信息
如果Client连接到服务器后, MySQL首先会在本地TCP池中根据IP地址解析客户端的hostname或者反解析, 如果解析不到, 就会去DNS中进行解析, 如果还是解析失败, 就是在error log中写入这样的警告信息
1 | #修改mysql配置文件 |
MySql5.7 子查询 order by 失效
1 | #获取用户最新的30条评论 |
MySql 多个联表查询统计时, 数据统计不准确
1 | -- 测试表及数据 |
MySql server has gone away 问题重现及断线重连
经常出现在长连接项目或者cli模式运行时间过长, 如果没处理好断线重连, 则会出现此问题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 // test.php
/**
* @param PDO $dbh
*
* @return string
*/
function ping(PDO $dbh)
{
$message = '';
try {
$dbh->getAttribute(PDO::ATTR_SERVER_INFO);
} catch (\PDOException $e) {
$message = $e->getMessage();
}
return $message;
}
$config = [
'host' => '127.0.0.1',
'dbname' => 'test',
'user' => 'root',
'password' => 'root',
];
$options = [
// 主动抛出 PDOException 异常
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$sql = 'SELECT * FROM test';
$dbh = new PDO("mysql:host={$config['host']};dbname={$config['dbname']}", $config['user'], $config['password'], $options);
$result = $dbh->query($sql);
var_dump($result, ping($dbh));
sleep(30);
$message = ping($dbh);
if (!empty($message)) {
echo "MySql错误:{$message}, 正在重连..." . PHP_EOL;
$dbh = new PDO("mysql:host={$config['host']};dbname={$config['dbname']}", $config['user'], $config['password'], $options);
}
$result = $dbh->query($sql);
var_dump($result, ping($dbh));测试方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #1. 运行 test.php
php test.php
#2. 运行后在 mysql 命令行模式下, 查看当前有哪些连接
mysql> SHOW PROCESSLIST;
+----+------+-----------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+----------+------------------+
| 95 | root | localhost:58021 | test | Query | 0 | starting | show processlist |
| 96 | root | localhost:58024 | test | Sleep | 4 | | NULL |
+----+------+-----------------+------+---------+------+----------+------------------+
2 rows in set (0.02 sec)
#3. 杀死指定连接, ID = 96, 此连接是 test.php 内的 mysql 连接
#4. 等 test.php 的休眠时间过后, 即可看到测试结果