PDO connection refused with laravel, but not with $con = new PDO() - php

I've configured mysql for my laravel 5.0-dev project like so:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST') ?: 'localhost',
'database' => env('DB_DATABASE') ?: 'homestead',
'username' => env('DB_USERNAME') ?: 'homestead',
'password' => env('DB_PASSWORD') ?: 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
However, I get connection refused ('PDOException' with message 'SQLSTATE[HY000] [2002]') whenever I try to interact with the database using either php artisan, or the laravel app itself.
The weird thing is, when I run the code below, the connection doesn't get refused.
<?php
$dsn = 'mysql:dbname=homestead;host=localhost';
$user = 'homestead';
$password = 'secret';
try
{
$dbh = new PDO($dsn, $user, $password);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
I've been googling for 3 hours now and I haven't found a single solution which actually works. So any help is greatly appreciated.

The problem is fixed. First off, if you're using a VM, like I am, you can't set your host to localhost, it has to be set to 127.0.0.1. Secondly, the port through which the connection is made needs to be explicitly defined (default for mysql is 33060). So in my case the connection configuration is defined as follows:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '33060',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Lastly, you need to modify the .env file in your project:
DB_HOST=127.0.0.1
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

This is very tricky stuff of PDO.
If you set URI as "localhost", PDO will use a local unix socket! Where as a plain IP, PDO will use a TCP connection.

Related

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server [duplicate]

This question already has an answer here:
How to connect a Laravel app to MySQL using MAMP?
(1 answer)
Closed 8 years ago.
I am receiving the following error on my localhost for Laravel 4.1 (using MAMP)
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (61)
It points to:
/Applications/MAMP/htdocs/crowdsets/laravel-master/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php
This is the function it is pointing to:
public function createConnection($dsn, array $config, array $options)
{
$username = array_get($config, 'username');
$password = array_get($config, 'password');
return new PDO($dsn, $username, $password, $options);
}
Up to this point, I had not received this error.
I have a local environment and production environment set up (the default).
in config/local/database.php I have:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
An error like that means the server itself is not even reachable. Did you start MySQL in MAMP?
Also, how have you started MAMP? With the standard MySQL 3306 port? Or the alternative port MAMP uses for non-admins: 8889?
I bet your server is running, but is attempting to connect to 3306 so you need to set the port to 8889. Change your config to be like this; note the addition of the port param:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '8889',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
EDIT: I just found this question thread that addresses the issue of connecting Laravel to MAMP via port 8889.

How to connect to remote server in laravel

I am trying to connect to other db server in laravel, i changed the host and port in my database.php but it is not working can any one suggest help.
This is my database.php:
'mysql' => array(
'driver' => 'mysql',
'host' => '192.168.1.6',
'port' => '3306',
'database' => 'ired4',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
I am getting the following error:
PDOException (2002)
SQLSTATE[HY000] [2002] Connection refused

Laravel fails to connect to other database servers

I have been using Laravel for a while but mostly on a single database server.
Now, my client has got atleast 5 database from multiple database servers in the same network.
I am able to confirm the communication in between servers using ping through ssh and also using the
mysqli_connect to check the database connections.
However, when I use laravel through its database.php (mysql configs) like :
// This setup is working fine (using localhost)
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
// Other DB (which is failing to connect) but i can confrim the connection using a file outside
// the laravel app through mysqli
'other_db_connection' => array(
'driver' => 'mysql',
'host' => '192.168.33.241',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'another_db_connection' => array(
'driver' => 'mysql',
'host' => '192.168.33.211',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and using the new PDO
// This connectiion functions with no problem
$conn1 = new PDO('mysql:host=localhost;dbname=dbname;port=3306','username','password');
if($conn1) {
echo 'connected';
}
// But this returns an error
$conn = new PDO('mysql:host=192.168.33.241;dbname=dname;port=3306','username','password');
if($conn) {
echo 'connected';
}
// Also, i have no problems using the DB:: or even the model as long as the host is the localhost
it fails to connect, both the PDO and the laravel default DB feature only works if the host is the localhost.
For some reason it throws back an error like this if i use the other servers ip, which is where i would like to communicate with:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.33.241' (13)
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.33.211' (13)
When i try using the server ip or the main domain name to connect.
Am I missing something? Why is it that I can connect only on the local db server which is localhost
and not to any other database servers? But I can connect to them with no problem if I use say an index.php file from outside the laravel application and use the mysqli_connect to make queries and make connections?
Thanks for all your help.
You can access all the connections you have specified within database.php with:
$user = DB::connection('other_db_connection')
You don't need to setup new PDO connections or similar, will work like above for you

SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (61) error Laravel 4.1 [duplicate]

This question already has an answer here:
How to connect a Laravel app to MySQL using MAMP?
(1 answer)
Closed 8 years ago.
I am receiving the following error on my localhost for Laravel 4.1 (using MAMP)
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (61)
It points to:
/Applications/MAMP/htdocs/crowdsets/laravel-master/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php
This is the function it is pointing to:
public function createConnection($dsn, array $config, array $options)
{
$username = array_get($config, 'username');
$password = array_get($config, 'password');
return new PDO($dsn, $username, $password, $options);
}
Up to this point, I had not received this error.
I have a local environment and production environment set up (the default).
in config/local/database.php I have:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
An error like that means the server itself is not even reachable. Did you start MySQL in MAMP?
Also, how have you started MAMP? With the standard MySQL 3306 port? Or the alternative port MAMP uses for non-admins: 8889?
I bet your server is running, but is attempting to connect to 3306 so you need to set the port to 8889. Change your config to be like this; note the addition of the port param:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '8889',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
EDIT: I just found this question thread that addresses the issue of connecting Laravel to MAMP via port 8889.

Laravel 4 migration:install ErrorException

php artisan migrate:install
{"error":{"type":"ErrorException","message":"PDO::__construct(): [2002] Connection refused (trying to connect via tcp:\/\/127.0.0.1:3306)","file":"\/Applications\/MAMP\/htdocs\/DRCSports\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connectors\/Connector.php","line":47}}
In my database.php I have updated the information to mysql
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'Laravel_DRCSports',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
I am not sure if I am understanding the error right, but to me it looks like my laravel isn't connecting to mysql right. If that is the case I have no clue how to fix it.
The problem was that mysql is running on port 8888, while Laravel's default port value is 3306 (as it's the default port of mysql servers).
The solution is to add 'port' key to the array (For example: 'port' => 8888) and it'll
do the work.
This is what i did... in /app/config/app.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost:8889',
'database' => 'pic',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and at the bottom of the php code
'redis' => array(
'cluster' => false,
'default' => array(
'host' => '127.0.0.1',
'port' => 8888,
'database' => 0,
),
),
it has to work it...
I experienced problems (Laravel 4) when I used MySQL on a port other than 3306.
A browser-run app expects the following app/config/database syntax:
'mysql' => array(
...
'host' => 'localhost',
'port' => '8889',
...
)
While the command-line run artisan expects the following syntax:
'mysql' => array(
...
'host' => 'localhost:8889',
...
)
The issue is described here:
https://github.com/laravel/laravel/issues/1182
Most articles suggest a workaround using Laravel environments, but it results in duplicate config files and violates the DRY principle (Don't Repeat Yourself), so here's another alternative:
At the top of app/config/database.php:
$my_hostname = 'localhost';
$my_port = '8889';
$my_database = 'database';
$my_username = 'username';
$my_password = 'password';
if (App::runningInConsole()) { // artisan runs from the command line
// change 'localhost' to 'localhost:8889'
$my_hostname = $my_hostname.':'.$my_port;
}
And further down:
'mysql' => array(
'driver' => 'mysql',
'host' => $my_hostname,
'port' => $my_port,
'database' => $my_database,
'username' => $my_username,
'password' => $my_password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Cheers
Change your database information in your config/database.php and .env file.
I did these changes and worked like a champ :
in database.php :
host : localhost:8889
Port: 8889
and my mamp has a password so I there was two way to putting that password either in database.php file or in .env file I changed the password '' value to 'forge' and then use my MAMP password in the .env file
by the way, you can see your specific information about MAMP in MAMP application in the port tab (MySQL one)
Make sure to edit this part of ".env" file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=root
This worked for me.
i guess you can solve this problem by add the following code inside app/config/database syntax:
'mysql' => array(
...
'pconnect' => 'TRUE',
...
)

Categories