Laravel Homestead SQLSTATE[HY000] [2002] Connection refused after server move - php

I moved an application that I'm working on into a new dev server, a Laravel Homestead VagrantBox on a Mac OSX host. Upon doing so, I ran php artisan:migrate to update my database and this went through without a hitch.
I decided to create a new user to continue testing, so I created the route
Route::get('/newuser', function()
{
User::create([
'username' => 'someone',
'email' => 'someone#someone.com',
'password' => Hash::make('password')
]);
return 'Done.';
});
When I visit /newuser, however. I am getting the following message.
SQLSTATE[HY000] [2002] Connection refused
Now, I know that my database config must be correct, as I received no errors when I ran php artisan migrate and my database migrated successfully. However, just to be safe, I checked my database config.
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost:33060',
'database' => 'site',
'username' => 'root',
'password' => 'apassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
I figured that maybe there was an issue with the way I defined the port I was on, so I added the parameter:
'port' => '33060'
Upon doing this, the error message changed from "Connection refused" to "No such file or directory"
I'm at a loss. Does anyone have any pointers?

Now, I know that my database config must be correct, as I received no errors when I ran php artisan migrate
It's a better than even money bet when you're stuck on something that it's one of your assumptions that's the problem. It's possible your Laravel application is reading different credentials during a command line run, or that the migration had nothing to do, or for some weird PHP reason the errors were suppressed during your migration run. I'd check the credentials Laravel's using during the context your errors are cropping up. Add the following code to your newuser route to see what Laravel's reading.
$default = Config::get('database.default');
var_dump($default);
$config = Config::get('database.connections.'.$default);
var_dump($config);

I had same problem and i saw my database server was unresponsive i restarted and it works... sometime it cause when wrong configuration.
I posted this answer if someone will have same issue and mysql unresponsive :)

Related

Unable to use any user in laravel project other than the root

I am a beginner developing an app in Laravel framework. I am facing a problem that only the root user works with my database without the password or the user ''#'host' works. However, when I deploy the app to the server I can not use any of these users
I get the following error, it seems that the Laravel is not reading any user name from .env or database.php file. (as the name of the user is blank here).
SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'itax'
Again it works if I use the user=root and no password or user=''
Here are the configurations of my .env file
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=itax
DB_USERNAME=raees
DB_PASSWORD=12345
and database.php file
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'itax',
'username' => 'raees',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Anyone facing the same issue or found any solutions to these problems please share
Found solution with empty username. The username was always blank.
The issue was with MySQL. The SQLSTATE error message is being passed directly from MySQL.
So then I deleted the database and user. Re-created the database in the Databases tab of PhpMyAdmin. And importantly separately added the user and privileges. This fixed the issue.
I could connect from the MySQL client and run migrations.

CakePHP Console Cron Database connection Error

I have a Webapp which is using the CakePHP 2.x framework. The main front-end works perfectly fine but I have some cron jobs which are run form the Cake Console. When I run these cron jobs I get the following error:
"Error: Database connection “Mysql” is missing, or could not be created"
There are several similar questions on SO (but none of the given solutions works for me. I have the following DB connection setup in the Cake config file and which works for the front-end:
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'xxxxx.com',
'login' => 'xxxxxx',
'password' => 'xxxxxxx',
'database' => 'goalarc-dev',
'prefix' => '',
'unix_socket' => '/var/run/mysqld/mysqld.sock'
);
I added the 'unix_socket' key after reading a similar solution. My server is running on AWS and so I opened:
/etc/mysql/my.cnf
And found the socket line as follows:
socket = /var/run/mysqld/mysqld.sock
However when I browse to 'var/run/mysqld' the folder is empty when I use the command "ls -l". This would suggest to me that somethig is mis-configured. How can I find the true and valid location of mysql.sock?
A.

Connecting to MySQL in brand new Laravel installation with Homestead unclear

