Connecting to MySQL database with SSH key in CakePHP3 - php

I'm a super rookie developer so be nice!
I have web hosting and database through GoDaddy. It's linked to MySQL Workbench but need it to be linked to my application, where I'm using CakePHP3.
Can someone please help me with how to enter the right host names, usernames etc. when there's an SSH key involved? See the image for the database credentials to help me fill in the blanks below:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '???',
'port' => '3306',
'username' => '???',
'password' => '???',
'database' => 'EquineEventFinder',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
DB details image - see for db details.

Related

how to store all facebook emoji in mysql database

I am using Emoji character in my project for facebook post. Some emoji character are saved good but some characters are saved like (??) into mysql database. My database table Default collation is utf8mb4_unicode_ci .
Is there any way to save all emoji into database.
I am working in Cakephp3.
My database structure is :
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => '******',
'password' => '******',
'database' => '******',
'encoding' => 'utf8mb4',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'url' => env('DATABASE_URL', null),
],
I had a similar problem to this when dealing with unusual characters such as this. The issue wasnt with the database, it was with how i was connecting to the database. Since you have provided no code, I can't give any specific advice, but check your DB connection string and make sure its using the mb4 version of utf8. Otherwise the response will not contain the characters.
If you are using PDO, then your db string will look like this.
$db = 'mysql:host=example.com;dbname=testdb;port=1234;charset=utf8mb4';

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'#'%'.

Dynamic database connection in Laravel

I know that in Laravel you can use multiple database connections by specifying them in the config/database.php file, then using DB::connection('my_conn_name'), but is there anyway to use a connection that is not specified in the config/database.php file?
I am writing an archiving application, so the user can specify what connection they would like to use for the process (host, user and password), and I am hoping that I can return the results from show databases for the supplied connection.
After the user has specified the db parameters you could store it in a session to populate a custom connection at config/database.php:
'connections' => [
'mysql' => [
'...'
],
'testing' => [
'...'
],
'custom' => [
'driver' => 'mysql',
'host' => session()->get()->db_host,
'database' => session()->get()->db_database,
'username' => session()->get()->db_username,
'password' => session()->get()->db_password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
]

Avoid hard coded databases credentials in combination with shell - Cakephp 3

I have written a Cakephp 3 shell application which uses models and behaviors to generate thumbnails. Since I populated the database credentials through the $_SERVER super global I need to manually insert the credentials. However, when I try the following snippet of code this results in an error saying that I can't overwrite the connection:
public main($host, $username, $password, $db){
ConnectionManager::config('default', [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => $host,
'username' => $username,
'password' => $secret,
'database' => $db,
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
]);
........
}
Moreover, it first tries to load my default settings in config/app.php which aren't populated and throws notices and errors:
if (isset($_SERVER['RDS_HOSTNAME']) && isset($_SERVER['RDS_USERNAME']) && isset($_SERVER['RDS_PASSWORD']) && isset($_SERVER['RDS_DB_NAME']) ) {
define('RDS_HOSTNAME', $_SERVER['RDS_HOSTNAME']);
define('RDS_USERNAME', $_SERVER['RDS_USERNAME']);
define('RDS_PASSWORD', $_SERVER['RDS_PASSWORD']);
define('RDS_DB_NAME', $_SERVER['RDS_DB_NAME']);
}
return [
.......
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => RDS_HOSTNAME,
/**
* CakePHP will use the default DB port based on the driver selected
* MySQL on MAMP uses port 8889, MAMP users will want to uncomment
* the following line and set the port accordingly
*/
//'port' => 'non_standard_port_number',
'username' => RDS_USERNAME,
'password' => RDS_PASSWORD,
'database' => RDS_DB_NAME,
'encoding' => 'utf8',
'timezone' => 'UTC',
Is there any way to avoid hard coding of the database credentials in this way?

Connect to Redshift Database from Laravel 5 using Pgsql Driver?

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

Categories