# โœ… Railway Configuration - Verified ## ๐ŸŽฏ Environment Variables di Railway Anda Railway Anda sudah menggunakan format yang **BENAR** dan **SUDAH DIDUKUNG** oleh backend: ```bash DB_HOST=mysql.railway.internal DB_NAME=railway DB_PASSWORD=jIRceppgKCeUdEAjeYtzLaiAdPbBhDPX DB_PORT=3306 DB_USER=root MQTT_PORT=1883 NODE_ENV=production WS_PORT=8883 ``` โœ… **Backend otomatis detect dan gunakan variables ini!** --- ## ๐Ÿ” Penjelasan Variables ### Database Variables (MySQL): | Variable | Value | Keterangan | |----------|-------|------------| | `DB_HOST` | `mysql.railway.internal` | Internal hostname Railway MySQL | | `DB_PORT` | `3306` | Port MySQL standard | | `DB_USER` | `root` | Username MySQL | | `DB_PASSWORD` | `jIRc...hDPX` | Password MySQL (disembunyikan) | | `DB_NAME` | `railway` | Nama database | ### Server Variables: | Variable | Value | Keterangan | |----------|-------|------------| | `NODE_ENV` | `production` | Environment mode | | `MQTT_PORT` | `1883` | Port MQTT broker (tidak digunakan di Railway) | | `WS_PORT` | `8883` | Port WebSocket MQTT (tidak digunakan di Railway) | --- ## ๐Ÿ“ Catatan Penting ### 1. **`mysql.railway.internal`** Ini adalah **internal hostname** Railway untuk MySQL. - โœ… Hanya bisa diakses dari dalam Railway network - โœ… Lebih cepat dan aman - โœ… Tidak perlu expose ke public ### 2. **MQTT_PORT dan WS_PORT** Railway **TIDAK SUPPORT** custom ports untuk MQTT broker. - โŒ Port 1883 dan 8883 tidak bisa digunakan di Railway - โœ… Solusi: Gunakan **HiveMQ Cloud** (public MQTT broker) - โœ… ESP32 connect ke `broker.hivemq.com` - โœ… Backend hanya handle REST API, tidak host MQTT broker ### 3. **Database Name: `railway`** Railway otomatis create database dengan nama `railway`. - โœ… Backend sudah support ini - โœ… Tidak perlu ganti nama database --- ## ๐Ÿš€ Cara Kerja Backend di Railway ### 1. **Backend Startup** ```javascript // Backend baca environment variables DB_HOST=mysql.railway.internal DB_PORT=3306 DB_USER=root DB_PASSWORD=jIRc... DB_NAME=railway // Backend connect ke MySQL ๐Ÿ“ฆ Using individual environment variables for connection ๐Ÿ”ง Database Config: { host: 'mysql.railway.internal', port: 3306, user: 'root', database: 'railway', password: '***' } โœ… Database connected successfully โœ… Database tables initialized ``` ### 2. **Backend Create Tables** Backend otomatis create 3 tables di database `railway`: - `sensor_data` - Data sensor dari ESP32 - `status_history` - Riwayat status - `control_commands` - Riwayat command ### 3. **Backend Ready** ``` ๐Ÿš€ REST API running on port 3000 ๐Ÿ“ก MQTT Broker: mqtt://localhost:1883 (DISABLED di Railway) ๐ŸŒ WebSocket MQTT: ws://localhost:8883 (DISABLED di Railway) โœ… Server is ready! ``` --- ## ๐Ÿ”„ Flow Komunikasi ### ESP32 โ†’ Backend (via MQTT Public Broker) ``` ESP32: โ”œโ”€โ”€ Connect ke broker.hivemq.com (PUBLIC) โ””โ”€โ”€ Publish data ke topic: novil/pengering/data Backend di Railway: โ”œโ”€โ”€ Connect ke broker.hivemq.com (PUBLIC) โ”œโ”€โ”€ Subscribe topic: novil/pengering/data โ”œโ”€โ”€ Terima data dari ESP32 โ””โ”€โ”€ Simpan ke MySQL (mysql.railway.internal) ``` ### Flutter โ†’ Backend (via REST API) ``` Flutter: โ””โ”€โ”€ POST https://web-production-47eb.up.railway.app/api/control Backend di Railway: โ”œโ”€โ”€ Terima REST API request โ”œโ”€โ”€ Simpan command ke MySQL โ””โ”€โ”€ Publish command ke broker.hivemq.com ESP32: โ””โ”€โ”€ Terima command dari broker.hivemq.com ``` --- ## ๐Ÿงช Test Connection ### 1. Test Database Connection ```bash curl https://web-production-47eb.up.railway.app/api/database/test ``` **Expected Response:** ```json { "success": true, "message": "Database connection successful" } ``` ### 2. Initialize Database Tables ```bash curl -X POST https://web-production-47eb.up.railway.app/api/database/init ``` **Expected Response:** ```json { "success": true, "message": "Database tables initialized successfully" } ``` ### 3. Check Server Status ```bash curl https://web-production-47eb.up.railway.app/ ``` **Expected Response:** ```json { "status": "OK", "message": "Pengering Ikan Backend Server", "version": "1.0.0", "uptime": 123.45, "timestamp": "2026-05-28T10:30:00.000Z" } ``` --- ## ๐Ÿ› ๏ธ Troubleshooting ### Error: "Database connection failed" **Cek Railway Logs:** ```bash # Di Railway Dashboard: # 1. Go to your service # 2. Click "Deployments" # 3. Click latest deployment # 4. View logs ``` **Cari log:** ``` ๐Ÿ“ฆ Using individual environment variables for connection ๐Ÿ”ง Database Config: { ... } โŒ Database connection failed: ... ``` **Solusi:** 1. Pastikan MySQL service running di Railway 2. Pastikan environment variables benar 3. Pastikan `mysql.railway.internal` bisa diakses --- ### Error: "Access denied for user 'root'" **Solusi:** 1. Cek `DB_PASSWORD` benar 2. Cek `DB_USER` benar 3. Restart MySQL service di Railway --- ### Error: "Unknown database 'railway'" **Solusi:** 1. Database belum dibuat 2. Connect ke MySQL via Railway CLI: ```bash railway connect mysql ``` 3. Create database: ```sql CREATE DATABASE railway; ``` --- ## โœ… Checklist Setup - [x] Environment variables sudah set di Railway - [x] Backend code sudah support `DB_*` variables - [ ] Deploy backend ke Railway - [ ] Test `/api/database/test` - [ ] Initialize tables via `/api/database/init` - [ ] Test insert data via ESP32 - [ ] Test query data via `/api/data/latest` --- ## ๐ŸŽฏ Next Steps ### 1. Deploy Backend ```bash cd backend git add . git commit -m "Update database config untuk Railway" git push ``` ### 2. Wait for Railway Deploy Railway akan auto-deploy (2-3 menit). ### 3. Test Endpoints ```bash # Health check curl https://web-production-47eb.up.railway.app/ # Test database curl https://web-production-47eb.up.railway.app/api/database/test # Initialize tables curl -X POST https://web-production-47eb.up.railway.app/api/database/init ``` ### 4. Check Logs Di Railway Dashboard, cek logs untuk: ``` โœ… Database connected successfully โœ… Database tables initialized โœ… Server is ready! ``` --- ## ๐ŸŽ‰ Done! Konfigurasi Railway Anda sudah **BENAR** dan **SIAP DIGUNAKAN**! Backend akan otomatis: - โœ… Connect ke MySQL di `mysql.railway.internal` - โœ… Create tables di database `railway` - โœ… Handle REST API requests - โœ… Connect ke MQTT broker public (HiveMQ) Tinggal deploy dan test! ๐Ÿš€