Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I ping mysql server before each query?

Tags:

php

mysql

ping

So I was wondering whether I should or should not ping the mysql server (mysqli_ping) to ensure that the server is always alive before running query?

like image 379
Rihards Avatar asked Jun 23 '10 17:06

Rihards


People also ask

Can you ping a MySQL server?

From the docs: MySQL Connector/J has the ability to execute a lightweight ping against a server, in order to validate the connection.

Why is MySQL running slow?

Queries can become slow for various reasons ranging from improper index usage to bugs in the storage engine itself. However, in most cases, queries become slow because developers or MySQL database administrators neglect to monitor them and keep an eye on their performance.

How do I test MySQL connection?

To test the connection to your database, run the telnet hostname port on your Looker server. For example, if you are running MySQL on the default port and your database name is mydb, the command would be telnet mydb 3306 . If the connection is working, you will see something similar to this: Trying 10.10.

Is MySQL server necessary?

You do not need the full MySQL server installed on the web server. Ideally it makes sense to have the client and server using the exact same version of MySQL, as they will support exactly the same features, but it's not completely necessary.


1 Answers

You shouldn't ping MySQL before a query for three reasons:

  1. Its not a reliable way of checking the server will be up when you attempt to execute your query, it could very well go down in the time between the ping response and query.
  2. Your query may fail even if the server is up.
  3. As the amount traffic to your website scales up, you will be adding a lot of extra overhead to the database. Its not uncommon in enterprise apps that have used this method to see a huge amount of the database's resources getting wasted on pings.

The best way to deal with database connections is error handling (try/catch), retries and transactions.

More on this on the MySQL performance blog: Checking for a live database connection considered harmful

In that blog post you'll see 73% of the load on that instance of MySQL was caused by applications checking if the DB was up.

like image 114
Sean Amos Avatar answered Oct 22 '22 00:10

Sean Amos