WP_CLI\Utils

get_mysql_binary_path()WP-CLI 2.12.0

Get the path to the MySQL or MariaDB binary.

If the MySQL binary is provided by MariaDB (as determined by the version string), prefers the actual MariaDB binary.

No Hooks.

Returns

String. Path to the MySQL/MariaDB binary, or an empty string if not found.

Usage

get_mysql_binary_path();

Changelog

Since 2.12.0 Introduced.
Since 2.12.0 Now also checks for MariaDB.

get_mysql_binary_path() code WP-CLI 2.13.0-alpha

function get_mysql_binary_path() {
	static $path = null;

	if ( null !== $path ) {
		return $path;
	}

	$path    = '';
	$mysql   = Process::create( '/usr/bin/env which mysql', null, null )->run();
	$mariadb = Process::create( '/usr/bin/env which mariadb', null, null )->run();

	$mysql_binary   = trim( $mysql->stdout );
	$mariadb_binary = trim( $mariadb->stdout );

	if ( 0 === $mysql->return_code ) {
		if ( '' !== $mysql_binary ) {
			$path   = $mysql_binary;
			$result = Process::create( "$mysql_binary --version", null, null )->run();

			// It's actually MariaDB disguised as MySQL.
			if ( 0 === $result->return_code && false !== strpos( $result->stdout, 'MariaDB' ) && 0 === $mariadb->return_code ) {
				$path = $mariadb_binary;
			}
		}
	} elseif ( 0 === $mariadb->return_code ) {
		$path = $mariadb_binary;
	}

	return $path;
}