[内容] 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
;

[内容] MariaDB & MySQL 性能的优化

思路一:减少总连接数

1.1 显示数据库目前的连接数

> SHOW STATUS LIKE 'Threads_connected';

1.2 查看数据库目前所允许的最大连接数

> SHOW VARIABLES LIKE 'max_connections';

1.3 临时设置数据库目前所允许的最大连接数

> SET GLOBAL max_connections = 200;

(补充:这里以临时将数据库目前所允许的最大连接数设置为 200 为例)

思路二:优化 SQL 语句

2.1 查询执行的 SQL 语句

2.1.1 通过 SQL 语句查询正在运行的 SQL 语句
> SHOW PROCESSLIST;
2.1.2 通过 SQL 语句查询正在运行的 SQL 语句的详细信息
> SELECT * FROM performance_schema.threads;

(注意:当数据库越来越大是,全库查询的 SQL 语句执行起来会越来越慢,耗时也会越来越长。应用最终会被 SQL 语句查询的长时间拖死)

2.1.3 查询 SLOW 日志

(步骤略)

2.2 优化 SQL 语句

(步骤略)

思路三:增加硬件性能

(步骤略)

[SQL] MariaDB & MySQL 创建用户的步骤

步骤一:创建用户并设置密码

mysql> create user 'mingyuzhu'@'192.168.%' identified by 'mypassword';

(补充:这里以创建用户 mingyuzhu@192.168.% 并且把密码设置成 mypassword 为例)

步骤二:将密码的有效期设置为 90 天

mysql> alter user 'mingyuzhu'@'192.168.%' password expire interval 90 day;

(补充:这里以将用户 mingyuzhu@192.168.% 密码的有效期设置为 90 天为例)

步骤三:给用户某个库增、删、改、查的权限

mysql> grant select,insert,update,delete,drop,create on eternalcenter.* to 'mingyuzhu'@'192.168.%';

(补充:这里以给用户 mingyuzhu@192.168.% 库 eternalcenter 增、删、改、查的权限为例)

步骤四:刷新权限

mysql> flush privileges;

步骤五:删除数据

mysql> drop user 'mingyuzhu'@'192.168.%';

(补充:这里以删除用户 mingyuzhu@192.168.% 为例)