MySQL 2 masters N-slaves replication - mysql

I'm trying to set up a two masters, N-slaves replication in MySQL
I have setup two masters that works perfectly fine with each other.
It breaks when I add a slave for master#1.
Creating
master#1 : Create database test.
master#2 : test database gets created from master#1's query.
creates a test database in slave#1
Deleting
master#2 : Delete database test.
master#1 : test database gets deleted from master#2's query.
test doesn't get removed in slave#1
What I did was to create a user in **master#
create user 'root'#'slave.one.ip' identified by 'slaveonepass';
Give it replication privileges:
grant replication slave on dbname.* to 'root'#'slave.one.ip';
Get the info of master#1:
show master status;
Add the info from above to slave#1
mysql> change master to\
master_host='first.master.ip',\
master_user='root',\
master_password='slaveonepass',\
master_log_file='mysql-bin.123456',\
master_log_pos=123456;`
Reference:
How To Set Up MySQL Master-Master Replication

This would be the expected behavior if you have not set LOG_SLAVE_UPDATES=ON on both masters. Set this in the configuration files, and restart the masters.
Normally, a slave does not write to its own binary log any updates that are received from a master server. This option causes the slave to write the updates performed by its SQL thread to its own binary log
https://dev.mysql.com/doc/refman/5.6/en/replication-options-binary-log.html#sysvar_log_slave_updates
This option is mandatory for all cascading replication configurations, where an instance needs to propagate incoming replication events out to additional replicas.
You should also enable this in any slave that will have downstream slaves connected.

Related

Slave isn't writing any data into the table in mysql master slave replication

I am having a difficult time in setting Master-Slave configuration.
Master Database runs on Ubuntu( Amazon AWS instance) and successfully set-up master replication.
I have localhost as a Slave Server. (Windows Machine).
Snapshot of Master Database
Master database has record
Binar Log Information
Process List on Master Replication
Status of Master Replication
I debug master replication which works okay I guess.
On the Salve Side:
Status on Slave Side
Even though MASTER_LOG and MASTER_POS are synced but data doesn't.
Currently, I have 0 table on Slave side and 34 tables on Master side.
Tables on Slave side
I am open to any suggestion or any reference do you have.
I spend an entire day and trying to find what I did wrong.
I want to Sync my Local database with a database hosted on remote-server.
Update: Thigs I did to debug the Master-Slave Replication
Checked Master Database is up and running.
Master Status and Connected Slaves. [Which includes unique id for
each server.]
Slave database is up and running [Including Slave IO Thread and
SQL thread is running.]
These three steps ensure that Master-Slave replication is up and running without any problem.
Handling Data Sync Problem
Created/update/delete data in the master database to check
whether data is sync on a server or not.
Checked Binary Log [Specifically I checked the file size. If I
entered data file size will continuously increasing.]
Thanks in advance.
we had similar problem - read more about gotchas in "binlog-do-db" and "replication-do-db" and related parameters. Here is a big problem with crossdatabase references. At the and we had to remove these settings limiting replication.
Why MySQL’s binlog-do-db option is dangerous
Gotchas in MySQL replication
As your show slave status output says you enabled Replicate_DO_DB for the DB "Arihantpos" at the same time you did Binglog_Do_Db for the same db
try to remove Binglog_Do_Db from config file and restart mysql and start replication again

Replication Issues for client machines connecting to Master in mysql

I have done replication set up between 2 database in mysql
One is master and second is slave.
Now whenever I do any change on master database on same machine it works fine and data is getting replicated to slave .
Now when I connect to master database from different machine using tools like Toad , workbench and perform any action (insert , update etc) this is not getting replicated to slave .
Does mysql support replication this scenario.
IF yes please share the steps ?
Thanks.
change data through gui tool todd etc in below manner-
first select database by use for an example-
use mydb;
update mytable set mycol='value' where mycol2='value';
Now check if data is replicating..
Also execute below command on slave and provide results if not replicating-
show slave status;

Replicating Different Databases to Different Slaves

I need to replicate some databases as shown below.
-DB1---replicate to---> SLAVE_A
/
MASTER <--DB2---replicate to---> SLAVE_B
\
-DB3---replicate to---> SLAVE_C
The process described here would work well if I controlled both the master and all slave servers, but unfortunately I only control the master in this case. The slaves will potentially be in different countries, run by other admins.
My concern is that the configuration on SLAVE_A could easily be changed to replicate not only the intended DB1, but DB2 and DB3 as well, which is not good.
The only hope I have of limiting which slave replicates which database is if I can somehow control it from the master, which, from what I've been able to figure out, can't be done. A user has replication privileges either to all databases or none, which is unfortunate.
Am I missing something here, or is this just a limitation in MySQL?
Any thoughts on how this could be accomplished?
Thanks.
On the slave server there is possibility to filter transactions to apply by replicate-do-db or replicate-ignore-db parameters, included in my.cnf file or in command line as option.
The same can be done on master with binlog-do-db or binlog-ignore-db but then - it limits replication do some certain databases on the master. So for you better solution is to filter transactions on the slave.
Create a different MySQL user for each replication slave, and give that user access only to the database you want it to have replication access to.
You can read more in the MySQL Documentation: 16.1.1.3. Creating a User for Replication
CREATE USER 'slave_a'#'some_slave_server' IDENTIFIED BY 'slavepass';
GRANT REPLICATION SLAVE ON `DB1`.* TO 'slave_a'#'some_slave_server';
FLUSH PRIVILEGES;
And then repeat for Slaves B and C on DB 2 and 3 respectively.

MySQL Master - Master (Select, Insert, Update, Delete for both)

I'm looking for a way to have 2 databases running; 1 at the office and 1 at a data center. When at the office, employees would connect locally but when outside the office they would connect remotely to the data center. Both databases would be fully synchronized. That means that an employee could log in and update a record on the data center and that change would replicate instantly to the office server (or vice-versa). So, either user could edit the same record.
So, the typical scenario of auto_increment_offset etc. won't work because, in this case, each server has to be able to update the same record.
Am I missing something obvious? I can't think of a viable way to handle this. 2 users on 1 db can modify the same records so there HAS to be a way to do the same in this type of setup. I just can't think of one.
You can not do this reliably with asynchronous replication. Consider this simple example:
site A: update foo set bar = 1
site B: update foo set bar = 2
Some time later (milliseconds or hours depending) the transactions are replicated to each site:
site A: update foo set bar = 2
site B: update foo set bar = 1
foo.bar will now be 2 on site A and 1 on site B. There is nothing in MySQL that will detect this or prevent it from happening.
Either partition the problem so the masters "own" different parts of the data or elect a primary master for all updates. Or implement some global locking mechanism.
With one database things are completely different since there is synchronous locking going on internally.
Here's how we did it:
In master-master configuration there are 2 masters and 2 slaves. Both are masters and slaves at the same time:
master1 => slave1[master2]
[slave1]master1 <= slave2
enable binlogging on master server - my.cnf file under [mysqld] section:
log-bin=mysql-bin.log
relay_log=relay-bin.log
server-id = 1
log-slave-updates
auto_increment_increment = 10
auto_increment_offset = 1
You may need to add:
skip-slave-start option too, to prevent from start slave at database startup.
Note:
Each server should use different server-id
auto_increment_offset should differs
Auto increment options:
auto_increment_increment controls the increment between successive AUTO_INCREMENT values.
auto_increment_offset determines the starting point for AUTO_INCREMENT column values.
With auto_increment_increment = 10 and auto_increment_offset=5, auto_inc values would be 5, 15, 25..
Dedicated user:
GRANT REPLICATION SLAVE ON . TO 'replication'#'%' IDENTIFIED BY 'some_password';
Create consistent, binary backup of MySQL database (needed to setup a replication) using LVM but the same steps will apply to ZFS/UFS snapshot and other popular techniques:
connect to MySQL database on master (server1) and run "FLUSH TABLES WITH READ LOCK". It might take few second to complete flush operation and it will lock all tables! It is imporant to keep it in mind, because such operation may impact in production load in some cases. It's better to schedule replication setup to peak-off hours.
Create LVM snapshot (server1): lvcreate -L10G -s -n mysqlbackup /dev/vg/lvm_mysql_partition
Get information about master position. Using previously created connection to database run : SHOW MASTER STATUS; to get all information about current binlog, position and so on.
Copy-paste output somewhere - for future usage.
Uunlock all tables: UNLOCK TABLES;
Mount previously created filesystem: mount /dev/vg/mysqlbackup (this will allow you to access shapshot of data created).
Now you can just copy this data to the second server directly to data dir. Before you start up your database add previously mentioned parameters to my.cnf file, changing value of server-id.
Now, with master information copied somewhere and database files propagated to slave you can remove snapshot: lvremove -f /dev/vg/mysqlbackup on master (server1).
Now, you can log into server2 to check for permissions (depends on which user was used to copy files you'll need to correct it) and after that, startup MySQL instance. Database should start, perform InnoDB recovery (if you're signed it) and after a bit, you will be able to log into using command line client. Because of skip-slave-start your slave will not start by default.
Now, on both servers you need to configure replication thread by setting up master hostname, master port, password, user and info about position:
CHANGE MASTER TO master_host='IP_addr_of_server1', master_port=3306, master_user='replication', master_password='some_password', master_log_file='info_from_MASTER_STATUS', master_log_pos='info_from_MASTER_STATUS';
Now you can start replication: START SLAVE;
Check to make sure: SHOW SLAVE STATUS\G (on new machine)
Seconds_Behind_Master might not be equal to 0 because of changes done to database after you release all tables but with replication working slave should catch master pretty fast.
After your slave server catches up with master, start master-master (server1-server2) setup. Lock all tables on server2 ( FLUSH TABLES WITH READ LOCK;) and run SHOW MASTER STATUS;. On server1 set-up replication by CHANGE MASTER TO..
of course you need to change master_host to valid IP address, master_log_file and master_log_pos values. After that, UNLOCK TABLES on server2 and START SLAVE on server1.
Now you should have master-master-slave configuration with master-master replication between server1 and server2.

Add a table to an existing mysql replication?

I have an existing mysql replication set up (Windows 2008 to Ubuntu 9.04) and created several new tables in the master database. These are not showing up in the slave database.
Do new tables automatically get copied to the slave DB, or do I need to set up replication again?
Thanks!
I'm going to assume that other data is successfully replicating.
Replication in mysql is per-server, so the most likely problems are that either you aren't binloging the events, or that the slave is ignoring them.
For binglogs, verify you aren't turning sql_log_bin off for the connection (which would require SUPER) and that the various options binary-log options are set correctly. You can verify this by running mysqlbinlog on the server's binlogs.
On the slave side, check the replication options.