How to connect to mysql app from java app?

I followed the docs to create a mysql fly.io app ( link and it works fine, I can proxy it on my localhost and connect to it, see the data, etc.
I don’t really know, or seems like I don’t understand, how to connect to the database from my java spring boot app. Here’s my sql app fly.toml:

app = "bikes4all-mysql"
primary_region = "otp"


kill_signal = "SIGINT"
kill_timeout = 5



[processes]
app = """--datadir /data/mysql \
  --default-authentication-plugin mysql_native_password \
  --performance-schema=OFF \
  --innodb-buffer-pool-size 64M"""

[mounts]
  source="mysqldata"
  destination="/data"

[env]
  MYSQL_DATABASE = "inginerie_software"
  MYSQL_USER = "db_user"


[build]
  image = "mysql:8.0.32"

and from what I seen in the docs, I have to access it something like bikes4all-mysql.internal
I tried it like this in my springboot application.properties:

spring.datasource.url=jdbc:mysql://https://bikes4all-mysql.internal:3306
spring.datasource.username=db_user
spring.datasource.password=somepassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

but I get erros like: WrongArgumentException: Malformed database URL, failed to parse the main URL sections.

As I said, if I instead use the proxy (i proxy it on port 13306) in my application.properties like this:

spring.datasource.username=db_user
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

everything works fine and it connects to the database

EDIT
I tried again, a clean start and a new fly app to deploy the sql, just to be sure there is nothing else that gets in the way.
I followed the exact steps in the docs, and this is how my sql fly app looks:

app = "mysql-bikes4all"
kill_signal = "SIGINT"
kill_timeout = 5

primary_region = "otp"

[processes]
app = """--datadir /data/mysql \
  --default-authentication-plugin mysql_native_password \
  --performance-schema=OFF \
  --innodb-buffer-pool-size 64M"""

[mounts]
  source="mysqldata"
  destination="/data"

[env]
  MYSQL_DATABASE = "inginerie_software"
  MYSQL_USER = "non_root_user"


[build]
  image = "mysql:8.0.32"

i do fly deploy, then fly scale memory 2048 and again fly deploy and it gets deployed. ( I do fly deploy before the fly scale memory 2048 because if no deployment before, i was getting this error:
failed to grab app config from existing machines, error: could not create a fly.toml from any machines :-( No machines configured for this app
So, after I ran these commands, if I go and now try to proxy it on my localhost, it doesn’t work.
flyctl proxy 13306:3306 -a mysql-bikes4all results into this output:
mysql-bikes4all.internal: host was not found in DNS
Any ideas? (This edit is for the purpose of recreating this scenario)

EDIT2:
I cloned the machine the sql was running on ( I had only one, and it was suggested to clone the current one to make them 2 ) so now I can proxy it on my localhost again, and I can connect on my command prompt or spring boot to the database ( if it’s proxied on localhost). But still can’t connect to it remotely, let’s say mysql -hmysql-bikes4all.internal in command line or spring.datasource.url=jdbc:mysql://mysql-bikes4all.internal/inginerie_software in my spring boot application.properties.

this is the output of mysql -hmysql-bikes4all.internal:
ERROR 2005 (HY000): Unknown MySQL server host 'mysql-bikes4all.internal' (11001)

It looks like you have a redundant https:// scheme in the prefix of the above…

jdbc:mysql://{your app name}.internal/{your db name}

{your app name} is name of the application when you did “fly launch” process.
{your db name} is the name you specified in fly.toml under MYSQL_DATABASE key.

https://community.fly.io/t/no-suitable-driver-found-for-mysql-database-docker-app-with-mysql-app/16000/2

I tried this too, but still the same error

EDIT:
Also, i noticed that I can’t ping the address, for example my fly database is at https://bikes4all-mysql.fly.dev , but as you can see if you try to access it nothing shows, and also, I can’t ping it from command prompt. Could this be the problem?

You typically won’t have a fly.dev address for the database itself, so this part is expected. (You can check via fly ips list -a bikes4all-mysql.)

Can you ping the internal address instead?

fly ping bikes4all-mysql.internal

Also, it would be prudent to make sure that the machine is still running, since auto-stop is the default these days…

fly m list -a bikes4all-mysql

Yes, so I tried pinging the internal address, and if I do it from my app’s folder it works and get this result:


but if i ping it from any other location like desktop I get this:

I’m guessing this is something normal though.
Also, the machines list output seems normal:

It is normal; fly gets the application name from your fly.toml when one exists in the current working directory, but otherwise you need an -a.

What are you trying to accomplish overall? Do you intend to run your Java app within a Fly machine in the end, or is the idea to continue running it locally?

I will publish the java app on fly too, but on other app. I am now just trying to connect to the database from my java when its running on local host, just to test it. What difference does it make?

If it was going to be remote long-term then you might want the fly.dev address mentioned above. (A complete WireGuard tunnel is usually a better option for that kind of scenario, though.)

Since that isn’t the plan, in reality, sticking with fly proxy is the path of least resistance—in my view.

(Granted, some users have been reporting sporadic DNS glitches with it, which may have been what you were seeing earlier.)

Yes, but in the long-term, the app will be fully remote, deployed on a fly machine. With that being said, as of today, my database is quite small, as no relative important data has been stored to the app, so, would it be a better idea to switch to the postgresql service provided by the fly create postgres that attaches it to the app when deployed? ( docs on postgres )I never used postgresql but this seems to be the best choice from my point of view

I agree. Postgres really is the default on Fly, and you’ll save yourself the effort of replicating things like health checks, the .flycast address, etc.

It is stricter about some things than MySQL is, if I recall the old debates correctly, so there will be at least a little adjustment, though…

I found so too, I ended up using amazon for the mysql service. Thank you for your time

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.