Is there any way to reset mysql IDLE/Sleep connections? - mysql

I have a problem, my app have to open a lot of connections with MySQL and write data into it, but for after some time I'm getting more and more connections that are in idle/sleep state, and my app's speed reduces until it completely stops. I blame 'wait_timeout' variable to be too high,
mysql> show variables like '%timeout%';
+----------------------------+----------+
| Variable_name | Value |
+----------------------------+----------+
| connect_timeout | 10 |
| delayed_insert_timeout | 300 |
| innodb_lock_wait_timeout | 50 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 28800 |
| lock_wait_timeout | 31536000 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| wait_timeout | 28800 |
+----------------------------+----------+
10 rows in set (0.05 sec)
and
mysql> SHOW FULL PROCESSLIST;
+------+----------+----------------------------------------------------+-------------+---------+------+-------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+------+----------+----------------------------------------------------+-------------+---------+------+-------+-----------------------+
| 5425 | root | ip-10-xxxxxxxx.ec2.internal:60544 | my_db | Sleep | 2344 | | NULL |
| 5426 | root | ip-10-xxxxxxxx.ec2.internal:60561 | my_db | Sleep | 2136 | | NULL |
And there are a lot of connections like this.
So my primary question is how to reset connection? I know I can just kill PID but I would really like to reset it.
And do you think wait_timeout would solve an issue. By the way everything is on the Amazon RDS.

I would suggest that wait_timeout is probably the wrong solution, because, as #datasage implied, your application may not handle it gracefully. The solution is for your application to properly close the connections when it is finished with them.
KILL thread_id is the correct way to reset it from the server side, but that's just a patch for the actual solution, assuming this is actually what's slowing down your application.
I commonly have hundreds of sleeping threads on my MySQL servers, because the application holds them in a pool for reuse. Sleeping threads are sleeping. They aren't slowing anything down unless you have so many of them that you're hitting the max_connections limit on RDS.

You could reduce wait_timeout but you will need to be confident that your application can handle a connection being closed by the server instead of the client.

Related

ERROR 2013 (HY000): Lost connection to MySQL during query

I have a remote MySQL database, and my deployed application (a few java microservice applications) consistently encounters the error ERROR 2013 (HY000): Lost connection to MySQL during query with some SQL queries. Then I tried to connect to mysql with MySQL command line client, executed the same query, and I managed to replicate the same error. Below is Mysql setup after following this page https://dev.mysql.com/doc/refman/5.7/en/error-lost-connection.html
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| connect_timeout | 3600 |
| delayed_insert_timeout | 300 |
| have_statement_timeout | YES |
| innodb_flush_log_at_timeout | 1 |
| innodb_lock_wait_timeout | 5 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 7200 |
| lock_wait_timeout | 50 |
| net_read_timeout | 3000 |
| net_write_timeout | 6000 |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_stop_slave_timeout | 31536000 |
| slave_net_timeout | 4 |
| wait_timeout | 7200 |
+------------------------------+----------+
+--------------------------+------------+
| Variable_name | Value |
+--------------------------+------------+
| max_allowed_packet | 1073741824 |
| slave_max_allowed_packet | 1073741824 |
+--------------------------+------------+
However, I systematically have the same error after roughly 10 minutes after executing a big SQL query from the mysql command line client.
Note: On the DB server side, I notice the log [Note] Aborted connection xxxxx to db: 'xxxxx' user 'xxxxx' host 'xxxxx' (Got an error writing communication packets)
DBA executed the query successfully on the server on which mysql is installed. But we have to use mysql remotely as a service.
I will be grateful if anybody can help.
Finally, the root cause was a network issue. Our remote MySQL instance is exposed as service. There's an additional proxy between the client and MySQL DB. By default, the connection will be closed if no communication between client and server over 10 mins.

Mysql 5.5 cannot reconnect after a several minutes

I used mysql 5.5 in my project and encontered a problem that I can't solve. I googled but without result. My client can connect the server normally however after a while about 10 minutes the connection lost. The client would reconnect the server but it hangs up and never return a connection. I tried with two kinds of clients: The native mysql console client and the mysql workbench and same result. Here are some details:
MariaDB [(none)]> show variables like "%timeout%";
+----------------------------+----------+
| Variable_name | Value |
+----------------------------+----------+
| connect_timeout | 10 |
| deadlock_timeout_long | 50000000 |
| deadlock_timeout_short | 10000 |
| delayed_insert_timeout | 300 |
| innodb_lock_wait_timeout | 50 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 28800 |
| lock_wait_timeout | 31536000 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| thread_pool_idle_timeout | 60 |
| wait_timeout | 28800 |
+----------------------------+----------+
so the wait_timeoutand interactive_timeout seems normal and I can't get some other logs about the problem.
EDIT: I find this problem only occures when I connect the server via remote clients. The local client doesn't have the problem.

MySQL Connections causing server went away, nothing in processlist

