MySQL Replicate Different Databases from DIfferent Masters - mysql

I'm relatively new to MySQL replication. In a nutshell I have a MySQL 5.1 server instance on 3 Ubuntu Lucid Lynx servers.
ATM I have Server A (MASTER) which replicates a single database to Server C (SLAVE).
This has been running for a couple months without a problem.
I now want to have Server B (as MASTER) replicate a different single database to Server C (SLAVE).
I was looking into implementing this, but my initial reading seems to indicate that a replication slave server can't have two masters.
My question is, is this the case even when only single or select database are being replicated?
Keep in mind I do not wish to replicate the same database from different masters. I simply wish to replicate multiple separate database on a single server, from separate masters.

You can set a different master per database in MySQL.
The book high performance MySQL has a full chapter on this issue. I recommend getting hold of a copy and using the info in that.
http://oreilly.com/catalog/9780596101718/
You can read a copy of the relevant chapter online here:
http://oreilly.com/catalog/hpmysql/chapter/ch07.pdf
This is from the first edition, the second edition is more up to date, but chapter 8, which deals with replication is not freely available online.
UPDATE
The solution I'm talking about is only in the 2nd edition of the book, see this answer to a simular question: Is it possible to do N-master => 1-slave replication with MySQL?

In general you can't do this. You can't replicate from many-to-one (you can from one-to-many).
Also you can't really just replicate one database - cross-database updates in statement-based mode then become unreplicatable, which means your slave will fail (or become out of sync) as soon as someone does one on the master.
The standard solution is to install multiple mysql instances, which is far from ideal, but works.

Related

How to connect two different Mysql servers from one stored procedure in Mysql?

I have two Mysql databases on different servers.
I need to update the Server 2 database from Server 1, but both database tables structure is different. I have to create a stored procedure to connect both servers so I can make changes in the database.
I am not a MySQL expert, so I can't tell you exact specifics. I looked through guides and other questions to see how it can be done and it appears that exact same principle I used in other databases applies to MySQL.
You need to Setup a remote database connection. Usually in doing so you will specify a name for that database. This is the only tricky thing in your question because of few reasons. One of them being not all platforms, that the database is installed onto, seem to support external connections and the other is that you need to set the 2nd database up for remote connections. I found this guide/article explaining how to set it up, but you might need to look for another one that fits your setup.
When forming your query specify database name (that you specified in previous step) before table name. The rest of the query structure is the same, but bare in mind that large/complex joins across 2 servers might be slow without manual optimization. This answer shows what your queries should look like when you setup a remote database connection.

Query data from database for 2 different server

I want to query data from 2 different database server using mysql. Is there a way to do that without having to create Federated database as Google Cloud Platform does not support Federated Engine.
Thanks!
In addition to #MontyPython's excellent response, there is a third, albeit a bit cumbersome, way to do this if by any chance you cannot use Federated Engine and you also cannot manage your databases replication.
Use an ETL tool to do the work
Back in the day, I faced a very similar problem: I had to join data from two separate database servers, neither of which I had any administrative access to. I ended up setting up Pentaho's ETL suite of tools to Extract data from both databases, Transform if (basically having Pentaho do a lot of work with both datasets) and Loading it on my very own local database engine where I ended up with exactly the merged and processed data I needed.
Be advised, this IS a lot of work (you have to "teach" your ETL tool what you need and depending on what tool you use, it may involve quite some coding) but once you're done, you can schedule the work to happen automatically at regular intervals so you always have your local processed/merged data readily accesible.
FWIW, I used Pentaho's community edition so free as in beer
You can achieve this in two ways, one you have already mentioned:
1. Use Federated Engine
You can see how it is done here - Join tables from two different server. This is a MySQL specific answer.
2. Set up Multi-source Replication on another server and query that server
You can easily set up Multi-source Replication using Replication channels
Check out their official documentation here - https://dev.mysql.com/doc/refman/8.0/en/replication-multi-source-tutorials.html
If you have an older version of MySQL where Replication channels are not available, you may use one of the many third-party replicators like Tungsten Replicator.
P.S. - There is no such thing in MySQL as a FDW in PostgreSQL. Joins across servers are easily possible in other database management systems but not in MySQL.

