Attempt on Laravel/MySQLconnection with WSL and XAMPP - php

First I will explain my setup: I run Laravel in WSL (Windows Subsystem for Linux) and I have XAMPP on Windows.
What I'm trying to do is to run MySQL service on XAMPP and connect it with my Laravel project. I have modified my .env file setting the root password and I have created the "laravel" database with phpMyAdmin.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=mypassword
With this setup I tried to run in WSL
php artisan migrate
But I got this error
SQLSTATE[HY000] [2002] Connection refused
Then I changed the DB_HOST to localhost, that is a common solution for this, but then I got this other error.
SQLSTATE[HY000] [2002] No such file or directory
Now I have no idea what else to do. I got stucked. Any help would be appreciated.

It seems you are running MySQL via XAMPP, under Windows, not in Linux (Ubuntu?). You should be running MySQL in the WSL2.
See this---
WSL stack overflow issue

After a lot of digging, I found a solution.
First, cannot use localhost to communicate with XAMMP, but it should use $(hostname).local. You can test it by typing ping $(hostname).local in the WSL terminal: the keyword will be replaced by the host name and IP.
Second point, the app will be connecting from a "foreign" address, not localhost. Therefore, go to your phpMyAdmin interface, select the MySQL user that Flask is using, go to "Login information" and select "Any Host: %" in the "Host" section. I found a guide here.
Note that now this user can be accessed by your network, so maybe choose a secure password.
More specifically, I had a similar problem with a python Flask app running in WSL, with XAMPP running in Windows. In the app, I had to change my SQLALCHEMY_DATABASE_URI variable to account for the different localhost. The variable should look something like this:
export SQLALCHEMY_DATABASE_URI="mysql+pymysql://sql_user:sql_password#$(hostname).local/YOUR_DATABASE"

Related

Laravel - User Access denied

I created my first App, using Laravel in the Backend. Everything was running on my localhost and I deployed everything to my Netcup Webhosting Server.
I run composer install successfully, generated the API key by using php artisan key:generate --ansi and set up my .env file. The File looks like:
APP_NAME="App Name"
APP_ENV=prod
APP_KEY=base64_key
APP_DEBUG=false
APP_URL=https://xx.xxxxx.de
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME="db_user"
DB_PASSWORD="db_user_password"
The I want to create all the database tables by using php artisan migrate. I tripple checked the values set in the .env and red a lot of solutions of other posts, but nothing worked. I keep getting the following error:
I already added quotes to DB_User and Pasword in .env file. I restarted the server several times and cleaned the cache.
It looks like some problems with auth on the Netcup Web hosting Server. Can you check list of user with access to db_name? Besides, by screenshoot I guessing that hosting add prefix ("k177581_") for db_name and db_user
Solved the issue:
As #N69S correctly suggested it was related to Netcup. For any other Netcup user the following solution.
Netcup devides its Webhosting and Database. Therefore you need to put you Database IP into your .env file. That can be found in the connection data.

Accessing a remote database is giving an error on Laravel

I'm trying to access a database (which exists on a different server) on my Laravel application. I keep getting the following error: SQLSTATE[HY000] [2002] Permission denied
My .env file is set up as follows (changed a bit to protect privacy):
DB_CONNECTION=mysql
DB_HOST= IP_ADDRESS
DB_PORT=3306
DB_DATABASE=DB_NAME
DB_USERNAME="laravel_user"
DB_PASSWORD="password"
I'm not sure if this is important or not but I can access the web server's database if I go to the link IP.ADDRESS/phpmyadmin
My web.php file is as follows:
Route::get('/test', function(){
return DB::select("select * from umts_list");
});
Additionally, I also went to IP_ADDRESS/phpmyadmin (where my database exists) and added a new user called laravel_user with the ability to SELECT all data and saved it with the 'password' as its password.
My attempt: I tried whatever I mentioned above and also cleared the cache since I made changes to the .env file by entering the following commands:
php artisan config: clear
php artisan cache: clear
Any help would be appreciated. Thank you.
Edit 1: On phpMyAdmin, I made sure to add the user as depicted by the following picture:
PhpMyAdmin User Account
Additionally, I also checked the port number on phpMyadmin and it is 3306 as depicted by the following image:
Port Number
just in case anyone else stumbles upon this post in the future, it turns out that I did not have to create an SSH tunnel or whitelist the IP. The problem was actually with SELinux. This post was the solution: Laravel permission denied on remote Mysql server (AWS aurora)
Specifically, I ran the command: sudo setsebool -P httpd_can_network_connect_db=1
This worked perfectly and it was actually blocking connection to remote databases.

MySQL+Laravel: SQLSTATE[HY000] [1045] Access denied for user 'so'#'localhost' but connection data is OK and config cache is cleared

I need help here, because it's driving me crazy. I'm trying to connect to the remote production DB but I'm unable. I have read tons of posts about this that are focusing on the credentials and also running the following commands to clean config that are stored in Laravel cache:
php artisan cache:clear
php artisan config:cache
php artisan config:clear
Laravel 5 error SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
Any of these are working for me.
I have checked very well the credentials both in my .env file and my database.php. I'm able to connect to a local database by using the ip "127.0.0.1" but I'm unable with the production DB stored in Google Cloud. It's good to mention that my local IP is allowed to access that Database and I'm able to connect by using MySql Workbench.
Here is what I see when I perform "php artisan tinker" and then "DB::connection()->getPdo();" to check my connection from terminal:
The only thing that calls my attention is the message containing "so#localhost" when I'm not performing a connection to localhost but I don't know if that's related with the issue.
Any other suggestion? I have lost too much time trying to find out what could be happening.
EDIT 1: Laravel Framework 5.8.38
After two days fighting with this one, I readed tons of posts and a lot of unuseful solutions I have found my solution by looking at this video.
Basically, you must stop you XAMPP MySQL Server and open C:\xampp\mysql\bin\my.ini and edit by adding the following command "skip-grant-tables" after the [mysqld] attribute. The result in this file might look this way:
# The MySQL server
default-character-set=utf8mb4
[mysqld]
skip-grant-tables
socket="C:/xampp/mysql/mysql.sock"
...
After that, I ran "php artisan tinker" and DB::connection()->getPdo(); and everything was connected again, but it's good if you first clean your cache and config.

Upload Local Laravel Project to Server

I've created a Laravel 5.3 project on my local apache server(XAMPP) and it works fine with login and registration.
However, when I upload(via FTP) to my server(the server is 000webhosting) and try to do login it returns an error saying:
PDOException in Connector.php line 119:
SQLSTATE[HY000] [2002] Connection refused
I've already changed the .env to match the server DB's info, and just in case I also changed the config/database.php to also match. But it still appears like that?
Do you know exactly everything I have to change in my project so it can work on the server?
May be Your database password changed in live. update .env file

Laravel 5.1 migrate database to hosted mysql

How would I go about doing this (the title is the question), I've changed all my ENV files to my hosted mysql details. When i run php artisan migrate from composer I get the
"denied username#localhost using password yes error".
I've changed the port and everything but I'm not sure what to do anymore and I'm 99% sure that the details are correct I've tried them multiple times
You need check somethings:
are credentials valid (username, password and host)?
the database host is accessible by your application server?
.env file is correctly configured or have some config hard coded in app/config/database.php?
To validade connection, you can try this: http://www.w3schools.com/php/php_mysql_connect.asp
About configuration and enviroments : https://laravel.com/docs/5.2/configuration

Categories