Laravel 'sail mysql' command connects without any creditials - laravel

friends.
Since I am new to Laravel sail and docker in general, I can't realy understand, is it a bug or a feature:
When trying this command: "$ ./vendor/bin/sail mysql", I'm automaticly get in MySQL cli whithout asking any creditials.
I supose that's not quite secure behavior.
I'd tried to set MYSQL_ALLOW_EMPTY_PASSWORD paran to '0' in my docker-compose, but that brang no any visible effects.

It's a convenience feature.
Sail automatically uses the password defined in the .env file (as well as user and database) to connect to MySQL. Since Sail is only intended for local development and the password is already saved in .env there shouldn't be any additional security concerns.

Related

Laravel route returns localhost despite I changed the env variable

I have APP_URL env variable set to domainname.com and I have url set to the same domain in config file as well. route('route.name') in artisan tinker returns the proper domain. yet when used in the applicaiton code, it returns localhost. Any thoughts from you would be appreciated.
Edit: My environment is Github codespaces
Try running php artisan optimize:clear command to clear cache,
routes etc all in one go.
If you have separate frontend then change it there as well to hit the
right backend with your provided domain name.
Restart your docker containers if you are using Docker.

Sail - Initiate Xdebug session from command line

So the last version of Sails makes it very easy to use Xdebug. Basically just had to define SAIL_XDEBUG_MODE in the .env file, configure path mapping in PhpStorm, activate listening and all was set - it works perfectly from the browser.
Now, how should I go if I want Xdebug to activate when I'm using the command line? Like when using artisan commands for seeding, or even better when using custom artisan commands created to run scripts to update some data... I can't find any arguments to add to my sail artisan myOwnCommand that would tell Xdebug it has to activate.
I'm working on Windows 11 with WSL2.
Thanks ahead!
Thanks to Derick's suggestion, I found out a solution. Prepending a sail call with anything wouldn't help, since sail calls scripts in the docker container, and your environment variable wouldn't be set there. But since it was just about setting an environment variable, it can be easily done in docker-compose file.
I just had to add PHP_IDE_CONFIG: 'serverName=0.0.0.0' in the environment section of my Laravel service. Of course replace 0.0.0.0 with your own server name. Then, instead of running sail artisan test or sail artisan my:command you replace artisan with debug, as stated in the docs.
Now you can use command debug to run with Xdebug (ex. sail debug myOwnCommand).
Here is documentation: https://laravel.com/docs/9.x/sail#xdebug-cli-usage

Which is the right way to run laravel migrations using Google Cloud Run and Google Cloud SQL

