4 months ago
I am trying to change the image of out of the box MySQL service but it is not allowing to downgrade from major version.
When i am using custom service and providing mysql:5.7.44 docker image then it pulls the image and says to setup variables. Even after setting up variable or not, i am not able to connect it using internal host in one of my another java based service.
Can you guide as i am new to railway and chosen it for a deployment but mysql is causing trouble since 2 days.
I have seen some other thread related to same but in every thread there is no solution or guide proper, but the railway developer/focal person settle it directly by accessing the customer's project.
So it would be appreciated if you can help me in the same way or it would be more better if you can share the details process. Anyone will be appreciated.
Thanks
6 Replies
4 months ago
Hey there! We've found the following might help you get unblocked faster:
If you find the answer from one of these, please let us know by solving the thread!
4 months ago
As you point out, you need to set the environment variables for the service to function.
The fastest and easiest way to get started is by deploying MySQL from a template. See, for example, the MySQL template: https://railway.com/deploy/mysql
Status changed to Awaiting User Response Railway • 4 months ago
4 months ago
Thanks @christian for the response.
When i do that using template the steps are. I added a service 'Database' -> 'MySQL'. Set the image to '5.7.44' and wipe the volume if trying for 2nd time onward and Deploy it. It throws this error.
Volume is for sure empty as i wipe it but as i cannot access the directory thats why couldn't 100% confirm. Template it causing this issue. Can you guide here?
Attachments
franceskora
Follow these steps exactly to build a working MySQL 5.7 service from scratch.1. Start FreshDelete any of your previous failed MySQL services to avoid confusion and ensure the volume is clean.2. Deploy from a Custom ImageOn your Railway dashboard, click "+ New" -> "Service".Choose "Deploy from Image".In the "Image Name" field, enter exactly: mysql:5.7.44Click "Deploy". Railway will pull the image and create the service. It will likely show an error initially because it's missing its configuration.3. Configure Environment VariablesThis is the most critical step. The official MySQL image needs these variables to set itself up.Go to your new MySQL service and click on the "Variables" tab.Add the following variables:MYSQL_ROOT_PASSWORD: This is mandatory. Set a strong, secure password here. The service will not start without it.MYSQL_DATABASE: This is highly recommended. Set the name for your application's database (e.g., mydatabase). The container will create this empty database for you on its first run.4. Attach a Volume for Data StorageA database needs its data to be permanent. You must attach a volume.Go to the "Settings" tab of your MySQL service.Scroll down to the "Volumes" section.Click "Add Volume" and set the "Mount Path" to: /var/lib/mysqlThis is the standard directory where MySQL stores its data files.5. Connect Your Java AppOnce you add the variables and volume, your MySQL 5.7 service will redeploy and start correctly. Now you can connect your Java application.Go to your Java service's settings.Railway automatically provides environment variables to connect to other services in the project. The ones you need will be named MYSQLHOST, MYSQLPORT, MYSQLUSER (which will be root), MYSQLPASSWORD, and MYSQLDATABASE.Ensure your Java application's database connection string is configured to read these variables from the environment.By following this custom image approach, you bypass the incompatible MySQL 8 template and build the exact service you need. This will resolve the connection issues.
4 months ago
Thanks @franceskora for the detail steps.
I agree and I am clear on this that I need to setup custom service to get the older MySQL DB version but it's still throwing errors differently on each step. Let me share each error in detail.
==> When i follow your steps 1 to 3. I am done with running mysql but it is showing that variables are missing. As you mentioned to add only 2 variables so i added those 2 mentioned in Step#3. But the error in Data tab is as mentioned in the image below:
==> How to proceed from here? Do i need to add these variables? If YES then If i try to add user and password that it says a Warning that:Warning: MySQL authentication changes
This will manually change both the username and password variables without updating the actual database credentials.
What will happen:
Database will still use the old credentials internally
Applications may try to connect with the new credentials and fail
Due to this, even if i setup some username password, I am not able to connect with it from other railway service.
Additionally I am facing issue on Step#4 that even if i wipe the volume multiple times and even change the volume mount path, it keeps on saying this error
I hope errors are clear to you know.
Appreciated your support
franceskora
Part 1: Fixing the "Data Directory Has Files" ErrorThis error is happening because a previously failed deployment left some files in the volume. When MySQL tries to start again, its initialization script sees those files and aborts to avoid overwriting data. Wiping the volume can sometimes fail to clear it properly if it's still attached.We need to force a completely clean slate. Follow these steps exactly:Detach the Volume:Go to your MySQL service Settings -> Volumes.Find the attached volume and click the "Detach" button (or a similar detach/remove icon).Delete the Old Volume:Now go to your project's main dashboard where you can see all your services. There should be a "Volumes" tab for the whole project.Find the volume you just detached and permanently delete it. This ensures the corrupted data is gone for good.Redeploy Without a Volume (Crucial Step):Go back to your MySQL service and trigger a new deployment with NO volume attached.The service should deploy successfully. It will be running in an "ephemeral" mode, meaning data will be lost on restart, but this proves the Docker image itself is working. Wait for the green "Deployment Online" check.Create and Attach a NEW Volume:Once the deployment is online, go back to Settings -> Volumes.Click "Add Volume".Create a brand new volume and set the Mount Path to /var/lib/mysql.This will trigger one final redeployment.This final deployment will now run the MySQL initialization script on a brand new, truly empty volume, and the error will be resolved.Part 2: Understanding the "Required Variables" WarningNow, let's address the issue from your first screenshot. You can and should ignore this warning.Here is the simple explanation:Your Variables (MYSQL_ROOT_PASSWORD): These are the variables the MySQL Docker image needs to set itself up for the first time. You have configured these correctly. These are the ones that matter.Railway's Variables (MYSQLHOST, MYSQLPASSWORD): These are variables that Railway generates automatically after your service is running. It creates them so your other applications can easily connect to this database.The warning in the "Data" tab is a UI feature designed for Railway's official templates. It doesn't fully understand the needs of a custom Docker image, so it shows a generic prompt. It will not stop your database from working.The second warning you saw ("Database will still use the old credentials internally") is shown because you tried to edit the generatedMYSQLPASSWORD variable. Railway is correctly telling you that changing its reference variable won't change the actual root password you set inside the database with MYSQL_ROOT_PASSWORD.In summary: Follow the "clean slate" steps in Part 1 to get your database running. The variable warning in the UI is just a notification and can be safely ignored.
4 months ago
Thanks @franceskora for the detailed steps.
I have tried to follow. With slight changes like i used mount path as "var/lib/mysql1" instead of '/var/lib/mysql" because even after wiping it was throwing same errors with volume.
Anyhow mysql seems running fine now with these logs.
As you mentioned, I have only setup 2 variables as mentioned in the screenshot below:
Now, when i am referencing it inside my backend java service like below:
It is not connecting and throwing error of communication link failure.
In my java spring boot project application-prod.properties file i have tried 2 ways. 1st is the comment lines 1 to 6. Secondly via Line 8,9,10. But still same connection failure error. Any clue?
4 months ago
Thanks @francekora for the help this far. Appreciated.
Just wanted to share the final steps so that if anyone gets stuck here can see this summarized message to sort it out.
I was trying to setup mysql:5.7.44 on railway custom service. I did this by creating a new service, set the image to
mysql:5.7.44After that I added some environment variables as mentioned in the screenshot below:
Set the values of each. (Note: User should be other than root)
Turn on the service by deploying it. Once done then create a
Volumeand set its mount path to default/var/lib/mysql. And deploy the changes. It should setup Mysql.
(Note: If it throws error of directory already contains data then try to wipe the volume first and re-deploy. If it still doesn't work then try to change the mount path like this one/var/lib/mysql1worked for me. Also ignore any warning coming in during this process.)Once done, use these variables like these in your other service. The right way to reference a variable in other service is mentioned below:
Here in
mysql.MYSQL_USERmysql is the name of your MYSQL service and MYSQL_USER is the variable of that service exporting.And it will make things work fine.
Thanks.
Status changed to Solved brody • 4 months ago