Menu

Sponsored By: Password Angel - Share passwords, API keys, credentials and more with secure, single-use links.

HowTo: Check how long a MySQL server has been running

MySQL is an open-source relational database management system (RDBMS) that uses structured query language (SQL) to manage and manipulate data. Like any other software application, it's important to be able to monitor and track the uptime and start time of the MySQL instance running on your system. Here are a few commands that can help you do that:

How long has the MySQL instance been running?

To find out how long your MySQL instance has been running, you can use the SHOW GLOBAL STATUS command with the LIKE clause and the Uptime variable name. The Uptime variable contains the number of seconds that the MySQL instance has been running since it was started. Here's an example command:

mysql> SHOW GLOBAL STATUS LIKE 'Uptime';

The output of this command will display the uptime value in seconds. You can convert this value into a more readable format, such as minutes, hours, or days, using a simple calculation.

For example, to convert the uptime value to minutes, you can divide the value by 60:

mysql> SHOW GLOBAL STATUS LIKE 'Uptime';
+---------------------------+--------+
| Variable_name             | Value  |
+---------------------------+--------+
| Uptime                    | 201380 |
+---------------------------+--------+
1 row in set (0.00 sec)
mysql> SELECT (Uptime / 60) AS uptime_minutes FROM information_schema.global_status;
+----------------+
| uptime_minutes |
+----------------+
| 3356.3333      |
+----------------+
1 row in set (0.00 sec)

This command will display the uptime value in minutes, or to get in hours you could use

mysql> SELECT (Uptime / 3600) AS uptime_hours FROM information_schema.global_status;
+----------------+
| uptime_hours |
+----------------+
| 59.9388      |
+----------------+
1 row in set (0.00 sec)

When was the MySQL server last started?

To find out when your MySQL instance was last started, you can use the performance_schema database and the global_status table to query the Uptime variable value and subtract it from the current Unix timestamp using the UNIX_TIMESTAMP() and FROM_UNIXTIME() functions. Here's an example command:

mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP() - variable_value) AS last_started
       FROM performance_schema.global_status 
       WHERE variable_name='Uptime';
+---------------------+
| last_started        |
+---------------------+
| 2022-10-05 09:23:21 |
+---------------------+
1 row in set (0.00 sec)

Enjoyed this article?

Thank you for reading this article! If you found the information valuable and enjoyed your time here, consider supporting my work by buying me a coffee. Your contribution helps fuel more content like this. Cheers to shared knowledge and caffeine-fueled inspiration!

Buy me a coffee

Originally published at https://chrisshennan.com/blog/howto-check-how-long-a-mysql-server-has-been-running

Subscribe to my newsletter...

... and receive the musings of an aspiring #indiehacker directly to your inbox once a month.

These musings will encompass a range of subjects such as Web Development, DevOps, Startups, Bootstrapping, #buildinpublic, SEO, personal opinions, and experiences.

I won't send you spam and you can unsubscribe at any time.