Create new database on the fly in Laravel 4 - php

I want dynamically create new database, database user and password with privileges, create some tables in new database on the fly in Laravel 4 for every new user. This is for a multi tenant website.
Whats the best solution?
thanks.

This is not your first database connection it's easy, but you'll have to execute raw statements because database creation is no available as connection methods:
DB::statement(DB::raw('CREATE DATABASE <name>'));
To do that you can use a secondary connection:
<?php
return array(
'default' => 'mysql',
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
),
'store' => array(
'driver' => 'mysql',
'host' => 'host2',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
),
),
);
Then you can, during application bootstrap, change the database of the secondary connection:
DB::connection('store')->setDatabaseName($store);
or
Config::set('database.connections.store', $store);
And use the secondary connection in your queries:
$user = User::on('store')->find(1);
or
DB::connection('store')->select(...);

Related

Laravel 5.5 How to change DB_HOST on request

Ok, my system connects to remote databases on which I consistently change depending on what data I want to access.
The setup is multiple identical structure databases resides on different IP addresses, so I always update the .env DB_HOST line every time I want to access different database.
Now I'm planning to create a view on my system that will accepts an IP address and set it as the current database to use.
Referencing this answer on Laracasts:
Set two database connections in your config (one for your unchanging main connection, the other one for your custom dynamic connection):
'main' => array(
'driver' => 'mysql',
'host' => 'hostname',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'prefix' => '',
),
'dynamic' => array(
'driver' => 'mysql',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'prefix' => '',
),
Then change your database connection parameters for the second connection as needed:
Config::set('database.connections.dynamic.host', $newHost);
Config::set('database.connections.dynamic.username', $newUsername);
Config::set('database.connections.dynamic.password', $newPassword);
Config::set('database.connections.dynamic.database', $newDatabase);
Config::set('database.default', 'dynamic');
And reconnect the database:
DB::reconnect('mysql');

Illuminate Database connectivity with mysql router

I'm using Laravel's illuminate database library outside laravel with jessengers mongodb.
My requirement is multiple database connectivity through illuminate database.
Currently, I've added two connection one mysql and one mongodb.
To split the database load, I need to connect to mysql router instead of mysql db server directly. Also, in that I want one only for Read operation and one for Read/Write operation.
Kindly help me out on this.
Thanks in advance.
Current connections
$db = new Capsule;
$db->addConnection([
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'test',
'username' => 'test',
'password' => 'test#123#',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], "default");
$db->addConnection([
'driver' => 'mongodb',
'host' => '127.0.0.1',
'port' => 27017,
'database' => 'test',
'username' => null,
'password' => null,
'options' => []
], "mongodb");
$db->getDatabaseManager()->extend('mongodb', function ($config) {
return new Connection($config);
});
$db->setEventDispatcher(new Dispatcher(new Container));
$db->setAsGlobal();
$db->bootEloquent();
I need to replace one mysql connection with two mysql connection for Read and Read/Write operation through mysql router.
You can define read/write options separately with either mysql host or mysql router host and port
$db->addConnection([
'driver' => 'mysql',
'read' => [
'host' => '<mysql_router_host_ip>',
'port' => '<mysql_router_host_port>'
],
'write' => [
'host' => '<mysql_router_host_ip>',
'port' => '<mysql_router_host_port>'
],
'database' => '<mysql_database>',
'username' => '<mysql_database_user>',
'password' => '<mysql_database_password>',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], "mysql");

Cannot connect to Google Cloud SQL through Google App Engine with Pdo_Mysql in ZF2

I have been trying to solve this issue already for a few days without any results. When using default php pdo object I can connect to the database:
$db = new \PDO('mysql:unix_socket=/cloudsql/project-id:database-instance;dbname=test',
'root', // username
'' // password
);
But when trying to connect with ZF2 adapter the connection just times out.
'db' => array(
'driver' => 'Pdo_Mysql',
'database' => 'test',
'username' => 'root',
'unix_socket' => '/cloudsql/project-id:database-instance',
),
I am quite sure that the problem is somehow with the unix_socket as I can connect to the Cloud SQL server from my localhost directly without socket:
'db' => array(
'driver' => 'Pdo_Mysql',
'host' => 'xxx.xxx.xxx.xxx',
'database' => 'test',
'username' => 'user',
'password' => 'password',
),
What I am missing?
unix_socket isn't a recognized option for the pdo driver (https://github.com/zendframework/zf2/blob/master/library/Zend/Db/Adapter/Driver/Pdo/Connection.php#L164). Try this instead
'db' => array(
'dsn' => 'mysql:unix_socket=/cloudsql/project-id:database-instance;dbname=test',
'username' => 'user',
'password' => 'password',
)

Zend Framework 2: Database config

I'm digging into ZF2, and I've run into some confusion on how to use Zend\Config with Zend\Db to manually set up a connection.
In different places in the manual, there are db configs in different formats.
This one shows a flat array as the config format:
https://packages.zendframework.com/docs/latest/manual/en/modules/zend.db.adapter.html
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'Mysqli',
'database' => 'zend_db_example',
'username' => 'developer',
'password' => 'developer-password'
));
While this one shows a nested format:
https://packages.zendframework.com/docs/latest/manual/en/modules/zend.config.introduction.html
$configArray = array(
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
);
What I expect to happen is that I can call for a new db adapter like so, but this throws exceptions:
$config = new Zend\Config\Config(
array(
'db' => array(
'adapter' => 'Mysqli',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
)
);
$adapter = new Zend\Db\Adapter\Adapter($config->db);
What I end up having to do is:
$config = new Zend\Config\Config(
array(
'db' => array(
'driver' => 'Mysqli',
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'database' => 'mydatabase'
)
)
);
$adapter = new Zend\Db\Adapter\Adapter($config->db->toArray());
Is there a better way of achieving what I'm trying to achieve without having to resort to the service manager?
Ignore the example from the Zend Config introduction page, that's just showing how to make a config object from a PHP array, the structure of the array isn't meant to show anything in particular.
Since you don't want to use the service manager, you need to pass the parameters to the adapter class in the structure it expects. It expects an array, a config object won't work. You've worked out what the structure of the array is, so that's what you need to use.
I think this page in the docs: http://framework.zend.com/manual/2.3/en/tutorials/tutorial.dbadapter.html (the "Basic setup" section) gives a better explanation of the service manager approach, which is how I'd do it in an MVC app at least.

What is the best way to connect multiple database using PDO class?

I am creating one database wrapper class and want to have a feature to connect with multiple database using PDO. I have connected multiple database using pdo as below.
$config = array(
'database1' => array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'users',
'port' => '',
'dbtype' => 'mysql'
),
'database2' => array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'directory',
'port' => '',
'dbtype' => 'mysql'
),
);
foreach ($config as $key => $value)
{
$this->conn[$value['database']] = new PDO("mysql:host=".$value['hostname'].";dbname=".$value['database'], $value['username'],$value['password']);
}
Is it good practice to connect multiple databases as above using any types of database (may using Oracle or Mysql or may be both same). Because my above code takes more time in loading.
I want to have a best practice as Yii or Symphony framework uses to connect db.
Can anybody have solution.
Thanks

Categories