slam/mysql-php

PHP version of mysql cli that comes with MySQL

v1.1.0 2023-01-04 10:23 UTC

README

Latest Stable Version Downloads Integrate Code Coverage

PHP light version of mysql cli that comes with MySQL.

Why

  1. You are inside a PHP only environment, like a PHP Docker image
  2. You need to import a large mysql dump
  3. You don't have access to the native mysql client

Performance

Speed is exactly the same of the original mysql binary thanks to streams usage.

Supported formats

Input type Example Supported?
mysqldump output as is ✔️
Single query on single line SELECT NOW(); ✔️
Single query on multiple lines SELECT
NOW();
✔️
Multiple queries on separated single or multiple lines SELECT NOW();
SELECT
NOW();
✔️
Multiple queries on single line SELECT NOW();SELECT NOW();

Usage

The library provides two usages, the binary and the \SlamMysql\Mysql class.

From CLI

$ ./mysql -h
Usage: mysql [OPTIONS]
  --host       Connect to host     [Default: INI mysqli.default_host]
  --port       Port number         [Default: INI mysqli.default_port]
  --username   User for login      [Default: INI mysqli.default_user]
  --password   Password to use     [Default: INI mysqli.default_pw]
  --database   Database to use     [Default: empty]
  --socket     The socket file     [Default: INI mysqli.default_socket]

$ printf "CREATE DATABASE foobar;\nSHOW DATABASES;" | ./mysql
information_schema
foobar
mysql
performance_schema
sys

$ ./mysql --database foobar < foobar_huge_dump.sql

From PHP

$mysql = new \SlamMysql\Mysql('localhost', 'root', 'pwd', 'my_database', 3306, '/socket');
$return = $mysql->run(\STDIN, \STDOUT, \STDERR);
exit((int) (true !== $return));

\SlamMysql\Mysql::run accepts any type of resource consumable by fgets/fwrite functions.

Related projects

  1. ifsnop/mysqldump-php: mysqldump binary port in pure PHP