Laravel; "SQLSTATE[HY000] [2002] Connection refused" - mysql

I set up homestead 2.0 on OSX host and using Sequel Pro.
I can do migration and confirm that data are migrated in Sequel Pro, so it looks like no problem about DB connection.
But once I try to fetch data from Laravel 4.2 app, it fails to retrieve data and get following error.
PDOException (2002)
SQLSTATE[HY000] [2002] Connection refused
Here is my mysql configuration.
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '33060',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
Sequel pro setting is same above,
Name: 127.0.0.1
Host: 127.0.0.1
Username: homestead
Password: secret
Database:
Port: 33060
I tried change host name to 'localhost', homestead destroy and up, but No work.
Why?? Any suggestion helps.

I had exactly the same issue and I'm not sure why this happens.
However changing
'host' => '127.0.0.1',
to
'host' => 'localhost',
worked for me. Always simpler than what we expect!

For other devs there who are running Laravel on it's latest version (5.3) I just change these lines:
DB_HOST=127.0.0.1
DB_PORT=3306
Into this:
DB_HOST=localhost
DB_PORT=33060
Obviously, 33060 will be converted into 3306 but can't explain by now why localhost should work.

I solved the problem substituting the address 127.0.1.1 to localhost on the host parameter in the Laravel's .env file. I am working over GNU/Linux Debian 8, Laravel 5.6.3, PHP 7.1.8, Apache 2.4.27 and (Mysql) 10.1.26-MariaDB.

'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
Try this out it might work for you

I encountered the same problem for some reason neither of the above worked. For me I tried this in .env I changed DB_HOST to homestead.local. Homstead being the name of my site. This worked.
DB_HOST=homestead.local

I solved the problem by updating mariadb. I've checked what was outdated with 'brew outdated', then updated mariadb with 'brew upgrade mariadb' reload vagrant and issue was fixed. Hope this works for you.

Check your .env file for MYSQL_SSL=true.
We use SSL for MySQL in production not on dev after commenting the #MYSQL_SSL=true migration ran successfully.

Related

Laravel 5 on digitalocean: cannot connect to database

I have this in env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE={db_name}
DB_USERNAME=root
DB_PASSWORD={db_password}
I config/database.php
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false, //true is default
'engine' => null,
],
That's how I got the password:
cat /root/.digitalocean_password
But I get
SQLSTATE[HY000] [1698] Access denied for user 'root'#'localhost' (SQL: select * from `users` where `email` ....
I even cleared the password value in env file but ti doesn't work too. Any help please? Thanks in advance.
To backtrace your issue. try to connect via SSH on your database server . then try to do this
$ sudo mysql -uroot -p
$ password : ******** (enter your password here)
If you were able to connect, probably you shouldn't have any problem on your app level.
second option is : try to use host "localhost" instead of "127.0.0.1" as your root username might only pointed to #localhost instead of #127.0.0.1
Here's the solution I got:
I had to create another user rather than root and add all permissions to that new user which will sure have a new password. I used that username and password in .env file for database connection and it worked fine.

SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO)

I want to take all information from my database. Bu when i refresh the page error access denied occurs. How can i solve this. Can you please help me?
this is database.php;
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
this is .env;
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:HMSysJsvQBx9lKnctKT2Z+7Gv//of2W8O18uD+5wO0o=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=
The error is:
"SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO) (SQL: select * from tasks)"
Run:
php artisan config:clear
php artisan cache:clear
Or
php artisan config:cache
You will receive:
Configuration cache cleared!
Configuration cached successfully!
You must remember that .env datas are keep in laravel cache engine. So clear cache everytime when you make any changes in .env
I think you have to clear your config cache. Restart your application or run php artisan config:cache from your command line.
php artisan cache:clear
php artisan config:clear
php artisan optimize
Check on different browser better to check on chrome incognito after these commands.
Check if your username and password in the .env file are correct. It might be that user doesn't exist, or that the user has a password

Unable to connect to localhost database

This question has already been asked multiple times but none of the answers have helped me.
I am upgrading to Drupal 8 and to do so, I need to specify the login credentials for my Drupal 7 database.
SQLSTATE[HY000] [2002] No connection could be made because the target
machine actively refused it.
I can access the database perfectly fine to my SQL server is 100% running.
I have checked the credentials inside settings.php and it says the following:
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'karma-living.dev',
'username' => 'root',
'password' => '',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
So I copied above but I still get the error message above (yellow box).
See following image:
Does anyone have any suggestions? Thank you in advance.
The Acquia Desktop says your database name is "karma_dev", but you've entered "karma-living.dev" as the name for Drupal, so there is an inconsistency.

