Google App Engine Cloud SQL connection issue - google-app-engine

When trying to connect to Google Cloud SQL I get following error:
Connection error
Unable to find the socket transport "unix" - did you forget to enable it when you configured PHP?
I have been trying to solve this issue now for multiple hours, still nothing.
My config is as follows (in ZF2 config local.php):
return array(
'db' => array(
'driver' => 'Mysqli',
'host' => '',
'database' => 'ar_captab',
'username' => 'root',
'password' => '',
'driver_options' => [
'unix_socket' => ':/cloudsql/some-app-v1-test:some-db'
]
),
);
I also added my application ID of my Google App Engine application to Access Control list (Authorized App Engine Applications) in Cloud SQL.
When I try to run this:
$config = $this->getServiceLocator()->get('config');
$adapter = new \Zend\Db\Adapter\Adapter($config['db']);
$sql = new \Zend\Db\Sql\Sql($adapter);
$select = $sql->select();
$select->from('example');
$select->where('1');
$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
I get the unix socket error. What I am missing?

Finally figured it out. I was because of the colon. You do not need it with Mysqli connection, only with PDO connection types.
So the correct config is:
return array(
'db' => array(
'driver' => 'Mysqli',
'host' => '',
'database' => 'some-database',
'username' => 'root',
'password' => '',
'driver_options' => [
'unix_socket' => '/cloudsql/some-app-v1-test:some-db'
]
),
);

Related

How to access remote database from laravel 4.2

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

Connecting SQL Server to laravel as a second database

I have laravel 5.8 setup and working with MySQL. I need to connect a second database (SQL Server).
I have the following in my .env file
DB_EXT_CONNECTION=sqlsrv
DB_EXT_HOST=0.0.0.0
DB_EXT_PORT=1433
DB_EXT_DATABASE=database
DB_EXT_USERNAME=user
DB_EXT_PASSWORD=password
database.php
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env(''),
'host' => env('DB_HOST', '0.0.0.0'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'database'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
I am running xampp and have the following dll's installed
extension=php_pdo_sqlsrv_73_ts.dll
extension=php_sqlsrv_73_ts.dll
I keep getting
SQLSTATE[HY000]: [Microsoft][ODBC Driver 11 for SQL Server]Protocol error in TDS stream (SQL:select * from [dbo].[t_people]) (View: C:\xampp\htdocs
I have
$users = DB::connection('sqlsrv')->table('dbo.t_people')->select('*')->get();
in my blade template.
Change all the DB_DATABASE from your config file to DB_EXT_DATABASE, as you wrote in your .env file.
Try instead of
$users = DB::connection('sqlsrv')->table('dbo.t_people')->select('*')->get();
do the
$users = DB::connection('sqlsrv')->table('t_people')->select('*')->get();
And don't do it in your blade files, do it in controller.

How to add SSL to the backend mysql database and connect using Laravel

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'
),
),

CakePHP 3 Unknown email configuration "gmail" in cPanel server

Email are sent successfully in local. But the problem occurs in production server that shows an error: unknown gmail configuration.
My email configuration looks like:
'gmail' => [
'className' => 'Smtp',
// The following keys are used in SMTP transports
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 60,
'username' => 'noreplayxxxxx#gmail.com',
'password' => 'xxxxxxxxxxxxxxxxxxxxx',
'client' => null,
'tls' => true,
]
What is the problem and how can I fix this problem?
Make sure app.php file on your production server contains required configuration. app.php is ignored by git by default, so it might not get deployed in your case as you are expecting.
You can use these in your controller
///////////////////// Configuration /////////////////////////
$host = 'ssl://smtp.gmail.com';
$username = 'abc#gmail.com';
$password = 'xxxxxxx';
$port = '465';
$email_to = 'xyz#gmail.com';
$senderName = 'abc';
$email_id_reply ='abc#gmail.com';
$new_attachments = '<some image>';
$msgsend ='Hello!!';
Email::configTransport('WebMail'.$recipient,
[
'className' => 'Smtp',
'host' => $host,
'port' => $port,
'timeout' => 30,
'username' => $username,
'password' => $password,
'client' => null,
'tls' => null
]
);
////////// SEND MAIL
$transport = ['transport' => 'WebMail'.$recipient];
$email = new Email($transport);
$email ->template('default','default')
->emailFormat('both')
->from([$username => $senderName])
->to($email_to)
->replyTo($email_id_reply)
->subject('Client Message');
$email->attachments($new_attachments);
$response = $email->send($msgsend);

Variable database setting Cakephp

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

Resources