Parse mysql client version

All we need is an easy explanation of the problem, so here it is.

I need to programmatically call mysqldump with certain parameters. I try to use either the --source-data option or the --master-data option.

There is the following phrase in the documentation:

From MySQL 8.0.26, use --source-data, and before MySQL 8.0.26, use
--master-data

In order to use the correct parameter, I need to determine the version of the MySQL client. But here I face an issue. I ran the mysqldump –version command on different servers and found that the version is not formalized, and is output in different formats on different servers.

On one server, I got the following result:

mysqldump Ver 8.0.28-0ubuntu0.20.04.3 for Linux on x86_64 ((Ubuntu))

And the following on the other server:

mysqldump Ver 10.13 Distrib 5.7.37, for Linux (x86_64)

The format is obviously different. In this case, the version is in different positions. On the first server version is 8.0.28, on the other, it is 5.7.37. Perhaps there are other formats? How do I get the version of the client simply in the major.minor.path format?

It seems that the regular expression \d*\.\d*\.\d* is an appropriate option. But on other hand, it seems like an unreliable solution, since there may be more formats for how to output the MySQL client version.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I found three options for how to get the version.

In Linux

You can find out the version of the installed mysql-client package:

sudo apt show mysql-client | grep Version

Version: 8.0.29-0ubuntu0.20.04.3

Here the version format is the same for everyone.

The version can be extracted by one line:

sudo apt show mysql-client | grep Version | sed 's/Version: //' | sed 's/-.*//'

In Windows

The easiest way is to take the version of the mysql.exe file itself:

Parse mysql client version

more details on how to do it programmatically (c#):

https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.fileversioninfo.fileversion?view=net-6.0

Alternative

You can simply find out if the command is supported by a given version of the client by executing mysql --help and check if the desired parameter in output text.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply