Connect to Redshift Database from Laravel 5 using Pgsql Driver? - php

I was wondering if anyone knew whether the above was likely to be achievable or if I'm doing something nonsensical. These connection details work to an RDS (i.e. blah.blah.eu-west-1.rds.amazonaws.com) database:
'db1' => [
'driver' => 'pgsql',
'host' => env('DB_HOST_BRAIN'),
'database' => env('DB_DATABASE_BRAIN'),
'username' => env('DB_USERNAME_BRAIN'),
'password' => env('DB_PASSWORD_BRAIN'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'xyz_admin',
],
but these do not work to a Redshift (i.e. blah.blah.eu-west-1.redshift.amazonaws.com) database:
'db2' => [
'driver' => 'pgsql',
'host' => env('DB_HOST_PINKY'),
'database' => env('DB_DATABASE_PINKY'),
'username' => env('DB_USERNAME_PINKY'),
'password' => env('DB_PASSWORD_PINKY'),
'port' => env('DB_PORT_PINKY'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'xyz',
],
Assuming all the details are correct, is there a compelling reason why this is never going to work? Is there any way I can make it work?

As long as the environment variables are correct, your config works for connecting to Redshift:
$take_over_the_world = DB::connection('db2')->select('SELECT tonight FROM going_to_do');

Related

SQL Server returning strings for all values even with attributes set

I am working on migrating a MySQL database over to SQL Server (MSSQL) and am having an issue where all values returned from the database are coming back as strings.
I have been through the documentation and everything I can find on Google and haven't been able to resolve this so far.
My setup is using the latest PHP PDO driver with Laravel 6.18.35.
My database.php looks like this:
'migration' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_MIGRATION_USERNAME'),
'password' => env('DB_MIGRATION_PASSWORD'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'modes' => [
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION'
],
'engine' => null,
'options' => [
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true,
],
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'options' => [
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true,
],
],
The migration runs fine but as soon as my application requires an integer it gets a string. I also have issue with binary UUIDs but I believe that is an encoding issue so will deal with that separately. I have tried changing the charset, collation etc. I have tried removing the options and setting them on the PDO object with:
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES,false);
$pdo->setAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE,true);
None of these changes seem to make any difference and I still get strings back. If I do a get on the attributes:
echo $pdo->getAttribute(PDO::ATTR_STRINGIFY_FETCHES),
echo $pdo->getAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE)
I don't see a response for the ATTR_STRINGIFY_FETCHES but can see SQLSRV_ATTR_FETCHES_NUMERIC_TYPE is set true.
I would really appreciate any suggestions for other things to try/check etc! Thanks in advance!

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");

Laravel 5.2 Database [xxx] not configured, while being defined in database.php

So I have 3 mysql DB's defined in my database.php file. I had 2 for the longest time and everything worked fine. I have one titled mysql, one mysql1 and one mysql2. The first 2 work fine, I copied the second one, changed the name to mysql2 and added the proper parameters but it still always says "Database mysql1 not configured. Is there some sort of cache? or somewhere else I have to define it? I am calling the DB through a model like this.
protected $connection = 'mysql1';
This is my database.php for clarification
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql1' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
This was fixed by using the command
php artisan config:cache
I submitted a Laravel issue request to fix it since that absolutely should not be required to change the DB connections variable.
If you can not delete cache with php artisan config:cache because it throws error after you run it:
[InvalidArgumentException]
Database [] not configured
And if you are sure that your connections parameters are good then try to manually delete bootstrap cache config files which are placed in app/bootstrap/cache folder.

Laravel 4.2 Session Database Driver Read / Write Host

I configured the way I separate the database to read and write.
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
But now, I want to change the database driver in the session and I want to be able to read and write operations to a single host of the session value.
How can I only run on a single host in the session process without disturbing the above structure?
I think I found a solution.
We are adding a new connection into the first database.php connections.
'session' => array(
'host' => "HOST_NAME",
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
then we give the name of the connection a little bit before we install the connection value in session.php file.
'connection' => "session",
Thats it.

How to make read from slave and write at master in mongodb with replicaset using laravel

I am working with laravel 5.1 with mongodb. For mongodb I am using jenssegers mongo configuration.
Now I have to make mongodb replica set and I want to make read operation only from slave & write operation at master.
In laravel's documentation I read that we can make separation in read & write like below:
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
Here is link for how to set configuration in laravel.
In jenssegers documentation here is configuration for replication:
'mongodb' => [
'driver' => 'mongodb',
'host' => ['server1', 'server2'],
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => ['replicaSet' => 'replicaSetName']
],
So if I change this to:
'mongodb' => [
'driver' => 'mongodb',
'read' => ['host' => '192.168.1.1'],
'write' => ['host' => '192.168.1.2'],
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => ['replicaSet' => 'replicaSetName']
],
So can I use the above configuration in jenssegers mongo db configuration?

Categories