simple-swoole / simps

🚀 A simple, lightweight and high-performance PHP coroutine framework.
https://simps.io
Apache License 2.0
469 stars 48 forks source link

Multiple databases #9

Closed vladsvd closed 4 years ago

vladsvd commented 4 years ago

How to connect to multiple databases? config/database.php only the pool to one database is calculated.

sy-records commented 4 years ago

Need to see it. Doesn't seem to be supported right now.

sy-records commented 4 years ago

You can do this.

$db = new DB();
// View the default connected database table
$table = $db->query("show tables");
var_dump($table);

// Switching the test database
$db->exec("use test");
$table = $db->query("show tables");
var_dump($table);

// You need to switch back to the default database after completing the query,
// Prevent other connections from getting the test database.
$database = config('database.database');
$db->exec("use {$database}");
$table = $db->query("show tables");
var_dump($table);
vladsvd commented 4 years ago

This is a good solution if we have different databases on the same server. But most of all, swoole is used in systems, where you need to connect to different databases on different physical machines. I see a solution in creating several independent connection pools, where each pool will be connected to its database on the specified ip.

sy-records commented 4 years ago

It's now using the Swoole Library, You can try to load other connection pool components using composer, or you can try assigning multiple connection pools yourself, we usually make sure one connection pool is the same database!