skip to Main Content

How can I convert my MySQL database to a Firebase database without rewriting the database? Is there a way of exporting to Firestore or Realtime database and I just change a few things in my PHP code?

I am still confused how I can have my app work remotely using free Firebase hosting

2

Answers


  1. If you’re looking for a magic button that converts your MySQL database to Firestore or the Realtime Database, please note that there isn’t one. So, you’ll have to write code for the conversion yourself.

    Login or Signup to reply.
  2. A quick primer for you:

    1. Convert MySQL to Firebase:
    • Export your MySQL database to JSON.
    • Use Firebase Admin SDK to upload the JSON data.
    const admin = require("firebase-admin");
    const serviceAccount = require("path/to/serviceAccountKey.json");
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://your-database-url.firebaseio.com"
    });
    
    let data = require('./yourExportedMySQLdata.json');
    admin.database().ref().set(data);
    
    1. Update PHP for Firebase:
    • Replace MySQL queries with Firebase API calls using Firebase PHP SDK.
    require 'vendor/autoload.php';
    
    use KreaitFirebaseFactory;
    
    $factory = (new Factory)->withServiceAccount('path/to/serviceAccountKey.json');
    $database = $factory->createDatabase();
    
    $newPost = $database->getReference('path/in/database')->push([
        'title' => 'Post title',
        'body' => 'This is the post body'
    ]);
    
    1. Firebase Hosting:
    • Deploy your app on Firebase hosting.
    firebase init hosting
    firebase deploy
    

    Note: Make sure you’ve replaced database operations in your PHP app with Firebase SDK calls before deploying.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search