Kohana - Database_Exception [ 2 ]: mysql_connect() 2 - mysql

I am trying to setup my database config as demonstrated here: http://kowsercse.com/2011/09/04/kohana-tutorial-beginners/
I have cut and pasted the database.php with my mysql credentials. I then get an error which I fixed by renaming the file to Database.php. the next issue is that my credentials don't change no matter what I put in.
The error shows:
Database_Exception [ 2 ]: mysql_connect() [function.mysql-connect]: Access denied for user 'ivanh'#'localhost' (using password: NO)
MODPATH/database/classes/Kohana/Database/MySQL.php [ 67 ]
62 catch (Exception $e)
63 {
64 // No connection exists
65 $this->_connection = NULL;
66
67 throw new Database_Exception(':error',
68 array(':error' => $e->getMessage()),
69 $e->getCode());
70 }
71
72 // \xFF is a better delimiter, but the PHP driver uses underscore
So no matter what I insert, username, host and password values do not change.
My code in Database.php is then copied from: http://kohanaframework.org/3.3/guide/database/config
firstly with my own database values then after as is. With no luck.
Other info: I'm creating this on a shared unix host.
Kohana 3.1.4: Database_Exception [ 2 ]: mysql_connect(): Access denied seems like a similar issue without a resolution.
Any help would be great, thanks.

