I am currently do some development in a early developed system developed from laravel 4.2.
I run it in my local machine. At the same time now I have to access to a another System's database.
For testing purposes i have created that db in my local server. I don't have direct access to that db in real situation. Only i have a view. So please tell me how should i configure my system to achieve that task.
First you have to add a separate database connection to your other system's database, so in your app/config/database.php you can have this:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'forge',
'username' => 'forge',
'password' => '123456',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'forge2',
'username' => 'forge2',
'password' => '123456',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
In order to perform query to the other database you have to do this:
$users = DB::connection('mysql2')->select('select * from users where id = ?', array(1));
You may see this code in the Laravel's documentation: https://laravel.com/docs/4.2/database#accessing-connections
Related
i want to use multiple databases, i have 1db/user and 1 main db,already set main db in config file but how to manage user database(db name = {user_id}_db). thanks in advance
this is my config code
'main' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
You can use following configuration for multiple dbs.
<?php
return array(
'default' => 'mysql',
'connections' => array(
# primary
'mysql' => array(
'driver' => 'mysql',
'host' => 'ip1',
'database' => 'db1',
'username' => 'user',
'password' => 'pwd'
'charset' => 'utf8',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
),
# secondary db connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'dbip2',
'database' => 'db2',
'username' => 'user',
'password' => 'pwd'
'charset' => 'utf8',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null
),
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/db.sqlite',
'prefix' => '',
),
),
);
Then you can use the the connection() method:
$connection = ‘mysql2’;
Schema::connection($connection)->create(function(Blueprint $table) {
//
});
Creating DB for each user is useless, even if you are doing it for security or any other purpose whatsoever. Useful SO link MySQL performance: multiple tables vs. index on single table and partitions
Your question (connecting to multiple databases in Laravel) is already answered here. How to use multiple database in Laravel
UPDATE
I guess, you want to create new databases on the fly and connect to them (without using .env configuration). Create dynamic connections instead of specifying 'connection-name' when trying to connect to a new database. Then, using Config::set you can create new temporary connections and then use DB::connection with the temporary name you just created. read more https://lukevers.com/2015/03/25/on-the-fly-database-connections-with-laravel-5
I have a requirement to add SSL to the backend mysql database and connect using Laravel. But unable to know the way to do it.
Do I need to have MySql Db configured to use encrypted connection and then provide some parameters in Database.php file in Laravel.
My site has https enabled.
Please guide what should be taken care and to be done.
What you will need to is first configure SSL/TLS on your MySQL Server and the instructions will be specific to the OS hosting the MySQL Server as well as the MySQL Version itself.
Here's an example guide for MySQL 5.x and Ubuntu:
https://www.digitalocean.com/community/tutorials/how-to-configure-ssl-tls-for-mysql-on-ubuntu-16-04
Once you've done this, you can configure your laravel app to tell it how to communicate over encrypted mysql connection by adding this line to your db config:
'options' => array(
PDO::MYSQL_ATTR_SSL_CA => $cert_base . '/ssl-ca-bundle.pem'
),
the full config will look something like this in config/database.php
'mysql' => array(
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'options' => array(
PDO::MYSQL_ATTR_SSL_CA => '/path/to/ssl-ca-bundle.pem'
),
),
I need for my drupal 8 installation to connect a second database for a view.
I install the module views_database_connector (january 2015 release)
I connect with the setting.
but nothing works, i don't see my database with view.
my settings...
$databases['default']['default'] = array (
'database' => 'first base',
'username' => 'first base',
'password' => 'xxxxx',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
$databases['external']['default'] = array (
'database' => 'second_base',
'username' => 'second_base',
'password' => 'yyyyyy',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
Does this module need a patch, and how to apply ??
For this you can create multiple database connection in setting file :
use \Drupal\Core\Database\Database::setActiveConnection('external'); to get current active connection.
This question already has answers here:
How to use multiple databases in Laravel
(7 answers)
Laravel Change Connection Dynamically
(2 answers)
Closed 5 years ago.
How can a database connection be changed when user is admin role? Admin database contains information that must not be accessible to other user's and requirement also is that database must be located on own server. I do have currently separate configuration for admin connection.
'mysql' => [
'host' => 'admin.psa44.localhost'
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => 'secretpassword',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
I want that no hardcoding is in application so how can Laravel do the switch when necessary? Thanks for any help.
You need to create new database config rule in appconfig.
'mysql' => [
'host' => 'admin.psa44.localhost'
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => 'secretpassword',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
'mysql_admin' => [
'host' => 'admin.psa44.localhost'
'driver' => 'mysql',
'database' => 'admindatabase',
'username' => 'root',
'password' => 'secretpassword1',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
I named mysql_admin. In migration when use
Schema::create('some_table', function($table)
to create table ,use
Schema::connection('mysql_admin')->create('some_table', function($table)
to create that table in other database.
For query to that database do something like this
$admins = DB::connection('mysql_admin')->select(...);
And you can to define connection for model
class AdminModel extends Eloquent {
protected $connection = 'mysql_admin';
}
I'm searching a way to create a variable database in Cakephp.
I'm creating a website in wich the database setting (user, pass, db) depends of the user who logg's in. The database settings come from another database.
Thanks in advance,
Aäron
-- ANSWER
Thanks for your quick reply.
I know how to switch between databases, but the database settings have to be variable.
Example
$db = $this->DB_SET['DB_SET']['db']; //from model DB_SET the database
$db_login= $this->DB_SET['DB_SET']['login'];
$db_host= $this->DB_SET['DB_SET']['host'];
$db_pass= $this->DB_SET['DB_SET']['pass'];
var $db2 = array(
'driver' => 'mysql',
'persistent' => false,
'host' => $db_host,
'login' => $db_login,
'password' => $db_pass,
'database' => $db,
'prefix' => '',
'encoding' => 'UTF8',
'port' => '',
);
If you're looking to create database connections on the fly, you may want to use ConnectionManager::create().
// get logged in user's id and create a connection for them
$id = $this->Auth->user('id');
ConnectionManager::create("user_$id_db", array(
'driver' => 'mysql',
'persistent' => false,
'host' => $db_host,
'login' => $db_login,
'password' => $db_pass,
'database' => $db,
'prefix' => '',
'encoding' => 'UTF8',
'port' => '',
));
Then set your models to use it when you're ready.
// get logged in user's id and use their connection
$id = $this->Auth->user('id');
$this->Model->setDataSource("user_$id_db");