Search code examples
mysqlsqlreplicationdatabase-replicationmaster-slave

MySQL Replication with Different Table Name & only specific column


I have below queries regarding MySQL replication.

1) Suppose i want to do master-slave replication with different table name then is it possible ?

Ex. Master >> Db1.student i want to replicate with Slave >> Db2.employee table.

2) If i want to replicate only specific columns in same table then is it possible ?

Master >> Db1.student

  • id
  • firstname
  • lastname
  • parentname
  • status

Slave >> Db2.student

  • id
  • firstname
  • lastname

Apart from that is there any other way to achieve/solution above query (i.e MySQL Views) or any other alternate way ?

Thanks in advance.


Solution

  • The eassiest way is to use a View like this:

    CREATE VIEW Db2.student AS 
         SELECT id, firstname, lastname 
           FROM Db1.student;
    

    If you want to filter some content in the Db2 view you could add some filters to the view creation. For example:

    CREATE VIEW Db2.student AS 
         SELECT id, firstname, lastname 
           FROM Db1.student
          WHERE Db1.student.status = 1; 
    

    I hope it works for you.