可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Using Mac OS X and Homestead 2.2.1 with Laravel 5.2.
In terminal (within homestead in my project folder) I can do php artisan to see all the available commands. When I try to run php artisan migrate I get a connection error:
SQLSTATE[HY000] [2002] Connection refused
I have setup a Laravel project with these .env settings
DB_HOST=127.0.0.1
DB_DATABASE=tcv
DB_USERNAME=homestead
DB_PASSWORD=secret
I have also tried localhost for DB_HOST and root for DB_USERNAME and DB_PASSWORD. And all possible variations of these put together!
In Sequel Pro (db management application) I CAN connect with these settings
Host 127.0.0.1
Username homestead
Password secret
Database tcv
Port 33060
But this database is obviously empty, because I cant migrate to it from terminal ...
As far as I can make out it is a configuration issue, since I can connect to it with Sequel Pro. But I have honestly no freaking idea what is setup wrong.
Thanks for the help !!
EDIT
For some reason I get the same SQLSTATE[HY000] [2002] Connection refused
error when moving my project to MAMP and running php artisan migrate.
Now I am completely lost ...
回答1:
I just ran into this and found that changing this in the .env file from 127.0.0.1 to localhost fixed it.
DB_HOST=localhost
回答2:
Problem
In Laravel you have config/database.php
where all the setup for the connection is located. You also have a .env
file in the root directory in your project (which everyone uses for timesaving). This contains variables that you can use for the entire project.
On a standard L5 project the MySql section of config/database.php
looks like this:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
Notice there is no port set!
Although in my .env
file I had set DB_PORT=33060
. But that value (3306)
was never read into the config/database.php
.
So don't be a dumbass like myself and forget to check the database.php
file.
FIX
Simply add
'port' => env('DB_PORT', 3306),
to your config/database.php and set that value in .env like this
DB_PORT=33060
回答3:
If you are using MAMP on a mac OS add the following line to your mysql database config file
'unix_socket' => env('DB_SOCKET', ''),
and on your .env file add
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
回答4:
I was having this problem. When connecting with Sequel Pro, I need to use 33060 as the port, but in the .env file it has to be 3306. I had 33060 in the .env file. Changed it to 3306 and it worked.
回答5:
Another solution for anyone else who has a problem. I had all the settings correct but for some reason my changes weren't updated. Laravel actually caches the config file (which I find completely stupid).
Here was my solution after updating the configs:
php artisan config:clear
回答6:
I had the same problem, try this works
DB_HOST=localhost
回答7:
This is a simple fix. Your mysql database has lost its connection to the server. If you are running a local server run this in your terminal:
mysqld
That will reconnect your database. Then (if you are using homebrew) run:
brew services start mysql
This will automatically connect your database on login.
回答8:
Use localhost instead of 127.0.0.1 (in your .env file), then run command "php artisan config:cache"
回答9:
In my case this error appeared out of blue. While staring at that mysterious error I've realized, that I was trying to run the command outside of vm...
回答10:
It's possible that your 'mysql' hasn't started or is not to the port '3306'
回答11:
If you are using Homestead
then you should be running it with the default mysql port. So instead of using DB_PORT=33060
, you should be using DB_PORT=3306
in your .env file. Also, remember to run your php artisan migrate
commands in your homestead installation and everything should be ok.
Hope that helps.
回答12:
The only thing that solved it for me was to put the connection details in config/database.php instead of the .env file. Hope this helps
回答13:
After you put all configuration on .env file, if you already run php artisan serve, RESTART IT.
回答14:
Me, Im using vagrant, yet im executing php artisan outside the box so basically it doesn't have permission
回答15:
I had a similar problem and no suggestions placed here helped me.
This what has fixed my problem was to set the application name and database hostname with the same value. In my case, 127.0.0.1 works correctly.
APP_URL=127.0.0.1
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=zzz
DB_USERNAME=yyy
DB_PASSWORD=XXX
回答16:
I am having the same problem but I have to change between the localhost and 127.0.0.1.
When viewing the website in a browser and I need to access the database I use.
localhost
But when I need to do any migrations or seedings in the terminal I need to use.
127.0.0.1
I am currently using a vagrant, virtual box and homestead.
It would be nice not to have to keep flicking between the two.