cakephp version 4 fresh install database not connecting - mysql

'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => '000000',
'database' => 'cakephp',
'encoding' => 'utf8mb4',
'timezone' => 'UTC',
'cacheMetadata' => true,
my database in phpmyadmin in xampp is ok.
But stil the cakephp homepage says
CakePHP is NOT able to connect to the database.
Connection to database could not be established: SQLSTATE[HY000] [1045] Access denied for user 'my_app'#'localhost' (using password: YES)

I recently faced this issue and solved it by adding database configuration in config/app_local.php
As per Cakephp documentation:
The application skeleton features a config/app.php file which should
contain configuration that doesn’t vary across the various
environments your application is deployed in.
The config/app_local.php
file should contain the configuration data that varies between
environments and should be managed by configuration management, or
your deployment tooling
Reference:
CAKEPHP -> Configuration -> Configuring your Application

Related

How to fix connection with database in CakePHP with MAMP

I want to connect to CakePHP3.77 with MySQL 5.7.25 in MAMP.
I cannot connect with database named cake_youtube_db.
Error Message is this.
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 did try create other users and put user and password.
However they did not work.
/config/app.php
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
//'port' => 'non_standard_port_number',
'username' => 'root',
'password' => 'root',
'database' => 'cake_youtube_db',
SQL plugin is mysql_native_password.
I expect Database will be connected with my cakeapp.

lavaral 5 ERROR{ (SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)}

'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database.sqlite'),
'prefix' => '',
],
**JUST CHANGED THE CODE FOR LOCALHOST AND CHANNGED THE NAME TO ROOT AND SET THE PASSWORD OF PHPMYADMIN . **
**WHEN WRITTING THE COMMAND IN XAMPP SHELL ((Php artisan migrate:install)) **
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'password'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'password'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
it is showing error SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)
By default laravel assumes that you will want to have different configurations for different environments. E.g. in a testing environment, you might wish to have a different username and password and in a production environment different. Since laravel has so many configuration files, it quickly becomes a nightmare to manage all those. Hence laravel makes use of PHP's environment variables.
See the docs here.
What is basically says is that if you wish to use the "environment" variables, which laravel uses by default, you have to place all your configurations in the env() method as already mentioned.
If you do not wish to do this, e.g. for simple projects, simply remove the env from your code, like this.
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laravel',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Note that you can mix and match. i.e you can have some of the variables in env and some stand-alone.
So why use env at all?
Lets say your application has 100 testers all placed in different locations. In laravel you have to code approximately 8-10 configuration files. Also you need to version-control those files. So you have two problems at hand:
You do not wish to send all 100 users the same credentials. Also they might use different database, cache server, etc which means that they will have different configurations. So every user has to maintain those 8-10 configuration files by hand.
You do not wish to send these configuration files to version control. Because if you do, whole world will know your API secrets and possibly will take advantage of that (just like password). Also if you look at laravel conf files, you will notice that there are other information such as timezone, debug property, etc that are also in conf files, and you do want to version-control them. So how do you version-control such configuration files and still hide your sensitive information.
The answer is env variables. Laravel uses dotenv whose documentation can be found here. Basically these are variables that live in one file called .env in a key-value pair. E.g.
Sample contents of .env file
APP_DEBUG=false
APP_KEY=ABCDEFGH
...
Once you define your .env file as this, you can get the value using the key as such env('APP_DEBUG').
So this solves the above mentioned problem in following ways:
you keep the .env file to yourself. And you also declare another file called .env.example which is an exact replica of original file except the fact that it contains sample values, not your sensitive values. Then you pass this new example file to everyone. They will replace the sample data with their own sensitive information.
Since you are version-controlling the example file, you can version control all your conf files because they don't contain the secret. The secret is in .env files. All those conf files contain is values like these env('APP_KEY') and the actual value is replaced at run time using your .env file.
Make sure you have to set up right server credentials into .env file on your Laravel project:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myapp_db
DB_USERNAME=root
DB_PASSWORD=pass
Try to clean up artisan cache and restart the artisan,
php artisan config:clear
restart php artisan

Resolving database connectivity issues on Forge

