[内容] MariaDB & MySQL 大量连接数和高消耗 SQL 语句的临时处理

内容一:数据库连接数处理

1.1 查看数据库连接数

1.1.1 查看已设置的最大连接数
mysql> show variables like 'max_connections';

或者:

mysql> show variables like '%connect%';
1.1.2 查看试图连接数
mysql> show status like '%connect%';
1.1.3 查看目前的连接数
mysql> show status like 'Threads_connected';

或者:

mysql> show status where variable_name = 'Threads_connected';

1.2 需改数据库最大连接数

1.2.1 修改 /etc/my.cnf 配置文件
# vi /etc/my.cnf

添加以下内容:

......
max_connections=32000
1.2.2 重启 MySQL 数据库

(步骤略)

内容二:高消耗 SQL 语句处理

2.1 查看被锁住的表

mysql> select * from information_schema.innodb_trx where trx_state='LOCK WAIT';

(补充:可以通过被锁住的表推断哪些 SQL 语句耗时较长)

2.2 查看 SQL 语句

2.2.1 查看所有的 SQL 语句
mysql> show processlist
2.2.2 查看所有的 SQL 语句并按照耗时长短排序
mysql> select id,user,host,state,info,time,command 
  from information_schema.processlist 
  where command !='Sleep'
  order by time desc;


补充:
1) 这里以显示 information_schema.processlist 表里的 id、user、host、state、info、time、command 字段为例
2) 这里的 where command != ‘Sleep’ 代表排除处于睡眠状态的 SQL 语句

2.2.3 查看耗时最高的 10 条 SQL 语句并按照耗时长短排序
mysql> select *
from information_schema.processlist
where command != 'Sleep'
  and time > 0 
order by time desc
limit 10;


补充:
1) 这里以显示 information_schema.processlist 表里的所有字段为例
2) 这里的 where command != ‘Sleep’ 代表排除处于睡眠状态的 SQL 语句

2.2.4 查看对某个数据库进行操作的耗时最高的 10 条 SQL 语句
mysql> select id,user,host,state,info,time,command 
  from information_schema.processlist 
  -- where db = 'eternalcenter'
  where command !='Sleep'
  order by time desc;


补充:
1) 这里以显示 information_schema.processlist 表里的 id、user、host、state、info、time、command 字段为例
2) 这里的 — where db = ‘eternalcenter’ 代表查看对 eternalcenter 库进行操作的 SQL 语句
3) 这里的 where command != ‘Sleep’ 代表排除处于睡眠状态的 SQL 语句

2.3 杀死高消耗的 SQL 语句

2.3.1 杀死某条 SQL 语句
mysql> kill <SQL ID>
2.3.2 杀死正在运行的耗时最高的 5 条 SQL 语句
mysql> select concat('kill ',id,';')
from information_schema.processlist
where command ='Query' 
order by time desc
limit 5;
2.3.3 确认正在运行的耗时最高的 5 条 SQL 语句被杀死
mysql> select ID,COMMAND,INFO 
from information_schema.processlist
where command ='Query' 
order by time desc
;

[步骤] Linux 加密硬盘新解密密码的添加

注意:在新解密密码添加之前需要先加密硬盘

正文:

步骤一:取消加密硬盘的加载

# umount /dev/mysqldatavg/mysqldatalv

步骤二:关闭 LUKS 解密映射 (解锁逻辑卷)

# cryptsetup luksOpen /dev/mapper/mysqldatavg-mysqldatalv mysqldata

之后输入 /dev/mapper/mysqldatavg-mysqldatalv 的解密密码

(补充:这里的 mysqldata 是解锁后的硬件名称)

步骤三:添加新的解密密码

# cryptsetup luksAddKey UUID="2eg8c60ac-103k-4771-k31r-14t93b06226a" --key-file /root/keyfile


补充:
1) 这里的 2eg8c60ac-103k-4771-k31r-14t93b06226a 是新添加的解密密码
2) 这里的 /root/keyfile 是原来的用来解密加密硬盘的密钥

步骤四:开启 LUKS 解密映射 (加密逻辑卷)

# cryptsetup luksOpen UUID="2eg8c60ac-103k-4771-k31r-14t93b06226a"  mysqldatalv


补充:
1) 这里的 2eg8c60ac-103k-4771-k31r-14t93b06226a 是刚刚添加的解密密码
2) 这里的 mysqldata 是解密后的硬件名称

[命令] Linux 命令 colrm (删除所有行某列的字符)

案例一:删除所有行某列字符后的所有字符

# cat test.txt | colrm 3

或者:

# colrm 3 < test.txt

(补充:这里以删除所有行第 3 列及以后的所有字符)

案例二:删除所有行从某列字符到某列字符期间的所有字符

# cat test.txt | colrm 3 6

或者:

# colrm 3 6 < test.txt

(补充:这里以删除所有行第 3 列到第 6 列包括第 3 列和第 6 列的所有字符)

[内容] 常用的 Perl 兼容正则表达式 (PCRE)

1) \b 匹配单词边界处的空格字符,使用案例:# echo “ mingyuzhu ” | grep -P “\bmingyuzhu\b”
2) \B 匹配单词边界处的非空格字符,使用案例:# echo “mingyuzhu” | grep -P “\Bingyuzh\B”
3) \K 不要将 \K 左边的内容放进 $& (匹配的内容),也就是不会对 \K 左边的内容进行匹配
4) \A 匹配行首,使用案例:# echo mingyuzhu | grep -P “\Am”
5) \z 匹配行尾,使用案例:# echo mingyuzhu | grep -P “u\z”
6) \Z 匹配行尾或者匹配行尾换行符前面,使用案例:# echo mingyuzhu | grep -P “u\Z”
7) \G 全局匹配成功,位移指针自然会后移。全局匹配失败,位移指针会重置。如果匹配失败同时加上 \c 修饰符那位移指针不会变化

$txt = "123eternalcenter456";
$txt =~ /\G\d\d\d/gc; 
print "matched $&: ",pos $txt,"\n";

matched 123: 3


补充:
1) $& 代表匹配的内容
2) $` 代表匹配的内容前面的内容
3) $’ 代表匹配的内容后面的内容

8) 结尾处的 \g 全局匹配,当要匹配的内容出现时不会停下而是继续匹配下去,直到检查完全文。且每次匹配成功后都会记下指定位,每次记下的指定位在匹配的字符的左边,下次再匹配时就会从这个指定位开始匹配。每次的匹配位置可以通过 pos() 函数获取,如果匹配失败位移指针则会重置,pos 将返回 undef。如果和 \c 修饰符组合成 \gc, 匹配失败则不重置位移指针而是将位移指针向后移动 1 位,直到移动到最后 1 位。一般在匹配位置的最后面,案例:

$txt = "123eternalcenter456";
$txt =~ /\d\d\d/g; 
print "matched $&: ",pos $txt,"\n";

matched 123: 3


补充:
1) $& 代表匹配的内容
2) $` 代表匹配的内容前面的内容
3) $’ 代表匹配的内容后面的内容

9) \w 匹配单词,等价于 [[:alnum:]] 10) \W 匹配非单词,等价于 [^[:alnum:]]
11) \s 匹配空格字符,等价于 [[:space:]]
12) \S 匹配非空格字符,等价于 [^[:space:]]
13) \d 匹配数字,等价于 [0-9]
14) \D 匹配非数字,等价于 [^0-9]
15) \n 匹配换行
16) \N 不匹配换行,等价于 [^\n]