MySQL single DB accessed by two servers

I am trying to build a website that uses MySQL DB. What I am trying to do is make my database accessed by two servers, which means when server 1 is down server 2 can access the same database and the website continues working normally. I've read about multimaster replication but it does not seem to be what I need. And what happens when using a master slave replication and the master server goes down ? How it can be restored ?
Thanks for your help.
I think the master slave pattern is exactly what you're looking for. The master handles all the writes and the slaves handle all the reads. If your cloud hosting with someone like Rackspace or AWS they make it very easy to set up the data replication across each mode. As for your last sub question about what happens if the master goes down, I believe it is pretty straight forward to set up fallbacks for that too. There are likely several approaches but at the most basic level I know you can set up multiple db nodes (with a fallback algorithm) just like any other instance.
A final note... If its your first time doing this I highly recommend Rackspace because their support is amazing and they make a huge effort when you start to explain all your option and help you pick the best strategy.
Ps: retreading your question, it's a little unclear what you're trying to accomplish. You mention two servers accessing one DB and you also talk about redundant setups for multiple db instances. They're really two separate issues. The former is trivially easy because you can always just point more than one server to a db. As long as the credentials are right it will work. But the tricky part is keeping the data synched properly. If both are reading and writing the same tables things are going to bang together. That's where the master slave pattern comes into play. All the writes go through the master but anyone can read from any slave because the data gets replicated.

Any way to synchronize 2 mysql databases?

Two machines, each running mysql, each synchronized to the other peer-to-peer. I do not want a master db replicated. Rather, I want two users to be able to work on the data offline (each running a mysql server on his machine) and then when reconnected synchronize to each other. Any way to do this with mysql? Any other database I should be looking at to accomplish this better than mysql?
Two-way replication is provided by various database systems (e.g. SQLServer, Sybase etc.) but there are always problems with such a set up.
For example, if the same row is updated at the same time on the two databases, which update wins?
If your aim is to provide a highly-available MySQL database, then there are better options than using replication. MySQL has a clustering solution (though I've not had much success with it) or you can use things like DRBD and heartbeat to provide automatic failover with no loss of data.
If you mean synchronous writing back and forth, this would cause serious data consistency issues. I think you may be referring to MySQL replication, wherein a master server sends its updates to one or more slave database servers, which can be queried.
As for "Other Database Options" SQLServer supports a fairly advanced "replication" process for synchronizing the data between two or more db's. Looks like MySql has something like this as well though.

Strategy on synchronizing database from multiple locations to a central database and vice versa

I have several databases located in different locations and a central database which is in a data center. All have the same schema. All of them are changed(insert/update/delete) in each location with different data including the central database.
I would like to synchronise all the data in the central database. I would also like all data in the central database synchronise to all locations. What I mean is that database change in location 1 should also be reflected in location 2 database.
Any ideas on how to go about this?
Just look at SymmetricDS. It is a data replication software that supports multiple subscribers and bi-directional synchronization.
You will have to implement a two-way replication scheme between the databases. Every new record created should have a unique identifier (eg. a GUID), so that data from the different databases does not conflict. (See the mysql replication howto).
MySql only supports one-way replication, so you will need to set up each database as a master, and make each database a slave of all the other database instances. Good luck with that.
I went to SymmetricDS
I think it is the top quality also just to mention I found in sourceforge (php mysql sync)
and found many links on the internet.
Unfortunately, MySQL replication capabilities won't allow you to do exactly what you want.
Usually, to synchronize two servers the master-master replication scheme can be used. See http://www.howtoforge.com/mysql_master_master_replication
The problem is that each MySQL server can have ONLY ONE master.
The only way I know to keep several servers synchronized, would be a circular replication (see http://onlamp.com/pub/a/onlamp/2006/04/20/advanced-mysql-replication.html?page=2), but won't exactly fit your needs ("star" configuration)
Maybe this configuration could be close enough : all the "distant" (non central) databases would be only be readable slaves (synchronized though a basic master-slave replication), and writings would only occur on the central server (which would be master).