MySQL long connection with short connection

Source: Internet
Author: User
Tags socket error

What is a long connection?

In fact, a long connection is relative to the usual short connection, that is, long time to maintain the client and the server connection state.

The usual short connection operation steps are:

Connect-"Data transfer-" close the connection;

A long connection is usually:

Connect-"Data transmission-" Stay connected-"data transmission-" Stay connected-"...-" close the connection;

This requires a long connection in the absence of data communication, the timing of sending packets to maintain the status of the connection, the short connection in the absence of data transmission directly off the line

When to use long connections, short connections?

Long connections are primarily used for frequent communication between a small number of clients and the server, because at this time the socket error occurs frequently with short connections, and the frequent creation of socket connections is a waste of resources.

However, for the service side, long connections also consume a certain amount of resources, requiring specialized threads (which can be managed under Unix) to maintain the connection state.

In summary, the choice of long connections and short connections is subject to availability.

View MySQL Connection number

Mysqladmin-uroot-p processlist

In the actual test I found that when the Mysql_opt_reconnect is set to 1 o'clock, and then the processlist is checked out after the timeout, the automatically established connection is not in the list, but the connection is actually established and used.

In MySQL's default settings, if a database connection is not used for more than 8 hours (idle for 8 hours), the server disconnects the connection and subsequent query operations on that connection will fail. The problem is described very much on the network. The corresponding solutions are also provided. I'll mention some of my own ideas here.

Workaround One: Modify the configuration parameters of the MySQL server

The simple truth is that the default setting for MySQL is to disconnect the database after it has not been used for more than 8 hours, and if we change the time to a larger value, then the connection timeout will take longer, which means it is less likely to time out. The modification method provided on the network is generally modified/ETC/MY.CNF, adding a line in this file wait_timeout= the time-out you need to set. There is actually a relatively simple way to modify this parameter:

First log in to MySQL as a superuser, note that you must be a superuser, otherwise you will be prompted not to modify permissions later. And then enter

Show global variables like ' wait_timeout ';

The current time-out is displayed after the Enter execution:

+---------------+-------+

| variable_name | Value |

+---------------+-------+

| Wait_timeout | 28800 |

+---------------+-------+

1 row in Set (0.00 sec)

The default time-out, which is 8 hours (in seconds), is shown above. Now reset the parameter, for example we want to set the time-out to 10 hours, you can enter:

Set global wait_timeout=36000;

Enter execution, display:

Query OK, 0 rows Affected (0.00 sec)

Indicates a successful setup and can be re-validated using show global variables like ' wait_timeout '.

This method is more intuitive and the parameters set are immediately effective. However, if there is no configuration in/etc/my.cnf, the global variable will read the new variable value from/ETC/MY.CNF when the service is restarted.

For example, if the HTTP connection-alive is close, then the server receives a request and sends a response to close the connection, which is a short connection, if Connection-alive Keep-aliv E You can exchange multiple conversations in a TCP connection. Is this the definition? That's just a matter of procedural structure design.

Long connection short connection is only a conceptual problem, as long as you know its concept, not a special thing:
Long connection: After the system communication connection has been established, it has been maintained.
Short connection: Only the system needs to communicate with each other to establish the connection (client initiated), the request message received a response after the connection closed;
Communication entities using a long connection, generally also need to define the heartbeat message, sent periodically to detect whether the link between the system is abnormal, send a heartbeat at a certain time, if a certain number of times did not receive the heartbeat message, which is considered to be a problem with the connection, need to disconnect the connection re-establish.
The format of the specific heartbeat message, as well as the sending interval, and how many times the heartbeat is not received is considered a link anomaly, and whether the data part counts as a heartbeat message (some systems will clear the heartbeat if the packet is received is also equivalent to the system of packets also counted as heartbeat messages); This needs to be negotiated on both sides. For example, GSM commonly used short Message center and other Network entities Interconnect SMPP protocol, the requirement is to establish a long connection.

So the length of the connection is just a concept problem long-term connection socket, is the use of ordinary socket function, there is no special

The

database connection pool is provided by the database Manager. Regardless of the connection language. The
PHP provides a pconnect that can actually connect the pool.
For MySQL, the Mysql_pconnect function can be realized:
After the use of the library is not released, but to stay in the system, the next time the mysql_pconnect connected to the library, first check whether there is idle connection is not used, if so, use this Connection, rather than the operation of a connection to the repository, from the provincial system to the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.