Phalcon, Docker MySQL connection fails - php

So I have 3 docker containers, one with PHP+PhalconPHP, one with MySQL and another with the webserver. Now on my host system I can connect to the MySQL in the docker container and I can also connect with PDO using the following code
new PDO('mysql:host=mysql;port=1024;dbname=database', 'root', 'password');
But for some reason the phalcon framework is unable to connect to the MySQL database using the config.php file like so
return new \Phalcon\Config([
'database' => [
'adapter' => 'mysql',
'port' => 1024,
'host' => 'mysql',
'username' => 'root',
'password' => 'password',
'dbname' => 'database',
'charset' => 'utf8',
],
]);
In this case, for some reason I am getting a Can't connect to MySQL server on 'mysql' (111 "Connection refused) even though I am 100% sure I can actually connect (as proven before with the PDO connection). I also tried hardcoding the actual MySQL container ip address but no luck either.
Any ideas?

So it appeared to be kind of a stupid "mistake" but here's the solution.
Apparently when you use the Phalcon Developer Tools to scaffold your project, it does not initialize the database with the port configuration as it outright ignores it. You can easily fix this by going into the app/config/services.php file and changing the db service to also include the port configuration.
$connection = new $class([
'host' => $config->database->host,
'port' => $config->database->port,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname,
'charset' => $config->database->charset
]);

Related

Connection Drupal website with SQL Google cloud database

I can`t find too much documentation about connecting Drupal7 website with SQL Google database. Does anybody have experience with it? I trying to split Drupal website on VM instance and mysql database on SQL Google storage. After that, change settings.php in Drupal:
$databases['default']['default'] = array(
'database' => 'DATABASE',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'host' => 'Public_IP_of_Google_SQL',
'driver' => 'mysql',
'port' => 3306 (I try 1433 also),
'prefix' => '',
);
}
On SQL instance I have enabled firewall for tcp ports:3306,1433
However, I always get error
PDOException: SQLSTATE[HY000] [2002] Connection refused in lock_may_be_available() (line 167 of /home/public_html/includes/lock.inc).
Can anybody help? What can I do on Drupal side to connect website with external database?

Laravel 5.3 wont connect to mysql

I created a new user on my Macbook Air (OS X Version 10.11.2). The new user has full administrator access. I'm using MAMP. The MySQL port is set to 3306. I can connect to MySQL with everything else just fine. All of my Laravel projects stopped connecting to MySQL. When i try to use
php artisan migrate
I get:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Here is the database portion of my .env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=store
DB_USERNAME=clerk
DB_PASSWORD=inventory
I have tried using the root user and password. I have tried using different ports. I have tried switching the host to localhost. I have verified that the username and password are correct, and that the user has access to the database.
Here is the MySQL portion of my config/database.php file:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'store'),
'username' => env('DB_USERNAME', 'clerk'),
'password' => env('DB_PASSWORD', 'inventory'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
I have been searching for a few days, and i can't seem to find the answer. My old Laravel 5.2 projects won't connect either. I'm pretty sure that this was caused by creating a new user, but i don't know enough about MySQL to figure out the problem.
Having followed troubleshooting processes as I've suggested in comments below your question, all should be working as expected.
Should that not be the case, you must be missing something; ... a PATH issue I presume.
I suggest you export your database, uninstall your MySQL, then download a new one at here.
Follow the approach elaborated here for a possible fix.
With this, your challenge will surely be addressed.
Try changing your db_host = localhost:3306
You define an engine = null. The engine is used inside the MySqlGrammar.php specifically when compiling a create query as far as I know. I have only seen null engines for VIEWS.
Try the following config:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'store'),
'username' => env('DB_USERNAME', 'clerk'),
'password' => env('DB_PASSWORD', 'inventory'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
You did mention it in your question and take it that the following works from a terminal
mysql -uclerk -pinventory -h127.0.0.1 -P3306 store
Could be that it needs to be
mysql -uclerk -pinventory -hlocalhost -P3306 store
Something else you can check with just an info.php file in your project to double check that everything seems fine with the reference to PDO as a module.
<?php
phpinfo();
Then also play around with just a little PDO php script to see if your connection is working properly. Something like:
<?php
$servername = "127.0.0.1";
$username = "clerk";
$password = "inventory";
try {
$conn = new PDO("mysql:host=$servername;port=3306;dbname=store;", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Something else you can also check inside a dummy route what env('DB_DATABASE') etc gives you.
Updated things to check 2016.09.15
To make sure that env() is fetching the correct information include the route below and then include the output of the following:
Route::get('dbtest', function () {
$config = [
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
];
dd($config);
});
Then also access mysql as the root user and include the output of the select statement:
mysql> select host, user, password, authentication_string, password_expired from mysql.user;
As you are using MAMP make sure to use the mysql commandline tool from /Applications/MAMP/Library/bin/mysql
It's been solved. After a few hours of pulling my hair out, i decided to take #nyedidikeke advice and re-install mysql. I was using MAMP, and I have been wanting to stop using it for a while. I uninstalled it, and installed mysql through Homebrew. Everything is working just fine, thanks for all your help!

Yii2 db; dsn with ip local

i have my localhost configured with and other port, i'm trying to use this in the 'yii\db\Connection'
'dsn' => 'mysql:host=192.168.1.5;dbname=db_empresa_juridico',
'username' => 'db_user_jurid',
'password' => '[removed]',
'charset' => 'utf8',
but Yii2 still loading, don't show nothing how i can connecting to the db? using local ip?
i need to shared my db at lan
Missing the port=3306 in
'dsn' => 'mysql:host=192.168.1.5;dbname=db_empresa_juridico;port=3306',

Using Google Cloud SQL with dev_appserver.py and Laravel (PHP)

I am having issues trying to work with a Cloud SQL instance and Laravel. I was able to do local dev work on a Cloud SQL instance with Python but I can't seem to get it with PHP.
Here is the error I get:
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/cloudsql/project:instance-db'
Here is my app/config/database.php:
<?php
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'unix_socket' => '/cloudsql/my-project:instance-id',
'host' => '',
'database' => 'my_database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'migrations' => 'migrations',
);
It all works when deployed just not locally.
You cannot connect to CloudSQL from your local machine right now. For testing you should use a local MySQL instance the CloudSQL instance when running in production.
You should be able to assign an IP address to your Cloud SQL instance and allow your own network to access the instance in the management console.
Assigning an IP address will cost extra.

cakephp hosting "Cake is NOT able to connect to the database."

beginner level stuff-
-i recently hosted my cakephp project on 000webhost.com
-it shows the cake homepage
-but at the same time shows
Cake is NOT able to connect to the database.
Datasource class a2952772 could not be found.
-i have tried editing root/app/config/database.php number of times.
-i have created mysql database on 000webhost.com
i guess there is some problem with
$datasource in
public $default = array(
'datasource' => '????',
'persistent' => false,
'host' => 'mysql11.000webhost.com',
'login' => '*****',
'password' => '*****',
'database' => 'db1',
'prefix' => '',
//'encoding' => 'utf8',
);
if anybody has hosted and configured databases on 000webhost.com ,plz help...
You need to specify a valid datasource, such as 'Database/Mysql'. Also check the CakePHP database configuration section.

Categories