Using Laravel 8
Ubuntu 20.04 with Docker
Connect to Database over clients worked perfect. Docker-Container worked.
If I dont start it with php artisan serve it refused the connection to mysql.
With php artisan serve it connects to mysql-docker-container.
I want deploy it. But if I make it like this doc: https://laravel.com/docs/8.x/deployment
it worked fine, but only DB-connection crashes.
I tried: SQLSTATE[HY000] [2002] Connection refused within Laravel homestead
But if I change the Host it answer with this mistake: PDOException: SQLSTATE[HY000] [2002] No such file or directory
need help.
dont forget... WSL :)
use mysql not localhost.
This question already has answers here:
SQLSTATE[HY000] [2002] Connection refused within Laravel homestead
(31 answers)
PDOException SQLSTATE[HY000] [2002] No such file or directory
(37 answers)
Closed 1 year ago.
I'm having issues connecting to a docker instance of MySQL through Laravel. I've found that by using Tinker, I can connect successfully to the database. e.g, with Tinker, if I run
DB::select("select * from users;");
I'm able to get results. However, if I make a route
Route::post('/sql', function(Request $request){
return DB::select('select * from appointments;')->get();
});
and hit it through PostMan, I get back
"message": "SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `users` where `users`.`id` = 1 limit 1)",
I currently have the .env file set with
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test_project
DB_USERNAME=root
DB_PASSWORD=password
I've read that many people have trouble with using 127.0.0.1, but I've found that when I switch the DB_HOST to localhost, tinker doesn't even successfully connect. Instead, I get
Illuminate/Database/QueryException with message 'SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from users;)'
I've already tried
php artisan route:clear
php artisan config:clear
php artisan cache:clear
But I cannot get this to work. Any help or insight is greatly appreciated!
I deleted the migrations table from a Laravel 5.4 database named laravel. When I run php artisan migrate:install, I get this error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] No such file or directory
(SQL: select * from information_schema.tables where table_schema = laravel
and table_name = migrations)
I deleted and recreated the database. I also ran composer update. No luck. I can run the command in phpMyAdmin and create the table manually.
This problem sometimes also manifests itself with similar 2002 errors:
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.
(SQL: select * from information_schema.tables where table_schema = laravel
and table_name = migrations and table_type = 'BASE TABLE')
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
(SQL: select * from information_schema.tables where table_schema = laravel
and table_name = migrations and table_type = 'BASE TABLE')
If you are using localhost as your DATABASE_HOST in the .env file, change it to 127.0.0.1, then run php artisan config:clear and now try php artisan migrate:install again.
Here's what I recommend for .env file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
Then in Database.php add folder location:
'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
Finally, try:
php artisan config:clear
php artisan migrate:install
I fixed this issue by setting environment variables in .env file:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
Actually I just changed from DB_HOST=127.0.0.1 to DB_HOST=mysql.
If you are using MAMP Pro (not always necessary for the free version of MAMP) then the best thing to do is add this to your .env file:
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
This assumes your MAMP installation is at /Applications/MAMP of course (the default).
Credit: A comment in this question by #Uncoke
I am using MAMP on macOS and after editing localhost to 127.0.0.1 and port to 8888 this problem fixed by adding the following
'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
to config/database.php file.
For mac users, add this to your .env file
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
In some cases this may happen because mysql server is not running. It happened to me using Laravel 5.7 and restarting mysql server solved it. For ubuntu check the status of mysql server service mysql status. You can restart mysql server using command service mysql restart
Posting a resolution to my own question:
I deleted the folder and recreated the code base, making sure to point my environment to the correct database server. This time it worked. I don't know exactly what had gone missing to cause the original error.
I am trying to run Laravel app on Heroku
I've pushed the app and configured it. But when I am trying to migrate database via using command heroku run php artisan serve it is giving following error.
In Connection.php line 664:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_s
chema.tables where table_schema = forge and table_name = migrations)
In Connector.php line 68:
SQLSTATE[HY000] [2002] Connection refused
I've made changes in .env file too for DB credentials and host.
You need to set the environment variables in Heroku's web admin panel
Go to your app in Heroku -> settings -> Reveal Config Vars
Then set your environment variable
Take for example
DB_DATABASE = your-database-name
DB_HOST = your-database-host-name
DB_PASSWORD = your-db-password
DB_PORT = your-db-port
DB_USERNAME = your-db-username
I have uploaded my Laravel 5.5 project to the centOS7 server. Now I am facing the following error when I am running "php artisan migrate" for the first time to load database table.
Error message screenshot
Here is my .env db section
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=event
DB_USERNAME=root
DB_PASSWORD=testpass
I have created the DB in the MySQL.
[root#li129-226 event_management]# php artisan migrate
In Connection.php line 664:
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = event and table_name = migrations)
In Connector.php line 67:
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
In Connector.php line 67:
PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]
What I did that worked really well was to just alter the root user to have a password. Log in to mysql on the terminal then do this command:
ALTER USER 'user'#'address' IDENTIFIED WITH mysql_native_password BY 'yourpassword'
for the user, I used 'root'#'localhost' You may want to use a different user.
Then, replace DB_PASSWORD=yourpassword with the password you chose in the command above and save the file.
That allowed me to run php artisan migrate and everything worked great!