Unexpected error "Too many connections"

02-09-2019, 11:16 AM

Hello everyone,

my name is Roman and I need help.

In a nutshell: for the last 2 months I've been trying to fix issue with my application which is using Percona XtraDB Cluster 5.7. From time to time (from 1 to 10 days) mysql becomes unavailable: all new connections get error message "Too many connections", but it looks like that mysql does nothing (low CPU usage and disk i/o). Only "kill -9" and restart of mysql server can make it available.

Below I'll provide more details. I'll be pleasant for any ideas what can I try to fix the issue.

Several words about my application and data. I have PHP based application which is working with 5 mysql shards. Shards are independent on each other. Each shard have master-slave replication to backup server. So, I have 10 servers: 5 masters and 5 replicas. Application is working only with master servers, once a day special cron task takes a backup from each replica by using Percona XtraBackup.

Data is located in /srv/local partition, there are enough room for the data on this partition.

My databases are big: each shard have size ~500 Gb. There are about 50 tables in my DBs, but only several of them contains almost all data. All shards have roughly speaking the same size, the same number of requests, the same number of clients. One important fact: I faced my issue only on one shard.

My application is using a concept of "workers". Worker is a short php-program which usually working in the following way:
1. start and establish connection to mysql-server,
2. get some data from DB,
3. get some data from external API,
4. do some calculations and put data back to DB,
5. close connection to DB.

In common case I have not more than 200 workers working with each shard and I have max_connections variable in mysql-server set to value 1000. In common case it's much more then enough to work wihout any issues. Workers are usually working very fast: some of them can do their job during several seconds, but some of them can work during minutes or even dozens of minutes. Usually I have hundreds workers starts and finishes per every minute.

When I faced issue with "Too many connections error" I checked that I didn't have more workers then usual. No one besides workers can connect to DB. First of all I tried to optimize my application and separate queries which update a lot of data to smaller queries and distribute them in time. It didn't help.

Then I tried to compare settings between all my DB servers and found, that problem server has default value of variable binlog_cache_size - 32Kb, whereas on other servers this variable have value 100Mb. Then I also found that value of binlog cache usage is enormous on problem server - millions per day, whereas on other servers it's something like couple of thousands each day.

When I set binlog_cache_size to 100 Mb on problem server it was working without problems for 10 days, before that it was working not longer then 2-3 days. But after 10 days it again becames unavailable with the message "Too many connections". I also noticed that when mysql-server starts serving error "Too many connections" it's extensively using CPU and doing lot of writes on disk. But after couple of minutes CPU usage and i/o goes down almost to 0, but mysq still serving error "Too many connections".

I have logs from atop utility, which allows to log system state every minute. Below you can find a couple of screenshots made at the moment of error starts at 10:33.

I didn'y have general log enabled on my problem server and I decided to enable it (writing data to file) in hope to find something interesting in it at the moment of error "Too many connections" begins. I haven't found anything yet in this log, but my mysql server broke down twise during 2 days I enabled this log. Looks like there too many disk i/o operation and this might be a reason of error. I have these SSDs in my server: https://www.micron.com/products/soli...uct-lines/5100 and I found that they can deal with up to 500 Mb/s of writes and 43000 iops. Look like I didn't reach these limits in my case.

Does anyone has any ideas what should I try to find and fix the reason of issues with my DB server?

Comment

I suspect we may need more data but first point to check is when you hit issue due to see flow control. Can you share output of "show processlist" and show status like 'wsrep%'
It would be great to run pt-pmp (check percona toolkit) to get trace of what each thread is doing.

But the problem is that in time when I have "Too many connections" error values of "Threads_running" and other related counters are not high. Here is screenshot from the last outage:
I don't know if it just monitoring tool can't collect data because of outage, or it's a real values. Is it worth setting trigger on "Max_used_connections" variable? Something like this:

Comment

You have gaps in your graphs, which likely means the monitoring agent could already not connect to the database due to the problem with avaialble connections.
To avoid that, configure an extra TCP port for your database instances and use that one for monitoring as well as for pt-stalk itself.
Check here: https://www.percona.com/doc/percona-...tml#extra_port

For pt-stalk in this case, I'd rather use something like --threshold=400 --variable=Threads_connected

Comment

today my DB again became unavailable with "Too many connections" error. And what is suspicious - last time this problem happened exactly 10 days ago. And previous outage (with the same DB settings) also was 10 days before.

Unfortunately pt-stalk didn't collect any data, maybe because I set threshold to 900, now I decreased it to 400:

Comment

Does anyone knows if the field "Affects Version/s:" in Percona's Jira contain exact list of affected versions or approximate list?

In the issue https://jira.percona.com/browse/PS-4716 field "Affected versions" contains values "5.7.22-22, 5.7.23-23". Before my DB was updated to 5.7.23 it had version 5.7.21 and I want to know if this version also had this bug or it was introduced in 5.7.22.

Comment

more then 2 weeks passed from the moment I upgraded my mysql server from 5.7.23 to 5.7.25 (I'd done it at 25.02.2019). From that time server didn't have "Too many connections" error, looks like this upgrade fixed the issue. But unfortunately this upgrade dramatically decreased performance, value of innodb row lock time increased significantly:
Does someone have any ideas what can I check to fix this performance issue?