I have a large amount of connections but when I issue a show full processlist I am not showing anything close to the connections I see. Are these connections orphans of some sort? I tried the flush hosts command and the connections persist, even with a reboot of the server and also restarting the mysql server.
I believe these connections are causing issues with making new connections to the database. User's are getting a "server went away" error. How do I clear these?
See commands below:
mysql> show status like '%onn%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| Aborted_connects | 5 |
| Connections | 11743 |
| Max_used_connections | 24 |
| Ssl_client_connects | 0 |
| Ssl_connect_renegotiates | 0 |
| Ssl_finished_connects | 0 |
| Threads_connected | 6 |
+--------------------------+-------+
7 rows in set (0.00 sec)
mysql> show full processlist;
+-------+---------+----------------------+--------------------+---------+-------+-------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-------+---------+----------------------+--------------------+---------+-------+-------+-----------------------+
| 4494 | rode | localhost:43411 | NULL | Sleep | 11159 | | NULL |
| 4506 | rode | localhost:43423 | information_schema | Sleep | 11159 | | NULL |
| 4554 | rode | localhost:43511 | performance_schema | Sleep | 11112 | | NULL |
| 11500 | ass | serv:1243 | Home-Tech | Sleep | 0 | | NULL |
| 11743 | root | localhost | NULL | Query | 0 | NULL | show full processlist |
| 11744 | ass | out:6070 | Home-Tech | Sleep | 4 | | NULL |
| 11745 | ass | out:6074 | HTGlobal | Sleep | 8 | | NULL
The MySQL server has gone away (error 2006) has two main causes
Server timed out and closed the connection. To fix, check that “wait_timeout” mysql variable in your my.cnf configuration file is large enough.
Server dropped an incorrect or too large packet. If mysqld gets a packet that is too large or incorrect, it assumes that something has gone wrong with the client and closes the connection. To fix, you can increase the maximal packet size limit “max_allowed_packet” in my.cnf file, eg. set max_allowed_packet = 128M, then sudo /etc/init.d/mysql restart.
there are two main ways to fix this. if the above change doesn't there may be an issue with your linux or windows mysql database server; you either need to increase ram on your server or watch it's process.
is this on a windows or linux box?

MySQL - High Connections

I run this command:
mysql> show status like '%onn%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| Aborted_connects | 0 |
| Connections | 37226 |
| Max_used_connections | 45 |
| Ssl_client_connects | 0 |
| Ssl_connect_renegotiates | 0 |
| Ssl_finished_connects | 0 |
| Threads_connected | 4 |
+--------------------------+-------+
I know I don't have a high traffic site, so the Connections number is confusing me.
I'm positive I close all my connections when I use PHP.
Are there other reasons why Connections keep growing?
As documented here:
Connections
The number of connection attempts (successful or not) to the MySQL
server.
It is not the number of simultaneous connections, but a cumulative number.
You can Run Below command to know Traffic on your server.
Show global status like '%Thread%';

MySQL logging confusion

I need to turn this on/enable it. I found some info on the net but am very confused now, I don't know where to start.
This is what I have so far. I logged onto MySQL as root and did:
show variables like '%log%';
and got:
Variable_name | Value |
+---------------------------------+----------------------------------+
| back_log | 50 |
| binlog_cache_size | 32768 |
| binlog_format | STATEMENT |
| expire_logs_days | 10 |
| general_log | ON |
| general_log_file | /var/lib/mysql/helloise.log |
| innodb_flush_log_at_trx_commit | 1 |
| innodb_locks_unsafe_for_binlog | OFF |
| innodb_log_buffer_size | 1048576 |
| innodb_log_file_size | 5242880 |
| innodb_log_files_in_group | 2 |
| innodb_log_group_home_dir | ./ |
| innodb_mirrored_log_groups | 1 |
| log | ON |
| log_bin | OFF |
| log_bin_trust_function_creators | OFF |
| log_bin_trust_routine_creators | OFF |
| log_error | /var/log/mysql/error.log |
| log_output | FILE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | OFF |
| log_slow_queries | OFF |
| log_warnings | 1 |
| max_binlog_cache_size | 4294963200 |
| max_binlog_size | 104857600 |
| max_relay_log_size | 0 |
| relay_log | |
| relay_log_index | |
| relay_log_info_file | relay-log.info |
| relay_log_purge | ON |
| relay_log_space_limit | 0 |
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/helloise-slow.log |
| sql_log_bin | ON |
| sql_log_off | OFF |
| sql_log_update | ON |
| sync_binlog | 0
I did: SET GLOBAL general_log = 1 to set general_log = ON. Will all queries be logged in /var/lib/mysql/helloise.log?
And when I try to access the mysql directory via helloises#helloise:/var/lib$ cd mysql/ I get permission denied
and then I also have a log directory:
helloises#helloise:/var/log$
with a whole lot of log files ex: mysql.log
Which log file will contain my queries so I can track what my db is doing and see how long queries take?
Firs of all you got a permission denied because you hav not the privileges to read it. Try a
ls -l
on that file and see what permission are defined. Then yuo can use chmod to change permissions. Or simply try to read that file as a root user.
The general log is a logging feature for query log, reading, from guide:
The general query log is a general record of what mysqld is doing. The
server writes information to this log when clients connect or
disconnect, and it logs each SQL statement received from clients. The
general query log can be very useful when you suspect an error in a
client and want to know exactly what the client sent to mysqld.
The error log:
The error log contains information indicating when mysqld was started
and stopped and also any critical errors that occur while the server
is running. If mysqld notices a table that needs to be automatically
checked or repaired, it writes a message to the error log.
For understand the differences i suggest you to read that site:
http://dev.mysql.com/doc/refman/5.1/en/server-logs.html
The file that contains the queries is the one in /var/lib/mysql/helloise.log
For the slow query logs you can check:
http://dev.mysql.com/doc/refman/5.1/en/slow-query-log.html
it logs all the query that take more time than long_query_time seconds (that variable must be defined by yourself). I don't found a way to show the execution time of a query, but check that page http://dev.mysql.com/doc/refman/5.1/en/log-destinations.html it could help you on how to configure general/slowquery logging.
Hope this help.