8000 GitHub - voku/mysql: Async MySQL database client for ReactPHP.
[go: up one dir, main page]

Skip to content

voku/mysql

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MySQL

Build Status

Async MySQL database client for ReactPHP.

This is a MySQL database driver for ReactPHP. It implements the MySQL protocol and allows you to access your existing MySQL database. It is written in pure PHP and does not require any extensions.

Usage

Connection

The Connection is responsible for communicating with your MySQL server instance, managing the connection state and sending your database queries. It also registers everything with the main EventLoop.

$loop = React\EventLoop\Factory::create();

$options = array(
    'host'   => '127.0.0.1',
    'port'   => 3306,
    'user'   => 'root',
    'passwd' => '',
    'dbname' => '',
);

$connection = new Connection($loop, $options);

If you need custom connector settings (DNS resolution, TLS parameters, timeouts, proxy servers etc.), you can explicitly pass a custom instance of the ConnectorInterface:

$connector = new \React\Socket\Connector($loop, array(
    'dns' => '127.0.0.1',
    'tcp' => array(
        'bindto' => '192.168.10.1:0'
    ),
    'tls' => array(
        'verify_peer' => false,
        'verify_peer_name' => false
    )
));

$connection = new Connection($loop, $options, $connector);

connect()

The connect(callable $callback): void method can be used to connect to the MySQL server.

It accepts a callable $callback parameter which is the handler that will be called when the connection succeeds or fails.

$connection->connect(function (?Exception $error, $connection) {
    if ($error) {
        echo 'Connection failed: ' . $error->getMessage();
    } else {
        echo 'Successfully connected';
    }
});

This method should be invoked once after the Connection is initialized. You can queue additional query(), ping() and close() calls after invoking this method without having to await its resolution first.

This method throws an Exception if the connection is already initialized, i.e. it MUST NOT be called more than once.

Install

The recommended way to install this library is through Composer. New to Composer?

This will install the latest supported version:

$ composer require react/mysql:^0.3.2

See also the CHANGELOG for details about version upgrades.

This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.4 through current PHP 7+ and HHVM. It's highly recommended to use PHP 7+ for this project.

Tests

To run the test suite, you first need to clone this repo and then install all dependencies through Composer:

$ composer install

The test suite contains a number of functional integration tests that send actual test SQL queries against your local database and thus rely on a local MySQL test database with appropriate write access. The test suite creates and modifies a test table in this database, so make sure to not use a production database! You can change your test database credentials by passing these ENV variables:

$ export DB_HOST=localhost
$ export DB_PORT=3306
$ export DB_USER=test
$ export DB_PASSWD=test
$ export DB_DBNAME=test

For example, to create an empty test database, you can also use a temporary mysql Docker image like this:

$ docker run -it --rm --net=host \
    -e MYSQL_RANDOM_ROOT_PASSWORD=yes -e MYSQL_DATABASE=test \
    -e MYSQL_USER=test -e MYSQL_PASSWORD=test mysql

To run the test suite, go to the project root and run:

$ php vendor/bin/phpunit

License

MIT, see LICENSE file.

This is a community project now managed by @friends-of-reactphp. The original implementation was created by @bixuehujin starting in 2013 and has been migrated to @friends-of-reactphp in 2018 to help with maintenance and upcoming feature development.

The original implementation was made possible thanks to the following projects:

  • phpdaemon: the MySQL protocol implementation is based on code of this project (with permission).
  • node-mysql: the API design is inspired by this project.

About

Async MySQL database client for ReactPHP.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%
0