giant-rabbit/mysql-concentrator
Composer 安装命令:
composer require giant-rabbit/mysql-concentrator
包简介
A MySQL proxy server written in PHP
README 文档
README
This is a MySQL proxy server that takes several MySQL connections and "concentrates" them into a single connection to a single server. This may not seem very useful until you think about tests. One of the annoying things when doing tests on web applications is that you frequently have to do bunch of TRUNCATE or DROP TABLE statements between each test to get your database back into a known state. Doing that really slows your tests down.
Ruby on Rails gets around this to some extent by wrapping each test in a transaction (BEGIN ... ROLLBACK). It's very fast and works really well, but it only works because Rails tests all run in one process with one database connection. This technique breaks down in Rails when you introduce Cucumber tests because it launches separate web server and browser processes, so you can't wrap all the database calls in a transaction.
MySQL Concentrator helps with this problem. You can start a connection to MySQL through MySQL Concentrator in your test suite and configure your web application to also run MySQL commands through MySQL Concentrator. Then have your test suite fire a BEGIN before each test and a ROLLBACK after each test. Even if you fire off a bunch of separate processes, MySQL Concentrator will funnel all those connections into that same connection where you sent the BEGIN command and so all the database activity will happen in a transaction.
Installation
Just grab it from the github repository. It just runs out of its directory.
Usage
php mysql-concentrator.php -h <mysql server host name> -p <mysql server port>
Configure your web application to connect to mysql on 127.0.0.1 at port 3307 instead of its normal host and port. It's normal host and port are what you should use for the command above.
Now if you are going to use it for a testing framework, just add some lines to start the transaction. Here's an example of what I did for some Behat tests in the Behat Context class:
/**
* @BeforeScenario
*/
public function setupDB($event)
{
$this->db = new PDO("mysql:host=127.0.0.1;port=3307;dbname=foo_test;", "foo", "foo");
$this->db->exec("DROP TABLE IF EXISTS automated_testing");
$this->db->exec("CREATE TABLE automated_testing (pristine INTEGER)");
$this->db->exec("INSERT INTO automated_testing VALUES (1)");
$this->db->exec("BEGIN");
$this->db->exec("UPDATE automated_testing SET pristine = 0");
}
/**
* @AfterScenario
*/
public function resetDB($event)
{
$this->db->exec("ROLLBACK");
}
The extra stuff with "pristine" is a sort of hack so I can check and see if the web app did something to mess up the wrapping transaction. If the transaction didn't get rolled back properly "pristine" won't get set back to 1. Some statements in MySQL will cause an implicit commit. This includes pretty much all of the DDL statements like CREATE TABLE, ALTER TABLE, CREATE INDEX etc...
giant-rabbit/mysql-concentrator 适用场景与选型建议
giant-rabbit/mysql-concentrator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50 次下载、GitHub Stars 达 0, 最近一次更新时间为 2013 年 11 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 giant-rabbit/mysql-concentrator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 giant-rabbit/mysql-concentrator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 50
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-11-07