I have some questions since I am new to the Laravel/Homestead environment.
Question 1) In the docs here, it says that the homestead box comes with MySQL out of the box. If I go to my project/config/database.php file, I can see that there are some default details already there for mysql:
'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,
],
The thing is that whenever I SSH into my box using homestead ssh and I successfully connect to my box, I run the command mysql and it shows the following:
ERROR 1045 (28000): Access denied for user 'vagrant'#'localhost' (using password: NO)
I don't know why it is not letting me enter the mysql console. I even try mysql -u forge and it shows me the error above as well. I was wondering why am I getting this error and potentially how would I be able to fix it/successfully connect to the mysql console inside my box.
Question 2) I am really interested in installing phpMyAdmin.
I followed the following thread (I followed the second most up-voted answer, since I read somewhere that if you serve a route in laravel, like the procedure of the first answer in the thread, it is kind of temporary and you would have to do it every time you halt/up the box), but it did not work for me. My browser just tries connecting to phpmyadmin # http://phpmyadmin.dev and it just doesn't work (the connection times out after a while). I made sure to add phpmyadmin into my hosts file:
127.0.0.1 phpmyadmin.dev
as well as in my homestead.yaml file:
sites:
-map: phpmyadmin.dev
to: /home/vagrant/Code/phpmyadmin
I am sure that my box is mapping folders correctly since I have other apps that I can successfully connect to in my browser (for example test.app works perfectly)
Question 3) I was wondering if I can use a MySQL database I have installed in my local machine (not in my vagrant/homestead box). If so, what would I need to set up? I would also like to know what would be easier, working with MySQL in the box (which I think it would make more sense, since the whole point of having a vagrant/homestead box is to isolate your dev environment) or in my local machine, or it just comes up to your own choice.
Thank you for all your advice/help in advance!
Cheers!
Question 1) Turns out I just needed to use the credentials homestead as username and secret as password. After that, I could made all the modifications to my MySQL databases.
Question 2) Using that other thread on SO, with the second most up-voted answer, I could use phpMyAdmin. I was not restarting my homestead box, therefore I could not see the changes. After a restart, everything worked flawlessly.
Question 3) I am sticking with composer migrations for this one :)
Thanks to #Jeemusu for helping me out!
Cheers!

Can't connect to database with laravel 5

I'm trying to connect to my mssql database with laravel 5 but it's not working. I've tried to connect with different mssql databases so it's not a firewall issue. Also my credentials are correct. I found on google that you need SQL Server Native Client 11.0 and I've already installed that.
This is my connection array in config/database.php:
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('nvt'),
'database' => env('nvt'),
'username' => env('nvt'),
'password' => env('nvt'),
'charset' => 'utf8',
'prefix' => '',
],
And I've set the default to "sqlsrv" :
'default' => env('sqlsrv','sqlsrv'),
But when I'm trying to create a table I receive the error:
PDOException in Connector.php line 50: could not find driver
What could be wrong???
EDIT:
I've installed pdo for sqlsrv from here:
http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx
And I added it to my php.ini file:
extension=\Installaties\php_pdo_sqlsrv_53_nts.dll
But stil I receive the error?
Try to remove the eve() method, i mean use the following method
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => 'nvt',
'database' => 'nvt',
'username' => 'nvt',
'password' => 'nvt',
'charset' => 'utf8',
'prefix' => '',
],
I ran into a similar issue. Try putting in the server info directly rather than using env. If it works, then you know Laravel is having an issue reading the .env file.
I encountered your problem the last time I tried my connection in sqlsrv. I am using xampp. Try putting yung php_pdo_sqlsrv_53_nts.dll in the ext folder, replace extension=\Installaties\php_pdo_sqlsrv_53_nts.dll with extension=php_pdo_sqlsrv_53_nts.dll and try installing odbc 11 on your computer. It worked for me but not sure why. (not an expert) if it worked good to help a fellow person and if not, sorry for the trouble. Thank you for the time reading.
You may try any of the following solutions
Check that you have entered the right connection parameters. Test by connecting to the DB with a client tool.
Make sure you have the right version of sqlsrv configured for your machine. Check the PHP logs to be sure your web server was able to load the extension correctly.
Set the port to null in your env file
As a tip, I won't advise anyone to set the connection parameters anywhere but in the .env (Setting it in config/database is a security risk).

Laravel Forge detecting environment variables in only 3 of 4 deployed sites

they are all configured identically.
the environment is definitely set to production.
the database credentials do not get read at all - it wants to use ''#localhost password: NO as the default.
i set
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
in all 4 of the sites app/config/database.php, and i set the related environment variables in forge. i have tried leaving the environment field blank, and also typing in 'production'. i also tried using $_ENV['environmentvariable'] in database.php.
this gives me a different error of Undefined Index DB_HOST. so clearly the env vars aren't getting read.
i've taken everything down and re-created the repo and the laravel server many times. all 3 other sites are configured identically. it detects the environment in bootstrap/start with
$env = $app->detectEnvironment(function()
{
return getenv('ENV') ?: 'development';
});
i appreciate any help guys. it works fine on homestead btw.
edit: i can ssh into forge and do whatever i want in any of the sites or databases as well, except in the problem one any php artisan command fails with the 'Access Denied for ''#localhost password: NO', even if i run it with --env="production".
I am not entirely sure if you are using .env files at root to specify different environment vars, but after going through this myself I realized that while when environment is 'local' it reads '.env.local.php', when environment is 'production', it wants '.env.php'.
It's in the docs, but I forget almost every time. I'm hoping that typing this out will help. :)

Categories