mysql出现Too many connections的解决...

最近写javaee项目的时候,mysql报了too many connections的错误,百度的内容有一些有问题,所以我重新写一下我的解决方法。

    1. mysql -u root -p 回车输入密码进入mysql 

    2. show processlist; 
      查看连接数,可以发现有很多连接处于sleep状态,这些其实是暂时没有用的,所以可以kill掉

    3. show variables like "max_connections"; 
      查看最大连接数,应该是与上面查询到的连接数相同,才会出现too many connections的情况

    4. set GLOBAL max_connections=1000; 
      修改最大连接数,但是这不是一劳永逸的方法,应该要让它自动杀死那些sleep的进程。

    5. show global variables like 'wait_timeout'; 
      这个数值指的是mysql在关闭一个非交互的连接之前要等待的秒数,默认是28800s

    6. set global wait_timeout=300; 
      修改这个数值,这里可以随意,最好控制在几分钟内 

    7. set global interactive_timeout=500; 
      修改这个数值,表示mysql在关闭一个连接之前要等待的秒数,至此可以让mysql自动关闭那些没用的连接,但要注意的是,正在使用的连接到了时间也会被关闭,因此这个时间值要合适

    8. 批量kill之前没用的sleep连接,在网上搜索的方法对我都不奏效,因此只好使用最笨的办法,一个一个kill

      • select concat('KILL ',id,';') from information_schema.processlist where user='root'; 先把要kill的连接id都查询出来
      • 复制中间的kill id;内容到word文档
      • 替换掉符号“|”和回车符(在word中查询^p即可查询到回车符)
      • 把修改过的内容复制回终端,最后按回车执行!

最简单的办法是因为你的my.ini中设定的并发连接数太少或者系统繁忙导致连接数被占满解决方式:打开MYSQL安装目录打开MY.INI找到max_connections默认是100 .
-
-
实例

WINDOWS解决MYSQL Errno.: 1040错误
XXXX info: Can not connect to MySQL server

User: root
Time: 2004-5-20 3:00pm
Script: /XXXX/XXXX.php

Error: Too many connections
Errno.: 1040

An error report has been dispatched to our administrator.


上面错误观点提示


1.可能是mysql的max connections设置的问题
2.可能是多次insert,update操作没有关闭session,需要在spring里配置transaction支持。

解决:
1.修改tomcat里的session 的time-out时间减少为20,(不是必改项)
2.对处理量大的对数据库insert或update的操作提供transaction支持.

=======================================
下面的是解决办法:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"

原因:

因为你的mysql安装目录下的my.ini中设定的并发连接数太少或者系统繁忙导致连接数被占满


解决方式:

打开MYSQL安装目录打开MY.INI找到max_connections(在大约第93行)默认是100 一般设置到500~1000比较合适,重启mysql,这样1040错误就解决啦。
max_connections=1000

一定要重新启动MYSQL才能生效

代码如下 复制代码 
CMD->

net stop mysql

net start mysql


 

关于改变innodb_log_file_size后无法启动mysql的问题

innodb_buffer_pool_size=768M
innodb_log_file_size=256M
innodb_log_buffer_size=8M
innodb_additional_mem_pool_size=4M
innodb_flush_log_at_trx_commit=0
innodb_thread_concurrency=20
 

以上是对innodb引擎的初步优化, 发现是更新innodb_log_file_size=256M时候出现了问题,只要加上这个就无法启动,

后来才知道原来要STOP服务先,然后再删除原来的文件………
打开/MySQL Server 5.5/data

删除ib_logfile0, ib_logfile1........ib_logfilen
再开启选项,成功启动。


高手优化的MYSQL my.ini的1000人在线配置。

#This File was made using the WinMySQLAdmin 1.4 Tool 
#2004-2-23 16:28:14 
#Uncomment or Add only the keys that you know how works. 
#Read the MySQL Manual for instructions 
[mysqld] 
basedir=D:/mysql 
#bind-address=210.5.*.* 
datadir=D:/mysql/data 
#language=D:/mysql/share/your language directory 
#slow query log#= 
#tmpdir#= 
#port=3306 
set-variable = max_connections=1500 
skip-locking 
#skip-networking 
set-variable = key_buffer=384M 
set-variable = max_allowed_packet=1M 
set-variable = table_cache=512 
set-variable = sort_buffer=2M 
set-variable = record_buffer=2M 
set-variable = thread_cache=8 
# Try number of CPU's*2 for thread_concurrency 
set-variable = thread_concurrency=8 
set-variable = myisam_sort_buffer_size=64M 
#set-variable = connect_timeout=5 
#set-variable = wait_timeout=5 
server-id = 1 
[isamchk] 
set-variable = key_buffer=128M 
set-variable = sort_buffer=128M 
set-variable = read_buffer=2M 
set-variable = write_buffer=2M 
[myisamchk] 
set-variable = key_buffer=128M 
set-variable = sort_buffer=128M 
set-variable = read_buffer=2M 
set-variable = write_buffer=2M 
[WinMySQLadmin] 
Server=D:/mysql/bin/mysqld-nt.exe

原文地址:https://www.cnblogs.com/sandea/p/9110357.html