Deploying a Node.js Project with MySQL on Railway
1. Create a Railway Account & Log In via GitHub Go to the Railway website. Create an account or log in using your GitHub credentials. 2. Deploy a New Project After logging in, click on “Deploy a New Project”. Choose MySQL as the first service you want to deploy. Select the MySQL image. Once the MySQL container starts, you’ll see an option at the top-right corner of the page to configure your server. 3. Configure the Server with GitHub Repository Railway will ask you “What do you need?” Select GitHub repo to link your project from GitHub. Click on Configure GitHub App, and it will show you your repositories. Choose the repo that you want to deploy. Wait for 2 minutes for Railway to configure your project. After the configuration is done, you will be able to see your repo name in the server image list. Click on it to continue. 4. Deployment and Configuration After clicking the repo name, Railway will open a window with different menus: Deployments - Variables - Metrics - Settings Under Deployments, you’ll see logs showing the deployment status and success/failure messages. 5. Set Up MySQL Variables Under the Variables section, add the following environment variables for MySQL: MYSQLDATABASE MYSQLHOST MYSQLPASSWORD MYSQLPORT MYSQLUSER Make sure these variables match the ones generated by Railway for your MySQL service. Note: After adding the variables, Railway will automatically connect to MySQL. If it needs to create tables or connect, the server code will handle it. 6. Fixing Common Errors Root Directory Issue: If you’re working with multiple projects (e.g., client and server), Railway may prompt you to specify the correct root directory for your server. If you have two directories, ensure that the server directory is set as the root directory in the settings. 7. Get Your Custom Domain In the Settings tab, you’ll see an option for Networking. Click on Generate Domain to get your custom domain name for the deployed service. 8. Server Code Setup: Create .env File In your server project, create a .env file in the root directory and add the following environment variables (make sure to replace the values with your own generated ones): # Server Configuration this for loacl PORT=5000 DB_HOST=localhost DB_USER=root DB_PASSWORD=Sharan@123 DB_DATABASE=people_pro # Railway MySQL config MYSQLHOST=tramway.proxy.rlwy.net MYSQLPORT=17091 MYSQLUSER=root MYSQLPASSWORD=RcMTiimLAURmUcweLMDtrgDwGvmZdFZw MYSQLDATABASE=railway 9. Update db.js (Database Connection) Update your database connection code (db.js) with the correct environment variables, and make sure it's configured to connect to the Railway MySQL service: // This code for local import mysql from "mysql2" import dotenv from 'dotenv'; dotenv.config(); const con = mysql.createConnection({ host:process.env.DB_HOST, user:process.env.DB_USER, password:process.env.DB_PASSWORD, database:process.env.DB_DATABASE }) con.connect(function(error){ if(error){ return console.log("Connection error"); }else{ return console.log("Coonected"); } }) export default con; // This code for server import mysql from "mysql2"; import dotenv from "dotenv"; dotenv.config(); const con = mysql.createConnection({ host: process.env.MYSQLHOST, port: process.env.MYSQLPORT, user: process.env.MYSQLUSER, password: process.env.MYSQLPASSWORD, database: process.env.MYSQLDATABASE, }); con.connect((error) => { if (error) { console.log("❌ Connection error:", error); } else { console.log("✅ Connected to the MySQL database!"); } }); con.query('SHOW TABLES', (err, results) => { if (err) { console.log('❌ Error:', err); } else { console.log('✅ Tables:', results); } }); export default con; This code ensures that the server can connect to the MySQL database hosted by Railway. 10. Push Updates to GitHub Commit and push the changes, including the .env file and the updated db.js file, to your GitHub repository. Railway will automatically detect the changes and deploy the updated code. 11. Check Deployment Logs Go back to the Deployments section on Railway to check the logs for any issues. If everything is set up correctly, your MySQL service should now be connected, and your server will interact with the database seamlessly. Conclusion You’ve successfully deployed your Node.js project with MySQL on Railway.

