赞
踩
关注微信公众号:CodingTechWork,一起学习进步。
在开发或者运维过程中,我们连接数据库的时候突然会遇到"Too many Connections"这种报错信息;这时我们就需要排除一下是哪些程序客户端连接较多而没有释放。
show variables like 'max_connections';
show status like 'max%connections';
发现已使用的已经大于最大连接数。
这种方式重启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。
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)各列的含义
数据库端口为3306,查看连接该端口的java程序进程
netstat -anp | grep 3306 | grep java
ps -ef | grep 进程号
netstat -anp | grep 3306 | grep 进程号 | wc -l
通过wc -l
统计出某个进程的连接数。
通过上述一系列操作,我们大概就能知道哪个程序服务连接数较多了。如上述的23818这个进程的连接数最多,为136。这时就需要考虑是否程序中对数据库连接没有做一些限制。
# 配置数据库【需要按照具体环境修改】 spring.datasource.url: jdbc:mysql://ip1:3306,ip2:3306,ip3:3306/dbName?useSSL=false&useUnicode=true&&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";
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。