Laravel Can't Connect to database - Migrations - Error 2002

I have searched for a couple of hours now, and still am unable to find this.
I get 2 errors, if I use the database host as 'localhost', I get this error:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
and if I change the database host to '127.0.0.1' I get this error:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Things I have tried:
changing where the apache / mysql server is run (either user or `josh (Apache) / josh (MySQL)
changing the port that MySQL runs on in MAMP, and putting that port in the mysql array in the database.php file
changing the host of the connection from localhost to 127.0.0.1 and back.
creating a new user in phpmyadmin
turning off the firewall
Any ideas how to fix this?
I figured it out, add this after the 'host' => '127.0.0.1':
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'
So the connection would look like this:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => 'dbname',
'username' => 'josh',
'password' => 'pass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
fist clear your config file by typing
php artisan config:clear
Then modify your .env file
APP_URL = 127.0.0.1
DB_HOST = 127.0.0.1
Hope this works for you.
IF you use lampp . You can try this:
Open file mysql config. (you can open lampp control panel in /opt/lampp/manager-linux-x64.run,after that open Configure of Mysql Database or open /opt/lampp/etc/my.cnf).
Find and see "socket =/opt/lampp/var/mysql/mysql.sock",
Add 'unix_socket' => '/opt/lampp/var/mysql/mysql.sock' to file database.php in laravel:
'mysql' => [
'driver' => 'mysql',
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'luanvan'),
'username' => env('DB_USERNAME', 'huuthang'),
'password' => env('DB_PASSWORD', '123456'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
File mysql.sock depend on your install location of lampp. I let you see if you can't find it. Good luck
I had to do the following in ubuntu 15.10
copy the file or file contents from /opt/lampp/etc/my.cnf to /etc/mysql/my.cnf
Then open database.php file in your app/config folder and add the following line below 'host' => env('DB_HOST', 'localhost')
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock'
That solved my problem, could save someone else's. Please note that it is VERY IMPORTANT you first verify the directory which contains mysql.sock file before configuring the path.

DB connection Yii Framework [duplicate]

This question already has answers here:
Database connection error in Yii
(2 answers)
Closed 9 years ago.
I've got a new domain & hosting but I can't connect to the new db.
What I tried:
'db'=>array(
'connectionString' => 'mysql:host=ascodcurro.com.mysql;dbname=ascodcurro_com',
'emulatePrepare' => true,
'username' => 'ascodcurro_com',
'password' => 'xxxx',
'charset' => 'utf8',
),
If I try localhost as always it works fine.
User info from host:
MySQL
server: ascodcurro.com.mysql
DB: ascodcurro_com
user: ascodcurro_com
pw: *****
PhpMyAdmin
PhpMyAdmin: https://dbadmin.one.com/
user: ascodcurro_com
pw: ******
During the initial startup and working with Yii, the first problem that I faced was connecting to the database.
I was getting a really weird error and I was not able to connect to my DB on my Mac OSX Mountain Lion on my XAMPP stack. After countless searches, I finally figured out a method that worked. This is the only way I am able to connect to my DB for Yii.
First check if you can connect to the database normally using the
default configuration by uncommenting the 'db' configuration in the
main.php file. Example :
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=ascodcurro_com',
'emulatePrepare' => true,
'username' => 'username',
'password' => 'pass',
'charset' => 'utf8',
'tablePrefix'=>'',
'enableProfiling'=>true,
'enableParamLogging'=>true,
),
However, if you are still not able to connect to the database even after this, as I wasn't able to connect to mine, try taking this approach :
Make a new php file and type :
<?php phpinfo() ?>
check that page out, find out where 'mysql' is located and find out the 'MYSQL_SOCKET' in it and note the location.
After noting the location down, try this as your 'db' connection string(for example, for me the location is "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock") :
'connectionString' => 'mysql:unix_socket=/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock;dbname=practice',
And the rest as the basic parameters that you use.
And then try connecting again.
Hopefully, that should connect you to your DB.
Regards,
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=basename',
'emulatePrepare' => true,
'username' => 'username',
'password' => 'pass',
'charset' => 'utf8',
'tablePrefix'=>'',
'enableProfiling'=>true,
'enableParamLogging'=>true,
),
its my config. You get any errors?