Failed to connect to external MySQL Database

pamedsi
HOBBY

18 days ago

I'm deploying a Docker Image for a Spring Boot application, if you want to test, the image is: bravit0924/mee-api
The environment variables required are:

DATABASEURL="jdbc…" DATABASEUSER="yourdbuser"
DATABASEPASSWORD="yourdb_password"
SECRET="anything"

I'm always getting the error: Access denied for user 'usuario'@'162.220.234.16' (using password: YES)
But, I just, allowed all possible IPs on my database instance, and the appplicattion still can't connect.

I don't know what is happening, but I'm sure the problem is not on database host.
I removed all IPs allowed from the database host, and tried to connect locally with Dbeaver, and got the error as expected, after that I configured the database for allowing all IPs, then it worked on Dbeaver, but it was still not working on railway, how can that happen?

Yes, all the variable values are correctly set.

Solved$10 Bounty

2 Replies

pamedsi
HOBBY

18 days ago

f0218967-5df8-4912-b78a-64c33ddadb44


phoenixauro
HOBBYTop 10% Contributor

18 days ago

This looks like a privilege issue at your database end.

Run
SELECT user, host FROM mysql.user WHERE user = 'usuario';

can you paste the output here.

try creating and using a new user that is allowed from all hosts, replace yourdb_password and your_database

CREATE USER 'test'@'%' IDENTIFIED BY 'yourdb_password';

GRANT ALL PRIVILEGES ON your_database.* TO 'test'@'%';

FLUSH PRIVILEGES;


turborx
FREETop 10% Contributor

17 days ago

hi, railway’s mysql plugin enables a tcp proxy by default for external connections
Railway Docs to connect from outside:

  1. open your project’s mysql plugin and click connect from outside railway.

  2. copy the host, port, username, password, and database values.

  3. in your client run, for example:

mysql -h  -P  -u  -p  --ssl-mode=DISABLED

or use a uri:

mysql://:@:/?sslMode=DISABLED
  1. ensure your local IP isn’t firewalled.

you’ll be billed for network egress when using the tcp proxy, but this will let you connect externally.


turborx
FREETop 10% Contributor

17 days ago

hmm.. after testing and some research.


turborx
FREETop 10% Contributor

17 days ago

don’t use the external host or ip‑whitelisting for an in‑project service railway injects internal mysql env vars you should use instead. set in your [application.properties](application.properties) or application.yml:

spring.datasource.url=jdbc:mysql://${MYSQLHOST}:${MYSQLPORT}/${MYSQLDATABASE}?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=${MYSQLUSER}
spring.datasource.password=${MYSQLPASSWORD}

railway will route this over its internal proxy. if you really need the public host, make sure your mysql user has privileges from any host ('usuario'@'%') or specifically the railway egress IP, e.g.:

grant all privileges on your_db.* to 'usuario'@'%' identified by 'your_password';
flush privileges;

then redeploy and it should connect.


pamedsi
HOBBY

17 days ago

Thank you, man! It really worked.

Unfortunately, for now, I can't use a railway database, but I'll remember this recomendation in the future!


turborx
FREETop 10% Contributor

17 days ago

yw


Status changed to Solved chandrika 8 days ago