Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

r123's avatar
Level 3

Problem with database connection (ports)

If I have port in database.php like this - 'port' => env('DB_PORT', '3306'), and port in .env like this - DB_PORT=3306 . Then I can execute something like this without errors:

$user = DB::table('users')->where('id', 1)->first();
echo $user->email;

But then I can't connect to my database when I migrate tables. Homestead database connection in MySQL Workbench has port 33060. I get error:

SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)

If I change port in .env to - DB_PORT=33060 then I can connect to my database and migrate tables, BUT when I run this:

$user = DB::table('users')->where('id', 1)->first();
echo $user->email;

I get error on, for example, /home page.

PDOException in Connector.php line 55:
SQLSTATE[HY000] [2002] Connection refused
1. in Connector.php line 55
2. at PDO->__construct('mysql:host=127.0.0.1;port=33060;dbname=homestead', 'homestead', 'secret', array('0', '2', '0', false, false)) in Connector.php line 55
3. at Connector->createConnection('mysql:host=127.0.0.1;port=33060;dbname=homestead', array('driver' => 'mysql', 'host' => '127.0.0.1', 'port' => '33060', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, 'name' => 'mysql'), array('0', '2', '0', false, false)) in MySqlConnector.php line 24
4. at MySqlConnector->connect(array('driver' => 'mysql', 'host' => '127.0.0.1', 'port' => '33060', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, 'name' => 'mysql')) in ConnectionFactory.php line 61
...
0 likes
1 reply
bashy's avatar
bashy
Best Answer
Level 65

Try using the Homestead IP 192.168.10.10, don't need port for that. 3306 is the default and is fine without it.

Use SSH connection when connecting remotely (local machine).

2 likes

Please or to participate in this conversation.