0

I have a Laravel application using MySQL hosted on Google Cloud Platform (GCP). I am successfully able to build and deploy on Google Cloud platform. I am getting 500 server error when I visit the site url. I have also created a MySQL instance and imported the SQL file. Here is my .env file.

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:+ly4U3QbWcULnj0A31g41q7Yim/L0W5olgwPbsuD1/A=
APP_DEBUG=false
APP_URL=http://127.0.0.1:8080

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tabiboba
DB_USERNAME=root
DB_PASSWORD=root

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

I dont know why am I getting 500 error. I assumed it has to do something with the database connectivity, so when I go to google cloud shell, I get Access denied for user 'root'@'35.204.189.236' (using password: YES)

enter image description here

Please let me know what am I missing here :(

za_ali33
  • 366
  • 2
  • 9
  • There's tonnes of things that could be wrong here, but the most obvious that strike out to me is 1) does the user exist AND the password is correct AND the host is allowed?, and 2) For your application itself, you have `DB_HOST=127.0.0.1`, but you're using a cloud SQL instance, which isn't hosted locally, do you have CloudSqlProxy running on your application's machine to make localhost valid, or should you update it with the IP of your SQL instance? – Matthew Jan 20 '23 at 14:54
  • 2
    The database doesn't like your user/pass combination. Are you sure you're using the right user? I'm going to guess it really shouldn't be `root`, and `root` shouldn't have access from other systems. – aynber Jan 20 '23 at 14:55
  • Even if I update db_host with the instance's IP address, still I get 500 error. @aynber the roote user exists in the instance's users list. This is the default user. – za_ali33 Jan 20 '23 at 15:18
  • @za_ali33 There is no such thing as root user. In mysql user names haves two parts: user id (this is root) and host name. Do you have a user with the host name in the error message or with %? – Shadow Jan 20 '23 at 15:21
  • 1
    Yes, `mysql` by default has a `root` user, but by default, `root` can only access the database from localhost or a whitelisted IP. Root should never be able to access from anywhere. You should create a dedicated user with specific access to the database, and use that. https://support.google.com/appsheet/answer/10107301?hl=en has some information on how to do that. – aynber Jan 20 '23 at 15:30
  • @aynber ok I created another user. Now I am getting SQLSTATE error `Illuminate\Database\QueryException SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `instagram_feeds` order by `media_timestamp` desc limit 8)` – za_ali33 Jan 20 '23 at 18:38
  • @Shadow yes there is a root user with a % in the users list but in the error message however, it is without % and it includes ip address. – za_ali33 Jan 20 '23 at 18:44

1 Answers1

1

The initial error you mentioned is for access related issue which could occur due to a variety of reasons:

  • Connecting to wrong host : Try to connecting the host directly and confirm right host
  • User does not exist : Create a new user and try connecting
  • User exists but client host does not have permission to connect:Grant permission and then try to connect using this.
  • Password is incorrect,or some character issue during password entry: Try with a well known password and check for any character issue while password is taken

You should also check if trying to access the server from a host that is different from the defined host name.Make sure that the IP address you are connecting to and from is authorized to the Cloud SQL instance, refer the documentation for information on authorizing connections

Also check Google Cloud SQL database permissions and verify that your MySQL login has been "granted" permission to login.Go to the Google Cloud SQL console>Go to the Users tab>Select your user name>Verify that the host is your public IP address or optionally use % to mean any host. You can use the Cloud SQL Proxy to access Cloud SQL. This is secure and you do not need IP addresses for Cloud SQL.

Also check out these links for similar issue:

Vaidehi Jamankar
  • 1,232
  • 1
  • 2
  • 10