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

dib258's avatar
Level 11

Connecting to Mysql on a Vagrant Virtualization

Hi,

I know there a looot of topics concerning this problem but I can't figure a way out of this one.

I installed vagrant with this video from laracasts: https://laracasts.com/lessons/get-off-mamp-now. But this forums everybody talks about homestead. My configuration files doesn't looks like a homestead release or else but it's working for most of it.

I'm following all the basics tutorial series's videos and was using sqlite for tests because it was simpler but know that a need to use mysql, I can't make it work.

Mysql is installed on the vagrant linux virtualisation. I know the login password. I even installed phpmyadmin.

My environment variables are :

DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=kic
DB_USERNAME=root
DB_PASSWORD=secret

I changed the DB_HOST value and port many times. And I tried to connect via telnet following this post : https://laracasts.com/discuss/channels/general-discussion/homestead-mysql-connection-error-sqlstatehy000-2002/replies/28730

Here's an error returned :

dibs-Air:kic dib258$ telnet 192.168.33.21 3306
Trying 192.168.33.21...
telnet: connect to address 192.168.33.21: Connection refused
telnet: Unable to connect to remote host

So, right know I'm just trying to run php artisan migrate to install my tables in the Mysql database and returned errors change regarding the host I put.

SQLSTATE[HY000] [2002] Connection refused 

or

SQLSTATE[HY000] [2002] No such file or directory 

I'm executing the command outside the linux virutalization because I prefer but when I ssh into and try php artisan migrate in my folder I get another kind of error:

vagrant@precise64:/var/www/kic$ sudo chmod -R 777 ./
vagrant@precise64:/var/www/kic$ php artisan migrate
PHP Fatal error:  Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/var/www/kic/storage/logs/laravel-2015-05-20.log" could not be opened: failed to open stream: Permission denied' in /var/www/kic/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:84
Stack trace:
#0 /var/www/kic/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(88): Monolog\Handler\StreamHandler->write(Array)
#1 /var/www/kic/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\RotatingFileHandler->write(Array)
#2 /var/www/kic/vendor/monolog/monolog/src/Monolog/Logger.php(265): Monolog\Handler\AbstractProcessingHandler->handle(Array)
#3 /var/www/kic/vendor/monolog/monolog/src/Monolog/Logger.php(543): Monolog\Logger->addRecord(400, 'exception 'Unex...', Array)
#4 /var/www/kic/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(47): Monolog\Logger->error('exception 'Unex...')
#5 /var/www/kic/app/Exceptions/Handler.php(27): Illuminate\Foundation\ in /var/www/kic/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 84
PHP Fatal error:  Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/var/www/kic/storage/logs/laravel-2015-05-20.log" could not be opened: failed to open stream: Permission denied' in /var/www/kic/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:84
Stack trace:
#0 /var/www/kic/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(88): Monolog\Handler\StreamHandler->write(Array)
#1 /var/www/kic/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\RotatingFileHandler->write(Array)
#2 /var/www/kic/vendor/monolog/monolog/src/Monolog/Logger.php(265): Monolog\Handler\AbstractProcessingHandler->handle(Array)
#3 /var/www/kic/vendor/monolog/monolog/src/Monolog/Logger.php(543): Monolog\Logger->addRecord(400, 'exception 'Symf...', Array)
#4 /var/www/kic/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(47): Monolog\Logger->error('exception 'Symf...')
#5 /var/www/kic/app/Exceptions/Handler.php(27): Illuminate\Foundation\ in /var/www/kic/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 84

The chmod doesn't do anything so I'm kind of lost. I'm trying to resolve this issues since a week with no success at all...

Hope I've been enough clear with my problem.

Thanks in advance.

0 likes
13 replies
fideloper's avatar

Lots of stuff here:

MySQL

Assuming MySQL is installed on the same server (virtual machine) as your laravel application, use localhost for connecting to MySQL. MySQL only listens on the localhost network by default, which may explain why telnetting into 192.168.33.21 gives you a refused connection.

