在进行Web应用程序开发的时候,人们经常会用Session存储数据。但可能有人不知道,在PHP中,Session使用不当可能会引起并发问题。印度医疗行业软件解决方案提供商Plus91 Technologies高级工程师Kishan Gor在个人博客上对这个问题进行了阐释。 如果同一个客户端并发发送多个请求,而每个请求都使用了Session,那么PHP Session锁的存在会导致服务器串行响应这些请求,而不是并行。这是因为在默认情况下,PHP使用文件存储Session数据。对于每一个新的 Session,PHP会创建一个文件,并持续向其中写入数据。所以,每次调用session_start()方法,就会打开Session文件,并取得 文件的独占锁。这样,如果服务器脚本正在处理一个请求,而客户端又发送了一个同样需要使用Session的请求,那么后一个请求会阻塞,直至前一个请求处 理完成释放了文件上的独占锁。不过,这只限于来自同一个客户端的多个请求,也就是说,来自一个客户端的请求并不会阻塞另一个客户端的请求。 如果脚本很短,这通常没有问题。但如果脚本运行时间比较长,那就可能会产生问题。在现代Web应用程序开发中,有一个非常常见的情况,就是使用…
PHP: MySQL报错: ERROR 1040: Too many connections
MySQL: ERROR 1040: Too many connections
This basically tells that MySQL handles the maximum number of connections simultaneously and by default it handles 100 connections simultaneously.
These following reasons cause MySQL to run out connections.
- Slow Queries
- Data Storage Techniques
- Bad MySQL configuration
I was able to overcome this issues by doing the followings.
Open MySQL command line tool
and type, 查看默认连接数:
show variables like "max_connections";
This will return you something like this. 结果如下:
+-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 100 | +-----------------+-------+
You can change the setting to e.g. 200 by issuing the following command without having to restart the MySQL server. 自定义连接数:
set global max_connections = 200;
Now when you restart MySQL the next time it will use this setting instead of the default. 重启apache2:
sudo service apache2 restart
Keep in mind that increase of the number of connections will increase the amount of RAM required for MySQL to run.
注意:增加连接数,意味着需要更高的服务器设备要求!
You have to change max_connections
to increase total permitted connections.
And set max_user_connections
back to default 0 => no limit unless you need to limit this per user connection
更多参看官网:https://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html
本文: PHP: MySQL报错: ERROR 1040: Too many connections