So I have been trying to get an connection my database with homestead, but everytime I make a DB select statement I get an SQLSTATE[HY000] [2002] Connection refused. It seems like my problem is not the same as the others with the same title. I am using homestead with virtual box
Here is my select statement:
<?php
echo DB::select('select * from bruger where id = 1')
?>;
This is simply to check if I can get any info out.
I have tried various things in both my .env file and and database.php
This is what they are looking like currently:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '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,
],
and
APP_ENV=local
APP_DEBUG=true
APP_KEY=randomKey
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33060
DB_DATABASE=phpDatabase
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=secret
REDIS_PORT=33060
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
For DB_HOST and DB_PORT try using the IP and port as set in the Homestead.yaml file.
Default:
DB_HOST=192.168.10.10
DB_PORT=3306
The port should be 3306, since you are accessing from within the homestead machine, if I understood correctly.
In fact, check the documentation
You will use the default 3306 and 5432 ports in your Laravel database configuration file since Laravel is running within the virtual machine.
Related
I have been trying to set up PHP Laravel 5.7.0 by installing and configuring Homestead and Vagrant with the VirtualBox plugin on Windows 8.1. I have been trying to make "php artisan migrate" work for a long time now but I keep getting this error:
PHP Laravel Doctrine\DBAL\PDOException SQLSTATE[HY000] [2002] No
connection could be made because the target machine actively refused
it
Or another error message like "Connection attempt refused"
Can anyone help me figure out what I have been doing wrong in my configuration?
~/Homestead/Homestead.yaml
---
authorize: ~/.ssh/homestead_rsa.pub
cpus: 1
databases:
- homestead
folders:
-
map: "C:\\reggie\\code\\salvo"
to: /home/vagrant/Code
ip: "192.168.10.10"
keys:
- ~/.ssh/homestead_rsa
memory: 2048
provider: virtualbox
sites:
-
map: homestead.test
to: /home/vagrant/Code/Laravel/public
variables:
-
key: APP_ENV
value: local
/c/reggie/code/salvo/.env
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:9REWE8zTJepml2KPA0icpkJoond+Gln0mMKwYs7/lrQ=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
/c/reggie/code/salvo/config/database.php
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
...
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'homestead' == gethostname() ?
'localhost' : '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
],
Please let me know if I missed any pertinent detail. Thank you!
Sorry I made a mishap in the configuration. I didn't set the .env file config DB_PORT to 33060 as mentioned in the https://laravel.com/docs/5.7/homestead.
So I have a site on Heroku, which loads fine as long I don't go to a page that requires a database connection. What happens when I do land on one of those pages, is I get this error:
PDOException in Connector.php line 55: SQLSTATE[HY000] [2002] Connection refused
I'm using db4free.net to try to access a MySQL database. This is my database.php file. The ****** masking the actual values.
'default' => env('DB_CONNECTION', 'mysql-production'),
'mysql-production' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'db4free.net'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'en*******'),
'username' => env('DB_USERNAME', '*******'),
'password' => env('DB_PASSWORD', '*******'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
This my env file:
APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:tUWa6AteuAOfEAh/IKx3c13tJEzSmS4La8QdKlEgByQ=
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=enviro-2
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
The env file is ignored by git.
Prior to trying db4free.net I tried following the steps in this article
https://mattstauffer.com/blog/laravel-on-heroku-using-a-postgresql-database/ but I still have the exact same problem. I also set the APP_KEY in the Heroku config variables to match the one in my env file.
Anyone have any ideas on why I get this error?
your connection details in .env file in incorrect.
you should write your credentials details in .env file as well as database.php file
I'm using Laravel 5.2 and phpMyAdmin (which I'm running through InstantWordpress) and when I try to migrate the database I get this error message from the command line:
[PDOException]
SQLSTATE[HY000] [2002] no connection could be made because the target machine actively refused it
This is what my .env file looks like:
APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:IBh/yuGAXTC2a2rRD7A9VE5io7AbPXLYfJUfLyFnRfA=
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=micedb
DB_USERNAME=roo
DB_PASSWORD=roo
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
Then, when I look at the app in the browser I have a landing page as though the initial migrate has worked but there's nothing in the database and when I try to register I get an error message and stack trace.
In Config->database
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'DBName'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
In .env file :
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=DBName
DB_USERNAME=root
DB_PASSWORD=
Close the laravel application server and restart again for clearing the cache.
php artisan serve
Try this. Good luck!
I've gotten several different errors running php artisan migrate while trying different things. The most recent one is:
Access denied for user 'homestead'#'localhost' (using password: YES
This is my database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
],
and this is the .env
APP_ENV=local
APP_DEBUG=true
APP_KEY=halMyGPhfVZGrdpibFIwEE0wZipP5lqu
DB_HOST=localhost *updated
DB_DATABASE=forge *updated
DB_USERNAME=forge *updated
DB_PASSWORD= *updated
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Try changing the following in your .env file or remove all the keys related to DB from .env file.
DB_HOST=localhost
DB_DATABASE=forge
DB_USERNAME=forge
DB_PASSWORD=
I had run into same issue, The problem is, the database.php file looks for the key DB_DATABASE etc. in the .env file. Since, that key is present in .env file, It doesn't consider the default value forge that you have provided in the statement env('DB_DATABASE', 'forge')
In plain english, "Look for the key DB_DATABASE in .env, if present, use that else use forge as default"
In your case, the keys are present in .env file hence, though you provide forge, it considers homestead that is defined in the .env file. And hence the error Access denied on 'homestead'#'localhost'
Hope I clarified the things.
I am new to laravel and while trying to connect to database in localhost using xammp iam getting an error that
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
my connection code is that
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'hindutemples'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
but 'hindutemples' database exists i dnt know why it is trying to connect with homestead
Any help appreciated.
rename .env.example to .env. Then set appropriate values in .env files. It should work.
UPDATE
Laravel 5 stores settings in .env file. Laravel 5 comes with default .env.example file in a root folder. If you can not find it in project root it may have been hidden.
You need to rename .env.example or create new .env file inside project root and put app settings in it like given below,
APP_ENV=local
APP_DEBUG=true
APP_KEY=0SewSBZ47SfA4vRV6o8BeW74PMFwPpoX
DB_HOST=localhost
DB_DATABASE=hindutemples
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
env() returns values from .env files. if it could not find setting in .env file it returns false or if supplied default e.g. env('DB_USERNAME', 'homestead') it will return default value. in your case it can not find .env setting so it is returning homestead as a username and hence error.
Read More