Parse mysql client version

The question:

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.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

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.


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 Comment