How can I detect whether connected database is Mar

2019-04-17 14:12发布

问题:

My PHP application has requirements, including "either MySQL 5.7+ or MariaDB 10.2+". How can I tell which of these alternatives is satisfied?

I know how to compare version numbers, and also to get the version number from the database, but I don't know how to determine which kind of database it is.

I tried

select version()

That returns only the version number and server OS information, but not the database kind.

回答1:

Look in VARIABLES for aria_block_size. Its existence almost certainly implies some version of MariaDB and not MySQL, nor Percona. (At least for the near future.)

The beginning part of version:

  • "5.7" and "8.0" Imply MySQL or Percona; it will not imply MariaDB.
  • "10.2" Implies MariaDB; it is unlikely for MySQL and Percona to get to "10" for a long time.

What feature are you needing? There is a chance that Percona will retrofit something from MariaDB 10.2 before it comes from MySQL.

Even when version is 5.1.53-rel11.7-log or 5.5.35-0ubuntu0.12.04.2-log, the first part gives you the important part of the MySQL/MariaDB/Percona version.

Percona versions look like: 5.5.31-30.3-log, 5.6.30-76.3-56-log, 5.6.19-67.0-log -- Note the extra 2 or 3 numbers after the initial 3.

MariaDB always starts with N.N.N-MariaDB

Oracle's MySQL starts with N.N.N, but might continue with -enterprise (paid version), -community (free version), -0ubuntu0 (ported by Ubuntu), -Debian, etc.

MariaDB: 5.1, 5.2, 5.3, 5.4, 5.5, 10.0, 10.1, 10.2
Oracle and Percona: 5.1, 5.5, 5.6, 5.7, 8.0

The 8.0 cycle is just beginning. This means that 5.6 will soon be closing down and 5.7 has some life left, but not much new will be but in it.

Clarification of what is a "major" release.
For MySQL (and Percona), these are "major", and they are not consecutive': 5.1, 5.5, 5.6, 5.7, 8.0.
For MariaDB: 5.1, 5.2, 5.3, 5.4, 5.5, 10.0, 10.1, 10.2.



回答2:

$info = $pdo->query("SHOW VARIABLES like '%version%'")->fetchAll(PDO::FETCH_KEY_PAIR);
$server_vendor  = strtok($info['version_comment']," ");
$server_version = $info['version'];


回答3:

if you are trying to do this from inside the php app you can ( which php version?) for PHP 7 use mysqli_get_server_info; or < php 7, use mysql_get_Server_info

    <?php
    $link = mysqli_connect("localhost", "my_user", "my_password");

    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    /* print server version */
    printf("Server version: %s\n", mysqli_get_server_info($link));

    /* close connection */
    mysqli_close($link);
    ?>

will return

Server version: 5.5.5-10.1.23-MariaDB-9+deb9u1

php 7 http://php.net/manual/en/mysqli.get-server-info.php php 5 & < http://php.net/manual/en/function.mysql-get-server-info.php