MySQL Error "Too Many Connections"

How to resolve Too many connections

You should not close connections; Go comes indeed with some connection pooling.

You should also use Exec() instead of QueryRow, because you are keeping connection open since MySQL still has to send data (although it looks odd).

func InsertPlayer(player Player, db *sql.DB) {
res, err := db.Exec("insert into players (muted,user_id,volume,play_back_rate) values (?,?,?,?)",
player.Muted,
player.UserId,
player.Volume,
player.PlayBackRate)
if err != nil {
// and handle errors!
}
fmt.Println(res.RowsAffected())
}

How to fix: mysql_connect(): Too many connections

Most probably, you have been a subject of a DDoS attack.

People on this forum complain on exactly same thing with exactly same provider.

The answer is this:

VB told me it was a DOS attack - here is their message:

This is not an 'exploit'. This is a DoS attack (Denial of Service). Unfortunately there is nothing we can do about this. DoS attacks can only be fought at the server or router level, and this is the responsibility of your host. Instead of doing this they have decided to take the easy way out and suspend your account.

If you cannot get them to take this seriously, then you should look for another host. Sorry for the bad news.

A possible workaround can be this: if your connection fails with mysql_connect(): Too many connections, you don't quit, but instead sleep() for half a second and try to connect again, and exit only when 10 attempts fail.

It's not a solution, it's a workaround.

This of course will delay your page loading, but it's better than an ugly too many connections message.

You also can come with a some kind of a method which tells bots and browsers apart.

Like, set a salted SHA1 cookie, redirect to the same page and then check that cookie and connect to MySQL only if the user agent had passed the test.

Golang, mysql: Error 1040: Too many connections

sql.Open doesn't really open a connection to your database.

A sql.DB maintains a pool of connections to your database. Each time you query your database your program will try to get a connection from this pool or create a new one otherwise. These connections are than put back into the pool once you close them.

This is what rows.Close() does.
Your db.QueryRow("...") does the same thing internally when you call Scan(...).

The basic problem is that you're creating too many queries, of which each one needs a connection, but you are not closing your connections fast enough. This way your program has to create a new connection for each query.

You can limit the maximum number of connections your program uses by calling SetMaxOpenConns on your sql.DB.

See http://go-database-sql.org/surprises.html for more information.



Related Topics



Leave a reply



Submit