How can MySQL be configured for long and short connections?

In MySQL, you can set up long connections and short connections using the following methods:

  1. Long connection: This can be achieved by setting the “wait_timeout” parameter of the MySQL server. This parameter indicates the number of seconds that the MySQL server waits before closing idle connections. By default, the value of wait_timeout is set to 28800 seconds (8 hours).

You can use the following command to check the current value of wait_timeout:

SHOW VARIABLES LIKE 'wait_timeout';

You can set the wait_timeout value using the following command:

SET GLOBAL wait_timeout = 600;

This will set the wait_timeout to 600 seconds (10 minutes). It is important to note that this setting only takes effect on newly established connections, existing connections will need to reconnect to take effect.

  1. Short connection: A short connection refers to immediately disconnecting from the database after each operation is completed. This can be achieved by the following methods:

When connecting using the MySQL client, add the -C parameter to indicate the use of compression protocol. For example:

mysql -C -h hostname -u username -p

One advantage of using short connections is to save resources, but since each connection needs to be reestablished each time, it may increase the workload on the database server.

bannerAds