Synchronize one table between MySQL database(s) - mysql

We have the requirement to synchronize the data in a table across multiple MySQL databases. One of the databases would be the source, and all others need to have the synchronized data for one of the tables.
We have multiple databases used in microservice architecture, and they all need to have a local copy of a specific table in their database, and not the entire database itself, hence read replica or multi-AZ configuration is not the solution.
Database: MySQL hosted on AWS RDS
Is there any managed service by AWS or another vendor that can be used to accomplish this? Or do we have to write a custom script to do that?

It's a simple MySQL replication. But you have to replicate the whole database. So create a MySQL database in AWS and enable Multi-AZ replication and activate the automatic snapshots.
The A-Z Replication is synchronous. When you use a "Read replica" it's asynchronous. So if you have very important data you should enable Multi-AZ replication.
https://aws.amazon.com/rds/details/multi-az/?nc1=h_ls

I think you have to either isolate the table in one database and replicate that database or write a custom script.
If I were writing a custom script I would look at the binlog functionality. Here are some helpful links:
https://dev.mysql.com/doc/refman/5.7/en/mysqlbinlog.html
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_LogAccess.Concepts.MySQL.html (at the bottom).

Related

Setting up MySQL (Master-Slave) replication with all ready configured databases/tables

I am trying to configure MySQL databases using the Master-Slave replication. Before I realized that I had to set up my environment using this replication, I already have 2 separate servers running their own MySQL DB. Each of these servers are configured the exact same. The MySQL DB are configured with hundreds of tables.
Is there a way that i can set up (Master-Slave) Replication using the configured DB's? Or will i have to start from scratch and configure the replication first and then load in all the DB tables?
You can delete all data from one of the servers. Remaining one with the data will be your Master. Then use mysqldump to backup all the data and insert it to the slave.
Take a look for the detailed instructions on the page below:
https://livecaller.io/blog/how-to-set-up-mysql-master-slave-replication/
If the data is exactly same in both the MySQL database then you can start master slave replication, but you need to be sure that the data is same. MySQL will not check that, and if there is some discrepancy in the primary key then it will throw error immediately after next DML statement.
To be on a safer side, drop the database from one server, and restore it using the MySQL dump of another server. This will give the surety that database is same on both the server.
Take the reference from the below link to establish replication between two MySQL servers.
https://www.digitalocean.com/community/tutorials/how-to-set-up-master-slave-replication-in-mysql

MySQL backup/restore with master/master replication

I didn't find useful information in documentation that is why I decided to ask.
My environment is as this: Two MySQL servers with Master <-> Mater replication (let's call it Node1 and Node2).
All applications (99%) connects to Node1, on Node2 only one application which is doing INSERTS in a separate DB.
So - changes are propagated to both nodes because of replication. Also because of replication I have relay-bin-logs and bin-logs (on both servers).
I take backup on Node2 using mysqldump
QUESTION:
in case of restore what I need to apply to DB?
only "relay-bin-logs"?
only "bin-logs"?
both?
What is the sequence?
(link to documentation is also appreciated)
Thanks!
P.S. In case of simple MySQL server (without replication) everything is pretty simple - restore from backup and after this apply bin-logs.

AWS RDS MySQL Cross-region replication

I need to create an RDS MySQL instance in us-east-1 and would like to replicate this data in another region (say eu-west-1). I know about read-replicas, and I will be using them also, but I wanted to have a back-up non-read-replica in another region.
Has anyone done this?
Is there code out there that does this replication?
I highly recommend switching from RDS MySQL to RDS Aurora, which is MySQL compatible. You won't have to change anything in your code, the database will still appear to be MySQL to any apps that connect to it. Among the many advantages Aurora has over MySQL is that it supports cross-region replication.
There are several ways to do this. I will cover two easy methods. RDS features are based upon engine (MySQL, Aurora, PostreSQL, etc.). Review as required.
1) Create a read-replica of your existing RDS instance in another region. Then promote that read-replica to master. Now you have a separate instance running in another region. You can then stop this instance to minimize billing (for up to seven days).
2) Create an RDS snapshot. Copy that snapshot to another region. When needed launch a new RDS instance from the snapshot.

Daily copy of AWS Aurora database to another AWS Aurora database

