search
HomeDatabaseMysql Tutorialmysql 1040错误Too many connections_MySQL

你的服务器是不是出现这样的警告?

信息如下:

\

 

从官方文档知道linux上面编译安装的mysql默认的连接为100个,这样对于网站的需求来说是远远不够的。

mysql官方告诉我们需要修改max_connections的值,那么我们怎么去修改呢?有两种方法

1、修改配置文件文件

修改/etc/my.cnf这个文件,在[mysqld]中新增max_connections=N,如果你没有这个文件请从编译源码中的support-files文件夹中复制你所需要的*.cnf文件为到/etc/my.cnf。我使用的是my-medium.cnf,中型服务器配置。例如我的[mysqld]的内容如下

[mysqld]

port              = 3306

socket            = /tmp/mysql.sock

skip-locking

key_buffer = 160M

max_allowed_packet = 1M

table_cache = 64

sort_buffer_size = 512K

net_buffer_length = 8K

read_buffer_size = 256K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M

max_connections=1000

由于对mysql还不是很熟悉,所以很多参数没有修改。哈哈。。

2、非使用mysqld脚本自动启动的用户。

修改$MYSQL_HOME/bin/mysqld_safe文件

例如:/usr/local/mysql/bin/mysqld_safe这个文件

grep -n 'max_connection' $MYSQL_HOME/bin/mysqld_safe

修改对应行号的max_connections参数值

以上方法为参考网上的做法写的。

方法二:

在PHP手册里面找关于mysql_connect和mysql_pconnect的资料,下面是在php手册中对这两个函数的描述:

 mysql_connect 函数原型:

     resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]])

返回:

     如果成功则返回一个MySQL 连接标识,失败则返回FALSE。

描述:

     mysql_connect() 建立一个到MySQL 服务器的连接。当没有提供可选参数时使用以下默认值:server = 'localhost:3306',username =

服务器进程所有者的用户名,password = 空密码。

    如果用同样的参数第二次调用mysql_connect(),将不会建立新连接,而将返回已经打开的连接标识。参数new_link 改变此行为并使

mysql_connect() 总是打开新的连接,甚至当mysql_connect() 曾在前面被用同样的参数调用过。参数client_flags 可以是以下常量的组合

:MYSQL_CLIENT_COMPRESS,MYSQL_CLIENT_IGNORE_SPACE 或者MYSQL_CLIENT_INTERACTIVE。

     注: new_link 参数自PHP 4.2.0 起可用。

         client_flags 参数自PHP 4.3.0 起可用。  

一旦脚本结束,到服务器的连接就会被关闭。除非之前已经调用了mysql_close() 来关闭它。

mysql_pconnect :

函数原型:

resource mysql_pconnect ( [string server [, string username [, string password [, int client_flags]]]])

返回:

如果成功则返回一个正的MySQL 持久连接标识符,出错则返回FALSE。

描述:

     mysql_pconnect() 建立一个到MySQL 服务器的连接。如果没有提供可选参数,则使用如下默认值:server = 'localhost:3306',

username = 服务器进程所有者的用户名,password = 空密码。client_flags 参数可以是以下常量的组合:MYSQL_CLIENT_COMPRESS,

MYSQL_CLIENT_IGNORE_SPACE 或者MYSQL_CLIENT_INTERACTIVE。

     server 参数也可以包括端口号,例如"hostname:port",或者是本机套接字的的路径,例如":/path/to/socket"。

     注: 对":port" 的支持是3.0B4 版添加的。

         对":/path/to/socket" 的支持是3.0.10 版添加的。                                            两者之间的区别 :

mysql_pconnect() 和mysql_connect() 非常相似,但有两个主要区别。

     首先,当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不打开新连接。

     其次,当脚本执行完毕后到SQL 服务器的连接不会被关闭,此连接将保持打开以备以后使用(mysql_close() 不会关闭由mysql_pconnect() 建立的连接)。

     可选参数client_flags 自PHP 4.3.0 版起可用。此种连接称为"持久的"。

 

     小结一下,要保证你的系统不会出现Too many connections 错误,需要注意两点:

     1.保证你的apache的最大进程数不超过mysql的最大连接数;

     2.不要在程序里面用过多mysql_pconnect连接到同一个数据库服务器(一个就够了).这需要好的编码习惯和规范.特别是不断的给系统增加

新的功能,如果不注重系统架构和编码规范,当系统的复杂度到了一定的程度,整个系统就变得无法维护了.出现问题的时候解决起来就很麻烦了.

解决方法是修改/etc/mysql/my.cnf,添加以下一行:

set-variable = max_connections=500

或在启动命令中加上参数max_connections=500

就是修改最大连接数,然后重启mysql.默认的连接数是100,太少了,所以容易出现如题错误

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
When should you use a composite index versus multiple single-column indexes?When should you use a composite index versus multiple single-column indexes?Apr 11, 2025 am 12:06 AM

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)Apr 10, 2025 am 09:36 AM

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL: Essential Skills for DevelopersMySQL and SQL: Essential Skills for DevelopersApr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Describe MySQL asynchronous master-slave replication process.Describe MySQL asynchronous master-slave replication process.Apr 10, 2025 am 09:30 AM

MySQL asynchronous master-slave replication enables data synchronization through binlog, improving read performance and high availability. 1) The master server record changes to binlog; 2) The slave server reads binlog through I/O threads; 3) The server SQL thread applies binlog to synchronize data.

MySQL: Simple Concepts for Easy LearningMySQL: Simple Concepts for Easy LearningApr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL: A User-Friendly Introduction to DatabasesMySQL: A User-Friendly Introduction to DatabasesApr 10, 2025 am 09:27 AM

The installation and basic operations of MySQL include: 1. Download and install MySQL, set the root user password; 2. Use SQL commands to create databases and tables, such as CREATEDATABASE and CREATETABLE; 3. Execute CRUD operations, use INSERT, SELECT, UPDATE, DELETE commands; 4. Create indexes and stored procedures to optimize performance and implement complex logic. With these steps, you can build and manage MySQL databases from scratch.

How does the InnoDB Buffer Pool work and why is it crucial for performance?How does the InnoDB Buffer Pool work and why is it crucial for performance?Apr 09, 2025 am 12:12 AM

InnoDBBufferPool improves the performance of MySQL databases by loading data and index pages into memory. 1) The data page is loaded into the BufferPool to reduce disk I/O. 2) Dirty pages are marked and refreshed to disk regularly. 3) LRU algorithm management data page elimination. 4) The read-out mechanism loads the possible data pages in advance.

MySQL: The Ease of Data Management for BeginnersMySQL: The Ease of Data Management for BeginnersApr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment