[步骤] MariaDB & MySQL 的无密码登录 (使用 –login-path 命令对 MariaDB & MySQL 的密码进行加密保存)

步骤一:创建对 MariaDB & MySQL 的密码进行加密保存的设置

# mysql_config_editor set --login-path=test --user=root --host=127.0.0.1 --port=3306 --password
Endter Password:


补充:这里以
1) 创建名为 test 的设置
2) 登录用户是 root
3) 登录服务器是 127.0.0.1
4) 登录端口是 TCP 3306
5) 单独输入密码
为例

步骤二:显示所有 MariaDB & MySQL 的密码加密设置

# mysql_config_editor print --all

步骤三:使用 MariaDB & MySQL 的密码加密设置登录数据库

# mysql --login-path=test

(补充:这里以使用 test 的设置登录 MariaDB & MySQL 为例)

步骤三:删除MariaDB & MySQL 的密码加密设置

3.1 显示所有 MariaDB & MySQL 的密码加密设置

# mysql_config_editor print --all

3.2 删除对 MariaDB & MySQL 的密码进行加密保存的设置

# mysql_config_editor remove --login-patch=test

(补充:这里以删除名为 test 的密码加密设置为例)

[内容] 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 临时修改数据库最大连接数
> set global max_connections=1000;

(补充:这里以把数据库最大连接数修改为 1000 为例)

1.2.2 永久修改数据库最大连接数
1.2.2.1 修改 /etc/my.cnf 配置文件
# vi /etc/my.cnf

添加以下内容:

......
max_connections=32000

(补充:这里以把数据库最大连接数修改为 32000 为例)

1.2.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 语句

(步骤略)

思路三:增加硬件性能

(步骤略)