Search code examples
laravelhomesteadlaravel-5.3

SQLSTATE[HY000] [2002] Connection refused only in browser


I am on OS X Yosemite, running the latest homestead with Laravel 5.3.

I am able to run and reset migration in terminal from OS X. I can see the tables when I connect to the database with Sequel Pro using standard connection. I even see the tables when I SSH into the virtual box and run mysql -uhomestead -psecret.

The problem is, when I try to register a new user from the browser, I get the PDOException in Connector.php line 119: SQLSTATE[HY000] [2002] Connection refused error. It works when I run php artisan serve and fill in the form at http://localhost:8000/register.

I tried changing DB_HOST to 192.168.10.10 but that causes migrations to fail.

I might have created some chaos while trying to install mysql. I installed it using homebrew. Then i ran mysql.server start and at another point I ran brew services start mysql because I was not sure if it was working.

Homestead.yml file:

---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/code/laravel
      to: /home/vagrant/laravel
      type: "nfs"

sites:
    - map: votingapp.dev
      to: /home/vagrant/laravel/votingapp/public

databases:
    - homestead

.env file:

    APP_ENV=local
    APP_KEY=base64:xxxxxxxx
    APP_DEBUG=true
    APP_LOG_LEVEL=debug
    APP_URL=http://localhost

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=33060
    DB_DATABASE=votingapp_development
    DB_USERNAME=homestead
    DB_PASSWORD=secret

web.php file:

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index');

Solution

  • It turns out I had the wrong port number. In the .env file it should be 3306 while in Sequel Pro it should be 33060.

    According to the Connecting to databases of the Laravel manual:

    You should only use these non-standard ports when connecting to the databases from your host machine. You will use the default 3306 and 5432 ports in your Laravel database configuration file since Laravel is running within the virtual machine.