1. Create a Railway Account & Log In via GitHub
- Go to the Railway website.
- Create an account or log in using your GitHub credentials.
2. Deploy a New Project
- After logging in, click on “Deploy a New Project”.
- Choose MySQL as the first service you want to deploy. Select the MySQL image.
- Once the MySQL container starts, you’ll see an option at the top-right corner of the page to configure your server.
3. Configure the Server with GitHub Repository
- Railway will ask you “What do you need?”
- Select GitHub repo to link your project from GitHub.
- Click on Configure GitHub App, and it will show you your repositories.
- Choose the repo that you want to deploy.
Wait for 2 minutes for Railway to configure your project. After the configuration is done, you will be able to see your repo name in the server image list. Click on it to continue.
4. Deployment and Configuration
-
After clicking the repo name, Railway will open a window with different menus:
Deployments - Variables - Metrics - Settings
Under Deployments, you’ll see logs showing the deployment status and success/failure messages.
5. Set Up MySQL Variables
- Under the Variables section, add the following environment variables for MySQL:
MYSQLDATABASE
MYSQLHOST
MYSQLPASSWORD
MYSQLPORT
MYSQLUSER
- Make sure these variables match the ones generated by Railway for your MySQL service.
Note: After adding the variables, Railway will automatically connect to MySQL. If it needs to create tables or connect, the server code will handle it.
6. Fixing Common Errors
- Root Directory Issue: If you’re working with multiple projects (e.g., client and server), Railway may prompt you to specify the correct root directory for your server. If you have two directories, ensure that the server directory is set as the root directory in the settings.
7. Get Your Custom Domain
In the Settings tab, you’ll see an option for Networking.
Click on Generate Domain to get your custom domain name for the deployed service.
8. Server Code Setup: Create .env
File
- In your server project, create a
.env
file in the root directory and add the following environment variables (make sure to replace the values with your own generated ones):
# Server Configuration this for loacl
PORT=5000
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=Sharan@123
DB_DATABASE=people_pro
# Railway MySQL config
MYSQLHOST=tramway.proxy.rlwy.net
MYSQLPORT=17091
MYSQLUSER=root
MYSQLPASSWORD=RcMTiimLAURmUcweLMDtrgDwGvmZdFZw
MYSQLDATABASE=railway
9. Update db.js
(Database Connection)
- Update your database connection code (
db.js
) with the correct environment variables, and make sure it's configured to connect to the Railway MySQL service:
// This code for local
import mysql from "mysql2"
import dotenv from 'dotenv';
dotenv.config();
const con = mysql.createConnection({
host:process.env.DB_HOST,
user:process.env.DB_USER,
password:process.env.DB_PASSWORD,
database:process.env.DB_DATABASE
})
con.connect(function(error){
if(error){
return console.log("Connection error");
}else{
return console.log("Coonected");
}
})
export default con;
// This code for server
import mysql from "mysql2";
import dotenv from "dotenv";
dotenv.config();
const con = mysql.createConnection({
host: process.env.MYSQLHOST,
port: process.env.MYSQLPORT,
user: process.env.MYSQLUSER,
password: process.env.MYSQLPASSWORD,
database: process.env.MYSQLDATABASE,
});
con.connect((error) => {
if (error) {
console.log("❌ Connection error:", error);
} else {
console.log("✅ Connected to the MySQL database!");
}
});
con.query('SHOW TABLES', (err, results) => {
if (err) {
console.log('❌ Error:', err);
} else {
console.log('✅ Tables:', results);
}
});
export default con;
This code ensures that the server can connect to the MySQL database hosted by Railway.
10. Push Updates to GitHub
Commit and push the changes, including the
.env
file and the updateddb.js
file, to your GitHub repository.Railway will automatically detect the changes and deploy the updated code.
11. Check Deployment Logs
Go back to the Deployments section on Railway to check the logs for any issues.
If everything is set up correctly, your MySQL service should now be connected, and your server will interact with the database seamlessly.
Conclusion
You’ve successfully deployed your Node.js project with MySQL on Railway.