CakePHP on AWS Bitnami LAMP server can't connect to database - php

I am just getting into PHP and I'm trying to get a server started and go through CakePHP's CMS tutorial. However I cannot get a database connection. I get the error: CakePHP is NOT able to connect to the database.
Connection to database could not be established: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
I have been trying to edit my app/config/app.php file. This is what I have:
'Datasources' => [
'default' => [
'className' => Connection::class,
'driver' => Mysql::class,
'persistent' => false,
'host' => 'localhost',
'port' => '3306',
'username' => 'root',
'password' => 'THE DEFAULT PASSWORD I USED TO GET INTO phpMyAdmin',
'database' => 'cake_cms',
'unix_socket' => '/opt/bitnami/mysql/tmp/mysql.sock',
//'encoding' => 'utf8mb4',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url' => env('DATABASE_URL', null),
],
'test' => [
'className' => Connection::class,
'driver' => Mysql::class,
'persistent' => false,
'host' => 'localhost',
'port' => '3306',
'username' => 'root',
'password' => 'THE DEFAULT PASSWORD I USED TO GET INTO phpMyAdmin',
'database' => 'cake_cms',
'unix_socket' => '/opt/bitnami/mysql/tmp/mysql.sock',
//'encoding' => 'utf8mb4',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url' => env('DATABASE_TEST_URL', null),
],
],
Thanks in advance. I'm super new, so maybe I need to provide more information.

Related

Exception:SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it

I am using vagrant and my database default configuration are as below(Cakephp-3):
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'vom',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
... ... ...
My virtual-box ip = '192.168.33.10' I have changed db-host with my vagrant ip but seeing the same error when run the
command: cake migrations migrate

How does DataSource test in cakephp 2

We have custom DataSource and want to test that verifies the data after find method execute equals to one not customized.As I thought it, it would be possible if we can change Datasource in test code.I try many case, but I couldn't make it work.
public $default = [
'datasource' => 'CustomPostgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'login' => 'hoge',
'password' => 'hogehoge',
'database' => 'prod',
'schema' => 'public',
'prefix' => '',
'encoding' => 'utf8'
];
public $test = [
'datasource' => 'CustomPostgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'login' => 'hoge',
'password' => 'hogehoge',
'database' => 'test_prod',
'schema' => 'public',
'prefix' => '',
'encoding' => 'utf8'
];
public $old = [
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'login' => 'hoge',
'password' => 'hogehoge',
'database' => 'dev',
'schema' => 'public',
'prefix' => '',
'encoding' => 'utf8'
];
public $test_old = [
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'login' => 'hoge',
'password' => 'hogehoge',
'database' => 'test_dev',
'schema' => 'public',
'prefix' => '',
'encoding' => 'utf8'
];
//try1
$this->Model->setDataSource('test_old');
//try2
ConnectionManage::create('test_old',///);
//try3
////in fixture File and ClassRegistry:Init("Model")
$useDbConfig = "old";
////inner start::up
ClassRegistry:Init("Model");
//try4 extends model
ModelForOldSetting extends Model{
$useDbConfig = "old";
}
Above codes didn't work and it always emit error says
MissingConnectionException: Database connection "Postgres" is missing, or could not be created ,when running test however it works fine when setting default datasource Database/Postgres running local browser without test.
So, I'm really confused why Database connection "Postgres" is missing.Any idea makes me appreciate to them.
I finally found irritating and stupid irritating code by outer database.php file overrides datasources' setting.Following code works fine after appropriate settings.
ModelForOldSetting extends Model{
$useDbConfig = "dev";
$useTables = "models";
}
ClassRegistry:Init("ModelForOldSetting");
$model = $this->getMockForModel('ModelForOldSetting');

Dynamic datasources and global in Cakephp 3?

I have an application that will be used by many databases. To choose the database with the application data I have a database with the connections parameters (host, username, pass, database...)
The problem is that this dynamically created datasource does not work for the rest of the application.
Controller Code
$data = $this->request->data;
$connection = ConnectionManager::get("default");
$query = "SELECT * FROM clients WHERE client_id = ".$data['codigo'];
$result = $connection->execute($query)->fetchAll('assoc');
ConnectionManager::drop('default');
$config = ConnectionManager::config('connection', [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => $result[0]['host'],
'username' => $result[0]['username'],
'password' => $result[0]['password'],
'database' => $result[0]['database'],
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
]);
$connection = ConnectionManager::get('connection');
ConnectionManager::alias('connection', 'default');
return $this->redirect(['controller' => 'Mains', 'action' => 'index']);
After this redirect, apparently the other controllers do not have this datasource.
It's certainly a bit unusual to be swapping databases based on contents inside request data - but generally database connection just needs to be done earlier on in Cake's initial setup, as each Controller's connection resource is already set to the default.
If you're only swapping the one time, you could manage this inside config/bootstrap.php (where Cake normally sets up it's ConnectionManager) instead.
You won't have access to $this->request, but could just grab it from $_REQUEST instead, for example:
In config/bootstrap.php, change these lines:
Cache::config(Configure::consume('Cache'));
ConnectionManager::config(Configure::consume('Datasources'));
Email::configTransport(Configure::consume('EmailTransport'));
To this:
Cache::config(Configure::consume('Cache'));
ConnectionManager::config(Configure::consume('Datasources'));
$connection = ConnectionManager::get("default");
$client = TableRegistry::get('Clients')->find()
->where(['client_id'=> $_REQUEST['client_id']])->firstOrFail();
ConnectionManager::drop('default');
$config = ConnectionManager::config('connection', [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => $client->host,
'username' => $client->username,
'password' => $client->password,
'database' => $client->database,
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
]);
ConnectionManager::get('connection');
Email::configTransport(Configure::consume('EmailTransport'));
Edited: Switched from your raw SQL to a Table::find()

Cakephp is trying to connect to my machine instead of the database I've setup

I'm having a hard time while trying to run a local project in my computer. It's built on cakephp 3.2.x, I run it with cakephp's dev server (bin/cake server), but the project crashes when it tries to paginate something for the user landing page:
Error: SQLSTATE[HY000] [1045] Access denied for user 'scqadev'#'192.168.1.63' (using password: YES)
Here comes the main headache: 192.168.1.63 is the IP of my computer, but my datasources aren't pointing to my machine. I want it to connect to a database in 192.168.1.43. Here's my config/app.php file:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '192.168.1.43',
'username' => 'scqadev',
'password' => 'scqadev',
'database' => 'scqadev',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
],
'csd' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '192.168.1.43',
//'port' => 'nonstandard_port_number',
'username' => 'csd',
'password' => 'csd',
'database' => 'csd',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
],
],
The problem happens in this line, inside a controller:
$this->set('analises', $this->paginate($this->Analises));
And my 'Analise' entity points to the default datasource (there is no custom datasource config in src/Model/Entity/Analise.php).
So where is this configuration coming from? Why is cakephp trying to log in my computer's database instead of 192.168.1.43's ?
It's not trying to connect to your computer. The application is trying to connect to the correct datasource as 'scqadev'#'192.168.1.63'. You should make sure that such user is defined in your database. Or (probably better) a user called 'scqadev'#'%'.

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