Thursday, March 16, 2017

Laravel database gives a PDO exception, can' t connect to the DB. How can i fix this.

I'm new to Laravel, and I'm getting the following exception. To enlighten, I'm using Xampp to test my Laravel project. The database is tunneled to my localhost on port 4444.

PDOException in Connector.php line 68:
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.

The details are

in Connector.php line 68
at PDO->__construct('mysql:host=127.0.0.1;port=3306;dbname=blog', 'root', '', array(0, 2, 0, false, false)) in Connector.php line 68
at Connector->createPdoConnection('mysql:host=127.0.0.1;port=3306;dbname=blog', 'root', '', array(0, 2, 0, false, false)) in Connector.php line 43
at Connector->createConnection('mysql:host=127.0.0.1;port=3306;dbname=blog', array('driver' => 'mysql', 'host' => '127.0.0.1', 'port' => '3306', 'database' => 'blog', 'username' => 'root', 'password' => '', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, 'name' => 'mysql'), array(0, 2, 0, false, false)) in MySqlConnector.php line 24
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => '127.0.0.1', 'port' => '3306', 'database' => 'blog', 'username' => 'root', 'password' => '', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, 'name' => 'mysql')) in ConnectionFactory.php line 183

This is the configuration I'm using for my .env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=4444
DB_DATABASE=DBnam
DB_USERNAME=Username
DB_PASSWORD=MyDBKey

This is my database.php (configuration file)

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '4444'),
            'database' => env('DB_DATABASE', 'DBname'),
            'username' => env('DB_USERNAME', 'Username'),
            'password' => env('DB_PASSWORD', 'MyDBKey'),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

If you read the error, it tries to connect to port 3306 with dbname blog and username root and -p. But that's not in my .env, so how can I let Laravel connect to my local MySQL DB ? FYI I already messed around with XAMPP mysql .ini and .config files, but that didn't work.



via Qun

Advertisement