MySQL—Linux查看客户端连接信息(连接数、进程等)

介绍

  在开发或者运维过程中,我们连接数据库的时候突然会遇到"Too many Connections"这种报错信息;这时我们就需要排除一下是哪些程序客户端连接较多而没有释放。

查看mysql数据库连接数

查看最大连接数

show variables like 'max_connections';
在这里插入图片描述

查看已使用连接数

show status like 'max%connections';
在这里插入图片描述
发现已使用的已经大于最大连接数。

更改最大连接数

全局set(临时)

这种方式重启mysql后会失效。
set GLOBAL max_connections=1000;

mysql> set GLOBAL max_connections=1000;
Query OK, 0 rows affected (2.00 sec)
查看最大连接数

在这里插入图片描述

查看已使用连接数

在这里插入图片描述
发现由原来的152变成了159,可以更多的连接。

配置文件修改

修改/etc/my.cnf配置文件
[mysqld]块中修改或添加:max_connections=1000,重启mysql。

查看数据库连接情况

查看数据库连接IP列表及数量

select SUBSTRING_INDEX(host,':',1) as ip , count(*) from information_schema.processlist group by ip;
在这里插入图片描述

查看数据库连接数

select count(*) from information_schema.processlist;
在这里插入图片描述

查看进程列表

(1)列出前100条
show processlist;
在这里插入图片描述
(2)列出所有
show full processlist;

(3)各列的含义

  • Id:该进程程序登录mysql时,系统分配的连接id,即为connection_id。
  • User:该进程程序连接mysql的用户。
  • Host:该进程程序连接mysql的ip。
  • db:该进程程序连接mysql的某个数据库。
  • Command:该进程程序执行的命令,取值为休眠(Sleep)、查询(Query)、连接(Connect)等。
  • Time:Command状态持续的时间,单位为秒。
  • State:使用当前的sql语句的状态,如starting。
  • Info:显示sql语句,如当前执行了show full processlist。

查看进程

查看连接数据库的java进程

数据库端口为3306,查看连接该端口的java程序进程
netstat -anp | grep 3306 | grep java
在这里插入图片描述

查看进程对应的程序

ps -ef | grep 进程号
在这里插入图片描述

统计某个进程连接数

netstat -anp | grep 3306 | grep 进程号 | wc -l
通过wc -l统计出某个进程的连接数。
在这里插入图片描述
通过上述一系列操作,我们大概就能知道哪个程序服务连接数较多了。如上述的23818这个进程的连接数最多,为136。这时就需要考虑是否程序中对数据库连接没有做一些限制。

Java程序配置数据库连接池

# 配置数据库【需要按照具体环境修改】
spring.datasource.url: jdbc:mysql://ip1:3306,ip2:3306,ip3:3306/dbName?useSSL=false&useUnicode=true&amp&characterEncoding=utf-8
# Username and password
spring.datasource.username: userA
#ENC()前缀为数据库密码密文处理,测试时可直接配置数据库密码如:spring.datasource.password =123456
spring.datasource.password: ENC(0cHjeDIecX6DZd+T8kfNuukmfdfdsfd)
#spring.datasource.password: 123456

# 指定获取连接时连接校验的sql查询语句
spring.datasource.validation-query: SELECT 1 FROM DUAL
# 配置获取连接等待超时的时间
spring.datasource.max-wait: 10000
# 获取连接时候验证,会影响性能
spring.datasource.test-on-borrow: true
# 配置获取连接池中最大空闲数
spring.datasource.max-idle: 30
# 配置获取连接池中最大活跃数
spring.datasource.max-active: 100
# 指定连接池中连接的最大生存时间,毫秒单位
spring.datasource.max-lifetime: 300000
# 指定数据库连接驱动
spring.datasource.driverClassName: com.mysql.jdbc.Driver
# 指定数据库类型为MYSQL
spring.jpa.database: MYSQL
# 查询时是否显示日志,无需修改
spring.jpa.show-sql: false
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto: update
# Naming strategy
spring.jpa.hibernate.naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the bean manager)
#spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
#spring.jpa.properties.hibernate.dialect: com.xxx.yyy.demo.entity.MySQL5DialectUTF8
# 字段无修改命名
spring.jpa.hibernate.naming.physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
# 遇到大写字母 加”_”的命名
# spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

jasypt:
  encryptor:
    password: EbfYkitulv73I2p0mXI50JMXoaxZTKJ7

注意:其中的spring.jpa.properties.hibernate.dialect: com.xxx.yyy.demo.entity.MySQL5DialectUTF8即为数据库生成表时为utf-8的编码。

package com.xxx.yyy.demo.entity;
import org.hibernate.dialect.MySQL5InnoDBDialect;

/**
 * 自动建表(字符集utf-8)
 */
public class MySQL5DialectUTF8 extends MySQL5InnoDBDialect {
    @Override
    public String getTableTypeString() {
        return " ENGINE=InnoDB DEFAULT CHARSET=utf8";
    }
}

posted @ 2020-05-22 12:37  Andya_net  阅读(10207)  评论(0编辑  收藏  举报