Try These Steps:
Step 1:
Go to bootstrap.php
path- kohana\application\bootstrap.php
Un-comment the below line for database access
'database' => MODPATH.'database', // Database access
Step 2:
Go to Your config folder,
create the database.php file if not created, with below codes by giving your MySql credential
Path: kohana\application\config\database.php
Notes: First create a database in your Mysql
If the problem is not solved check your MySql settings too..there may be a error
<?php defined('SYSPATH') OR die('No direct access allowed.');
return array
(
'default' =>
array
(
'type' => 'MySQL',
'connection' => array(
/**
* The following options are available for MySQL:
*
* string hostname server hostname, or socket
* string database database name
* string username database username
* string password database password
* boolean persistent use persistent connections?
* array variables system variables as "key => value" pairs
*
* Ports and sockets may be appended to the hostname.
*/
'hostname' => 'localhost',
'database' => 'CHANGE YOUR DATABASE NAME HERE',
'username' => 'CHANGE YOUR USER NAME HERE',
'password' => 'CHANGE YOUR PASSWORD HERE',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
),
'remote' => array(
'type' => 'MySQL',
'connection' => array(
/**
* The following options are available for PDO:
*
* string dsn Data Source Name
* string username database username
* string password database password
* boolean persistent use persistent connections?
*/
'dsn' => 'mysql:host=localhost;dbname=CHANGE YOUR DATABASE NAME HERE',
'username' => 'CHANGE YOUR USER NAME HERE',
'password' => 'CHANGE YOUR PASSWORD HERE',
'persistent' => FALSE,
),
/**
* The following extra options are available for PDO:
*
* string identifier set the escaping identifier
*/
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
),
);

I am going to make the following assumptions:
You are consequently writing "Database.php" because the file in APPPATH/config containing the code you copied does indeed start with a capital letter.
You haven't changed the config reader.
Database::instance() will request a config array from the Kohana::$config if none is supplied. Config_File::load() will then loop through all found /config/database.php's found in APPPATH, enabled modules and SYSPATH.
Unix filesystems are case sensitive, notice the lowercase 'd' Kohana is looking for versus your uppercase 'D'.
If you did name it 'database.php' and not 'Database.php', click on 'Environment' and then 'Included files' on the error page and make sure it is included. If it's not, do some backtracking and try to find out when things stop behaving as they should, followed by why.
Edit: I don't know how I missed the "renamed it to Database.php" part, but that did not fix anything.

Related

SQL/Laravel is not quoting text fields when building a query

UPDATED
I am new to Laravel and tried to test this demo https://github.com/laravel-json-api/laravel on my local Mac after installing ddev, Docker and MAMP. The problem is that the code (that I didn't write, I just downloaded without making any changes) is executing a SELECT trying to find a user with that email address but, apparently, Laravel builds the SQL sentence removing the quotes when adding the content for the field "email".
This is the error message that I get on the browser:
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `users` where `email` = frankie#example.com limit 1)
I use MySQL 5.7.32 and the DB collation is utf8_general_ci.
The error show in the browser when the exception is caught is:
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php
* #param array $bindings
* #param \Closure $callback
* #return mixed
*
* #throws \Illuminate\Database\QueryException
*/
protected function runQueryCallback($query, $bindings, Closure $callback)
{
// To execute the statement, we'll simply call the callback, which will actually
// run the SQL against the PDO connection. Then we can calculate the time it
// took to execute and log the query SQL, bindings and time in our memory.
try {
$result = $callback($query, $bindings);
}
// If an exception occurs when attempting to run a query, we'll format the error
// message to include the bindings with SQL, which will make this exception a
// lot more helpful to the developer instead of just the database's errors.
catch (Exception $e) {
throw new QueryException(
$query, $this->prepareBindings($bindings), $e
);
}
return $result;
}
/**
* Log a query in the connection's query log.
*
* #param string $query
* #param array $bindings
* #param float|null $time
* #return void
*/
public function logQuery($query, $bindings, $time = null)
{
$this->event(new QueryExecuted($query, $bindings, $time, $this));
if ($this->loggingQueries) {
*Arguments
"SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `users` where `email` = frankie#example.com limit 1)"*
This is the database.php config for the demo project:
database.php demo:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '8889'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
],
MySQL listens on port 8889
Anyone has experienced a similar situation?
Thanks
Your problem is not the quotes, there's something wrong with the SQL server internally.
Regarding the quotes:
When using the standard eloquent methods, query parameters will be sent to the server using prepared statements. This is very save and the no need four adding quotes. Internally the server handles the parameters correctly.
Whenever an error occurred, the server outputs the pure, unquoted data of received from the prepared statement. This indeed is a bit misleading, but it's not an error.
I closed this question as it is not related to how Laravel builds the SQL sentence. It is about an error trying to connect to MySQL from any PHP code on MAMP. See my new question here Error: 200 Connection refused on PHP connecting to MySQL running on MAMP
Such an error may occur when accidentaly we are passing in fields that don't match our Model's fields

Prestashop: 'Link to database cannot be established'

This is our first time moving a Prestashop Installation from one Domain to another and it has been one hell of a learning curve.
Steps we have taken so far:
Copied the database:
0) New database name: gamingco_ps, user gamingco_ps1
Changed ps_configuration
Changed the ps_shop_url (Domain/Domain SSL)
Amended htcacess to stop any redirects
CPANEL removed all temporary redirects
Changed the /home/gamingco/public_html/app/config/parameters.php :
<?php return array (
'parameters' =>
array (
'database_host' => 'localhost',
'database_port' => '',
'database_name' => 'gamingco_ps',
'database_user' => 'gamingco_ps1',
'database_password' => '{Password}',
'database_prefix' => 'pswy_',
'database_engine' => 'InnoDB',
'mailer_transport' => 'smtp',
'mailer_host' => '127.0.0.1',
'mailer_user' => NULL,
'mailer_password' => NULL,
'secret' => 'dva3atuoqjpqhb8xmjtfwjszkpbwaifombdlbg46qwygbi7e4mpyfgui',
'ps_caching' => 'CacheMemcache',
'ps_cache_enable' => false,
'ps_creation_date' => '2021-01-27',
'locale' => 'en-US',
'use_debug_toolbar' => true,
'cookie_key' => 'zml9yqgynwgixtb1e4c0xh8q8gjeynsp7arnovqw5dhpcthcyfpu89bx',
'cookie_iv' => 'qsembkbmpfbnk5bf3hhxjg3sw1laaa06',
'new_cookie_key' => 'def00000acce4699cd116debb37dc5533dfd6ff201153a61ac3446801d772207548a0f44596a7a01939e9f753e4290f3735373a25516e62b2118322ec7f7cd64e3b347ee',
),
);
We are now receiving error 500 without debug mode and with debugmode we are met with:
Link to database cannot be established: SQLSTATE[28000] [1045] Access denied for user 'gamingco_ps1'#'localhost' (using password: YES)
at line 136 in file classes/db/DbPDO.php
131. public function connect()
132. {
133. try {
134. $this->link = $this->getPDO($this->server, $this->user, $this->password, $this->database, 5);
135. } catch (PDOException $e) {
136. throw new PrestaShopException('Link to database cannot be established: ' . $e->getMessage());
137. }
138.
139. $this->link->exec('SET SESSION sql_mode = \'\'');
140.
141. return $this->link;```
DbPDOCore->connect - [line 330 - classes/db/Db.php]
DbCore->__construct - [line 241 - classes/db/Db.php] - [4 Arguments]
DbCore::getInstance - [line 47 - config/alias.php]
pSQL - [line 1336 - classes/shop/Shop.php] - [1 Arguments]
ShopCore::findShopByHost - [line 337 - classes/shop/Shop.php] - [1 Arguments]
ShopCore::initialize - [line 118 - config/config.inc.php]
require - [line 27 - index.php] - [1 Arguments]
I gone through all the steps again and I cannot seem to identify where the error maybe.
Error is pretty straighforward here, Prestashop cannot connect to database.
Things to check:
Make sure your var/cache/prod directory does not contain any cache file from previous installation as it can lead to old DB connection with old credentials.
Double check DB data in parameters.php, carefully look for data correctness (case sensitive, correct DB host / port etc.)
If none of above helps, try connecting your box from ssh and check direct db connection from there with :
mysql -u{username} -p{password} {database name}
and post the result.

CDbConnection failed to open the DB connection Yii after moving to aws

I am new into yii. I was transferring a premade yii website to my aws server. After adding the updated database info into protected/config/main.php I am getting this error. None of the references worked. Please help.
Site Url : http://multilingualbabies.com
Possible Issue:
1) PDO driver maybe its not enabled
2) your dsn connection not truth, make sure your configuration somthing like this:
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbName',
'username' => 'root',
'password' => 'yourPAssword',
'charset' => 'utf8',
];
Note: make sure mysql: is set in dsn.
3) make sure your mysql port is 3306, if other one try to change dsn by adding port like this 'dsn' => 'mysql:host=localhost;port=portNumber;dbname=dbName',.
Good Luck

CakePHP returns ?? when querying MySQL for Chinese Characters

We started adding Chinese language support to our application. Our DB Charset is set to UTF8 and I can successfully query from MySQL CLI and see the word in Chinese characters
However, when I try to use CakePHP query method "findAll", the value is returned as ?? instead of the Chinese characters
Our DB config in database.php looks like this:
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => true,
'host' => 'localhost',
'login' => 'root',
'password' => '******',
'database' => '******',
'prefix' => '******',
'encoding' => 'utf8'
);
Also we have this line set in core.php:
Configure::write('App.encoding', 'UTF-8');
We don't have any views set for this, we are using REST APIs so we are mainly just doing an "echo" in the Controller method for whatever response we get from the DB.
I also tried to create a test file and write this code in it:
<?php echo '基本' ?>
And it worked fine.
Any suggestions on how to get this to work?
Turned out that I needed to install php-mbstring which wasn't installed
Once I installed it and restarted apache, it worked like a charm

Yii2 unit testing :: Access denied for user 'something'#'localhost' (using password: NO)

When creating automated testing in unit testing. I get this error.
My code is in unit test
function testSavingUser()
{
$user = new User();
$user->auth_key='value';
$user->password_hash='value';
$user->password_reset_token='value';
$user->email='value';
$user->status=1;
$user->first_name='value';
$user->last_name='value';
$user->image='value';
$user->role_id=1;
$user->created_date='2016-08-31 16:57:10';
$user->updated_date='2016-08-31 16:57:10';
$user->save();
$this->assertEquals('Miles Davis', $user->getFullName());
$this->tester->seeInDatabase('users', ['name' => 'Miles', 'surname' => 'Davis']);
}
This my config file
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/web.php'),
//require(__DIR__ . '/main-local.php'),
[
'id' => 'app-tests',
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=' . getenv('DB_HOST'). ';dbname=' . getenv('DB_NAME'),
'username' => getenv('DB_USER'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
],
]
]
);
return $config;
In functional.suite.yml
class_name: FunctionalTester
modules:
enabled:
- Yii2:
configFile: 'config/test.php'
When trying to execute my test with codecept run it shows error like this
[yii\db\Exception] SQLSTATE[28000] [1045] Access denied for user 'something'#'localhost' (using password: NO)
Possible reasons of error:
Provided credentials are incorrect or mixed with each other. Check them accurately for typos, etc.
Environment variables are not accessed or extracted correctly with getenv calls. Check if the keys are right and make sure that correct values returned (you can use something like var_dump).
User has no privileges to access this database. You can see how to set it for example in this related question.