If you are trying to connect to MySQL from OUTSIDE of the virtual machine, then you have more work to do (that's not clear in your post). The MySQL series on Servers for Hackers might help clear up how that works.

Laravel Config

The error SQLSTATE[HY000] [2002] No such file or directory makes me think that you're still using Sqlite by accident. Make sure the default connection configured in your database.php is set to mysql instead of sqlite.

Permissions

Try using 0777 instead of just 777 with the chmod command. Try setting those permissions specifically on the storage directly instead of the whole project. Especially don't commit file permission changes in git if you're running that kind of command on every file in your code base!

Alternatively, if those are files being shared with your host computer, try setting the 0777 permissions of your storage directory from your host computer rather than from within the VM.

Overall, the Vagrant Series on serversforhackers might clear up how permissions are working within Vagrant.

1 like
bashy's avatar

@fideloper The 2002 error for no such file or directory is for MySQL but just means it can't find the sock file.

@dib258 What is this command meant to be doing? I would be very careful doing ./ and / and ~/

vagrant@precise64:/var/www/kic$ sudo chmod -R 777 ./

I would do a 775 and change the group to your web server user (nginx/www-data etc)

As for allowing remote connections to your MySQL server, just comment out bind-address in /etc/mysql/my.cnf and restart it with sudo service mysql restart or similar. You can then use the IP to connect.

1 like
dib258's avatar
Level 11

Hi,

Thanks for you complete answer ! :)

Yes I'm trying to connect outside of the virtual machine and for the telnet test I only showed one error but I've tried every different like said in the post mentioned. So localhost, 127.0.0.1 and the ip of the virtual machine. With the port 3066 and 30660.

The videos you linked in your post looks like interesting but am I obliged to pay 30 dollars just to watch 4 of them : /

For the laravel config, no I'm sure the default one is set to MySQL.

'default' => 'mysql',

For the permissions, normally, I don't chmod my entire folder but when I try to execute php artisan migrate INSIDE the virtual machine I got the last error in my previous post. Who refers to a problem of permission if I'm correct.

@bashy Regarding the chmod command I made. It's to try to let everyone avec full acces to my project because when I try to php artisan migrate in my folder INSIDE my virtual machine, I get a permission error. I know the difference between ./ , ~/ and / .So in this case it was just a test but the errors still pops when I try to run my command.

I also commented the bind-address but I still got the error from outside. What host should I put then in my database config file ? localhost, the virutal machine ip or 127.0.0.1 ?

bashy's avatar

In your config or environment vars, use your server IP 192.168.33.21 (or whatever your LAN IP for your VM is).

dib258's avatar
Level 11

Ok, I just modified my .env file like this :

DB_HOST=192.168.33.21
DB_PORT=3306
DB_DATABASE=kic
DB_USERNAME=root
DB_PASSWORD=secret

And I get this error :

 SQLSTATE[HY000] [1130] Host '192.168.33.1' is not allowed to connect to this MySQL server

It's searching 33.1 et not 33.21 ? oO I just copy pasted my .env file what's this error !

sitesense's avatar

Is there a particular reason why you chose not to use Homestead?

It might be better to abandon this custom install and just go with Homestead. At least when you have any other problems, it will be easier to get help because you're "singing from the same hymn sheet" so to speak.

dib258's avatar
Level 11

Hum. I'm pretty new to Laravel. I was using MAMP and I followed this video (https://laracasts.com/lessons/get-off-mamp-now) thinking it was the good way to do. That's why I'm kind of confused right now :o

Edit : I scratched a little about homestead. and I don't know exactly what I installed before then. I think the video I watched was a little old. I'm gonna try to install homestead instead :)

dib258's avatar
Level 11

Thanks all, Everything is working alright now !

@sitesense Yep, I followed the doc. You're right it's the best and up to date.

I'm already on my way to the next fight with Eloquent and migrations !

bashy's avatar

The answer to this thread was to read the docs? :P

dib258's avatar
Level 11

No, But I tried to like the post. Guess what it wasn't the right button xD

And there's no undo button :D

bashy's avatar

You can change it to another answer. If one posted isn't the solution, reply yourself and mark that as the answer with how you did it.

Please or to participate in this conversation.