When I try to login to the app I have setup on Forge, my Laravel app spits out this error:
SQLSTATE[HY000] [1045] Access denied for user 'appname'#'localhost' (using password: YES)
I have the environment variables configured correctly and they work fine on my copy of the site on my local Homestead vagrant box.
Here is the .env.php file that Forge wrote:
return [
'APP_ENV' => 'production',
'DB_DRIVER' => 'mysql',
'DB_HOST' => 'localhost',
'DB_NAME' => 'appname',
'DB_USER' => 'appname',
'DB_PASS' => 'LoNgPaSsWoRd',
];
Here is the relevant contents of app/config/database.php which works locally:
'default' => getenv('DB_DRIVER'),
'connections' => array(
'mysql' => array(
'driver' => getenv('DB_DRIVER'),
'host' => getenv('DB_HOST'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USER'),
'password' => getenv('DB_PASS'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
I tried dumping the environment values before the first database connection is accessed using dd(getenv('DB_PASS'), getenv('DB_USER')) and they show up correct.
I also tried connecting through MySQL Workbench and resetting the password to exactly what is shown in the environment variable Forge wrote.
None of my other subdomains are having this issue.
What could be going on here and how could I debug this?

DB connection Yii Framework [duplicate]

This question already has answers here:
Database connection error in Yii
(2 answers)
Closed 9 years ago.
I've got a new domain & hosting but I can't connect to the new db.
What I tried:
'db'=>array(
'connectionString' => 'mysql:host=ascodcurro.com.mysql;dbname=ascodcurro_com',
'emulatePrepare' => true,
'username' => 'ascodcurro_com',
'password' => 'xxxx',
'charset' => 'utf8',
),
If I try localhost as always it works fine.
User info from host:
MySQL
server: ascodcurro.com.mysql
DB: ascodcurro_com
user: ascodcurro_com
pw: *****
PhpMyAdmin
PhpMyAdmin: https://dbadmin.one.com/
user: ascodcurro_com
pw: ******
During the initial startup and working with Yii, the first problem that I faced was connecting to the database.
I was getting a really weird error and I was not able to connect to my DB on my Mac OSX Mountain Lion on my XAMPP stack. After countless searches, I finally figured out a method that worked. This is the only way I am able to connect to my DB for Yii.
First check if you can connect to the database normally using the
default configuration by uncommenting the 'db' configuration in the
main.php file. Example :
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=ascodcurro_com',
'emulatePrepare' => true,
'username' => 'username',
'password' => 'pass',
'charset' => 'utf8',
'tablePrefix'=>'',
'enableProfiling'=>true,
'enableParamLogging'=>true,
),
However, if you are still not able to connect to the database even after this, as I wasn't able to connect to mine, try taking this approach :
Make a new php file and type :
<?php phpinfo() ?>
check that page out, find out where 'mysql' is located and find out the 'MYSQL_SOCKET' in it and note the location.
After noting the location down, try this as your 'db' connection string(for example, for me the location is "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock") :
'connectionString' => 'mysql:unix_socket=/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock;dbname=practice',
And the rest as the basic parameters that you use.
And then try connecting again.
Hopefully, that should connect you to your DB.
Regards,
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=basename',
'emulatePrepare' => true,
'username' => 'username',
'password' => 'pass',
'charset' => 'utf8',
'tablePrefix'=>'',
'enableProfiling'=>true,
'enableParamLogging'=>true,
),
its my config. You get any errors?

cakephp can't connect to database when on server

I have my Cake app and it's is working on my PC on Apache but it connects to the remote database.
And everything is fine.
But when I copied my app on server, there is an error when I try to execute any controller:
Error: SQLSTATE[HY000] [2005] Unknown MySQL server host 'xx.xx.xx.xx:33306' (2) requires a database connection
Error: Confirm you have created the file : app/Config/database.php.
And I tested the connection in simple php script (on server) and I connect and get data from this database without problem. So why Cake can't connect to it?? What might be problem?
public $external = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'xx.xx.xx.xx:33306',
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx',
);
The file 'database.php' exists in app/config and has permissions rwxr-xr-x
I am not sure about mod_rewrite... how I can check it?
'host' => 'xx.xx.xx.xx:33306',
is not allowed. You should use the optional port option as specified in the CakePHP docs.
public $external = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'xx.xx.xx.xx',
'port' => '33306',
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx',
);