最近写javaee项目的时候,mysql报了too many connections的错误,百度的内容有一些有问题,因此我从新写一下个人解决方法。php
-
mysql -u root -p
回车输入密码进入mysqljava
-
show processlist;
查看链接数,能够发现有不少链接处于sleep状态,这些实际上是暂时没有用的,因此能够kill掉mysql -
show variables like "max_connections";
查看最大链接数,应该是与上面查询到的链接数相同,才会出现too many connections的状况spring -
set GLOBAL max_connections=1000;
修改最大链接数,可是这不是一劳永逸的方法,应该要让它自动杀死那些sleep的进程。sql -
show global variables like 'wait_timeout';
这个数值指的是mysql在关闭一个非交互的链接以前要等待的秒数,默认是28800s数据库 -
set global wait_timeout=300;
修改这个数值,这里能够随意,最好控制在几分钟内tomcat
-
set global interactive_timeout=500;
修改这个数值,表示mysql在关闭一个链接以前要等待的秒数,至此可让mysql自动关闭那些没用的链接,但要注意的是,正在使用的链接到了时间也会被关闭,所以这个时间值要合适session -
批量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