We have an AWS Aurora database sitting on an instance that holds all of our production data. I want to be able to perform analytics on that data without doing it in our production environment, so I want to copy the production data on a daily basis to another AWS Aurora database on a completely different instance. Within that "analytics" database, I'll build out all the needed views and stored procedures to aggregate whatever transformed data I need to store.
At first I thought of creating an Aurora replica, but of course that's read-only. I need to find a way to do this outside of the production environment and I feel it's an easy enough task to do, but I just can't find out how to do it. Maybe I haven't been able to ask the write questions, so I came here. How can I achieve this?
This is simple AWS replication.
http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Aurora.Replication.CrossRegion.html
Also if you prefer to use mysql or any other RDBMS use
http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Aurora.Overview.Replication.MySQLReplication.html
It is similar to master slave replication with little difference in sharded data mainted in Aurora.
Replication is the correct (subjective, of course) solution, but you can't use a managed Aurora replica, which is to say you can't use an Aurora replica in the cluster.
That does not, however, mean you can't create your own asynchronous Aurora replica... which would be a second Aurora cluster, an independent master that is writable, but that uses the replication stream (the binary logs, also called "binlogs,") from the master cluster to keep its data in sync.
The one caveat: you must be extremely cautious not to write to any of the tables on the asynchronous cluster that are being replicated from the production master. Do that, of course, and replication breaks. The master cluster will be completely unaffected, but the replica cluster will stop replicating once inconsistent data is detected. But you can create additonal tables, views, and stored programs without issue.
Within an Aurora cluster, there is no need for replication in the traditional sense -- the replicas use the same backing store as the master (the "cluster volume.") Here, we're just replicating from cluster to cluster, identical to the way two ordinary MySQL servers would replicate (in one direction, only, of course).
The setup is essentially identical to the setup for replicating in and out of Aurora, to or from MySQL. Since this solution uses MySQL native replication, the steps are the same.
http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Aurora.Overview.Replication.MySQLReplication.html

MySQL database replication

This is the scenario:
I have a MySQL server with a database, let's call it consolidateddb. This database a consolidation of several tables from various databases
I have another MySQL server, with the original databases, these databases are production databases and are updates daily.
The company wants to copy each update/insert/delete on each table in the production databases to the corresponding tables in consolidateddb.
Would replication accomplish that? I know that replication is done on a databas to database, but not on tables that belong to different databases to one target database.
I hope my explanation was clear. Thanks.
Edit: Would a recursive copy of all tables inn each database to the single slave work? Or is it an ugly solution?
To clear up some things, let's name things accordingly to current mysql practice. A database is a database server. A schema is a database instance. A database server can have multiple schemas. Tables live within a schema.
Replication will help you if you want to duplicate schemas or tables as they are defined on the master/production server. The replication works by shipping a binary log of all the sql statements that are run on the master to the slave which dutifully runs them as if they run sequentially on itself.
You can choose to replicate all data, or you can choose some of the schemas or even just some of the tables.
You can not choose tables from different schemas and have them replicated into one schema, a table belongs to a specific schema.
By the way, important notice. A replication server can not be a slave to multiple masters. You could mimic this using federated tables, but that would never copy the data to the consolidation server, just show them as if the data from different servers were on one server.
The bonus of replication is that your consolidation server will more or less have updated data all the time.
You could take the binary logs from each of the masters, parse them with mysqlbinlog and then run that into the consolidated machine.
Something very approximately like:
mysqlbinlog [binary log files] | mysql -h consolidated
you'd need some kind of simple application (I suspect it could be done in bash if you needed) to wrap the logic.
Check out Replicating Different Databases to Different Slaves, see if it helps you in any way.
MySQL statement-based replication (basic replication) works by running the exact same statements that were run on the master on the slave. This includes information about what database the table was in.
I don't think MySQL provides any built-in way to move replication statements between databases (i.e. "insert into db1.table1 ..." -> "insert into db2.table1"). You may be able to trick it by manually altering the replication logs on the fly, but it wouldn't be out-of-the-bod MySQL replication.
You might be able to pull it off with MySQL Proxy
You may want to check out the maatkit toolkit. It's a free download and has a host of tools that specialize in optimizing things like archiving tables. I've used it on past projects to duplicate certain data to another DB, etc. You can do it based on time or any other number of factors.
To the best of my knowledge you can set up replication (MySQL 4+) and in the my.cnf file have the slave either only process certain tables or have the master log only certain tables, either way will solve your problem.
Here is a guide to some techniques:
http://www.onlamp.com/pub/a/onlamp/2006/04/20/advanced-mysql-replication.html
I have very few problems with replication set-up, all my problems came trying to sync DBs, especially after a reboot etc.