I came here to expose the specific way that i found to run migrations into a Google Cloud Run project using Google Cloud SQL and Laravel, which is simple, i just connect from my .env laravel to Cloud SQL (using Cloud SQL Proxy) and from my local console i run the migrations with the classic command php artisan migrate.
I have never found another way to run migrations as i'm currently making them and actually i have never found more information about it, so that is the question:
Is there another simply and secure way to run laravel migrations to Google Cloud Run than running them in local enviroment?
Short version (very short version):
Create a new file and call it anything (e.g. "db-migration.sh") and add following :
#!/bin/bash
# Run Laravel migration (by force, since it would be a prod-environment)
php artisan migrate --force
# Run Apache in "foreground" mode (the default mode that runs in Docker)
apache2-foreground
Add following 2 lines to your Dockerfile
# Make the file executable, or use "chmod 777" instead of "chmod +x"
RUN chmod +x /var/www/html/db-migration.sh
# This will run the shell file at the time when container is up-and-running successfully (and NOT at the BUILD time)
ENTRYPOINT ["/var/www/html/db-migration.sh"]
Assumptions:
Docker image is having PHP/Apache combination (and not Alpine/Nginx version)
Cloud Run is configured correctly
Cloud SQL is up-and-running and attached correctly with Cloud Run
Check "Logs" tab of Cloud Run service to verify if the migrations executed successfully (screenshot of a test migration attached)
You may make use of app-engine-exec-wrapper, which is a helper for Cloud SQL Auth proxy. It will help you connect with the database securely. This is an example of how you would use it for Django applications:
- id: "apply migrations"
name: "gcr.io/google-appengine/exec-wrapper"
args:
[
"-i",
"gcr.io/$PROJECT_ID/${_SERVICE_NAME}",
"-s",
"${PROJECT_ID}:${_REGION}:${_INSTANCE_NAME}",
"-e",
"SETTINGS_NAME=${_SECRET_SETTINGS_NAME}",
"--",
"python",
"manage.py",
"migrate",
]
You can visit their github page to know more about this.
You can add a controller function to execute an Artisan command if called using HTTP Post Request.
For instance, use this controller function for /migrate path on your app (registered with Route::post in your routes file).
public function migrate(Request $request)
{
Artisan::call('migrate');
}
And you can execute the Artisan migrate command by sending a HTTP Request to /migrate
curl -request POST \
--header "Content-Type: application/json" \
https://you-cloud-run-app-url.run.app/migrate
But the downside of this solution is security. You have to make your own middleware to secure this action from un-allowed request (or use an OAuth2 protocol).
Reference:
https://medium.com/#kolban1/shell-exec-with-cloud-run-fbc6d299f6d4
Run artisan command in laravel 5

Laravel Sail Docker Hub

I've got laravel sail which as I know is few containers (mysql, redis, laravel, ...). Is there an easy way to just pack up the whole thing to ex. Docker Hub and easly download it on production server, and when i update it on localhost and run docker push, just run docker pull. Then everything (like new commands in DockerFile | apt install thing) will be updated and working exacly how it worked on localhost
I read the documentation, but I cannot figure out how docker works and how to easly change project location (Ex. I'm working on project at work, sometimes at home and this will be much easier to run docker push when I need build source code and deploy it)
I'm keeping source code on github, and it's working for dev servers, but to deploy something I have to check all dependencies and DockerFile, .env file and other things to make it works on production.
Thanks for help!
You can use the existing docker-compose.yml and just run docker-compose up -d on production to start all containers. Just be sure to for example disable xdebug on production as it slows down every request.

laravel 5 - when trying to retrive data from phpmyadmin: SQLSTATE HY000 1698 Access denied for user 'root'#'localhost'

That's my first post - I hope my problem explanation will be easy to understand. I just started with Laravel5.
I want to retrive data from mysql database that was successfully created using migrations. I'm following laracast series. I created route, controller and view to display the data. Unfortunatelly, when I enter browser to see it, I constantly get this error:
`SQLSTATE[HY000] [1698] Access denied for user 'root'#'localhost'`
enter image description here
My .env and database.php files are configured. Also, I can do migrations with no error (and I see result in phpmyadmin to which I can log in). When I type php artisan tinker, I can do various operations on my databases. The problem arises only when I want to display data in my laravel site.
I did vast research of the problem and tried numerus tips like:
1.change local host to 127.0.0.1
2.change my database login and pass for homeostead, secret
3.I did
`GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY 'Your_Password';`
When I type mysql --user=root -p I can log in but command show databases acts like never ending and shows nothing.
and other ...
Now, I'm at loss for ideas what to do next, please help me. I will appreciate that a lot cos I'm really stucked at this point!
my .env file:
`DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=haslo`
I use homeostead and Laravel 5 on Windows 7
Server type: MySQL5.7.15
PHP version: 7.1
Apache/2.4.23, PHP/7.1
have a look at my answer at Unable to connect to local MySQL DB using Laravel
That could be the answer for you as well.
If you use homestead with vagrant, then laravel will be lunched inside vagrant and 127.0.0.1,3306 will mean the mysql inside vagrant, type vagrant ssh and check for the database.
Try this command:
Php artisan config:clear

Resources