You are on page 1of 8

Mysql Replication

How it works

Mysql have 2 tipes of replication: master-slave
and master-master and it works doing a copy of
database in other server.

The main different bettween both is that in
mysql master-slave replication only the change
that do the master server will be made in the
slave server
Mysql master-slave

If we want to do a mysql master-slave we must
make the follow steps:

First step: the main is have installed mysql in
both server, we can do it with next commant
apt-get install mysql-server mysql-client
Mysql master-slave

Secont step: In the server 1 we need to do a change in the configuration file of
mysql. This can be on /etc/mysql/my.cnf, or also can use
/etc/mysql/mysql.conf.d/mysqld.cnf.

Open file and do the next changes:
Put a server Id on 1 and erase the #.
Erase the # of log_bin and binlog_dp_db and add the database that you need to do a
replication.
And comment the line bind-address with # because we need that mysql accept
conection not only of localhost
server-id =1
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = exampledb
# bind-address = 127.0.0.1
Mysql master-slave

Third step: Log in mysql console and make an user with replication
privileges.
CREATE USER 'replicator'@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%';
FLUSH PRIVILEGES;

And now we need some information about the database. In mysql


console we write the next commant.
show master status;
and of the output write down the file and position because it will need
more ahead.
Mysql master-slave

Fourth step: In server 2 open the configuration
file (my.cnf), add the line 'server-id = 2' and the
# where start the line 'bind-address = 127.0.0.1'
because we need that server accept the
conection to the master server.
server-id = 2
#bind-address = 127.0.0.1
Mysql master-slave

Fifth step: log in mysql console and run the next commant
slave stop;
CHANGE MASTER TO MASTER_HOST = '52.89.254.78',
MASTER_USER = 'replicator', MASTER_PASSWORD = 'repli',
MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS =
416;
slave start;
where MASTER_HOST is the server 1, replicator is the user created in
server 1, MASTER_LOG_FILE is the file that we have how output with
SHOW_MASTER_STATUS commant in the server 1, and
MASTER_LOG_POS is the position obtained with
SHOW_MASTER_STATUS.
Mysql master-slave

Last step: Test mysql master-slave.
Go to master server (server 1 in this case) and in
the mysql console do a new table with the next
commant.
create table exampledb.tabletest (`id`
varchar(10));
and check if tabletest was created in slave server
(server 2).

You might also like