Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 1
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 1
Demo: Replica
Keith Hollman
MySQL Principal Sales Consultant
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 3
El entorno de réplica
1x Master & 1x esclavo
Mismo host / servidor
Mismo instalación de software / binarios.
Puertos & rutas diferentes (3306 & 3307)
Réplica asíncrona.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 4
Master - esclavo
Creamos una instancia ‘master’: cd /usr/local/mysql
cp my.cnf my3306.cnf
Añadimos una parametrización
más ideal para réplica (my3306.cnf)
Preparación
server-id =6
log-slave-updates =TRUE
gtid-mode =ON
enforce-gtid-consistency =TRUE
master-info-repository =TABLE
relay-log-info-repository =TABLE
sync_binlog =1
sync_master_info =1
slave-parallel-workers =2
slave_transaction_retries =0
binlog-checksum =CRC32
master-verify-checksum =1
slave-sql-verify-checksum =1
binlog-rows-query-log-events =1
report-port =3306
log-bin =khollman_3306
binlog_format =ROW
report-host =khollman_es
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 5
Master - esclavo
Usando los valores específicos para este entorno, para asegurar que es único
y no sobrescribimos nada:
port = 3306
datadir = /opt/mysql/3306/data
socket = /tmp/mysql_3306.sock
server-id = 6
mkdir -p /opt/mysql/3306/data
alias mysql3306='/usr/local/mysql/bin/mysql -uroot -poracle -S
/tmp/mysql_3306.sock'
Preparación II
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 6
Master - esclavo
Instanciando los directorios: pwd = /usr/local/mysql
scripts/mysql_install_db --defaults-file=/usr/local/mysql/my3306.cnf \
--user=mysql --datadir=/opt/mysql/3306/data
Lo arrancamos: bin/mysqld_safe --defaults-file=/usr/local/mysql/my3306.cnf \
--user=mysql --datadir=/opt/mysql/3306/data &
Ahora, alguna buena práctica de seguridad: ./bin/mysqladmin -u root password 'oracle' -S /tmp/mysql_3306.sock
Creando la instancia
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 7
Master - esclavo
Creamos algún dato para replicar: create database nexus;
use nexus
source /home/mysql/cre_tab_replicant.sql;
source /home/mysql/insert_nexus.sql
select `First Name`, `Last Name`, `Replicant` from replicant;
Y el usuario que se conectará del esclavo al master para traerse los
datos: grant REPLICATION SLAVE on *.* to 'replicant'@'127.0.0.1' \
identified by 'pkdick' ;
Poblamos una base de datos.
create table replicant (
`Title` enum('Mr','Mrs','Miss','Ms','M.') not
null default 'M.',
`First name` varchar(40) not null default '',
`Middle name` varchar(40) not null default '',
`Last name` varchar(40) not null default '',
`Replicant` enum('Yes','No') not null default
'Yes'
) engine=InnoDB;
insert into `replicant ̀(`First name`, L̀ast
name`,`Replicant`)
VALUES
('Roy','Hauer','Yes'),
('Rutger','Batty','Yes'),
('Voight','Kampff','Yes'),
('Pris','Hannah','Yes'),
('Daryl','Stratton','Yes'),
('Rachael','Young','Yes'),
('Sean','Tyrell','Yes'),
('Rick','Ford','No'),
('Harrison','Deckard','Yes');
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 8
Master - esclavo
Sacamos una imagen consistente de los datos a replicar: FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
/usr/local/mysql/bin/mysqldump -uroot -poracle -S /tmp/mysql_3306.sock \
--set-gtid-purged=OFF --master-data=2 -B nexus > /home/mysql/nexus.sql
unlock tables;
Exportando los datos
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 9
Creando el esclavo
Es hora de crear el esclavo (3307). alias mysql3307='/usr/local/mysql/bin/mysql -uroot -poracle -S
/tmp/mysql_3307.sock‘
prompt slave: \R:\m \d>\_
cd /usr/local/mysql
cp my3306.cnf my3307.cnf
:1,$ s/3306/3307/g
Cambiamos: port, datadir, socket & server-id. mkdir -p /opt/mysql/3307/data
Preparación, otra vez.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 10
Creando el esclavo
Creamos la instancia: scripts/mysql_install_db --defaults-file=/usr/local/mysql/my3307.cnf \
--user=mysql --datadir=/opt/mysql/3307/data
Arrancamos el esclavo: bin/mysqld_safe --defaults-file=/usr/local/mysql/my3307.cnf \
--user=mysql --datadir=/opt/mysql/3307/data &
Ahora, alguna buena práctica de seguridad: ./bin/mysqladmin -u root password 'oracle' -S /tmp/mysql_3307.sock
Preparación, otra vez II
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 11
Creando el esclavo
Comprobamos la conexión remota, del futuro esclavo al master: bin/mysql -ureplicant -ppkdick -h127.0.0.1 -P3306 -e status
Insertar / cargar los datos previamente exportados: bin/mysql -uroot -poracle -h127.0.0.1 -P3307 < /home/mysql/nexus.sql
Comprobamos: bin/mysql -uroot -poracle -h127.0.0.1 -P3307 nexus
select `First Name`, `Last Name`, `Replicant` from replicant;
Preparación, otra vez III
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 12
Replicando
show slave status \G
Nada. Bien.
Le decimos al esclavo cual va a ser su master: change master to master_host='127.0.0.1', master_port=3306,
master_user='replicant', master_password='pkdick',
master_auto_position=1 ;
start slave;
Comprobamos: show slave status \G
Comenzamos a replicar
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 13
Replicando
Ver los errores, debido a las entradas en el bin-log. El "IO Thread" está
ok, pero el "SQL Thread" está parado. drop database nexus;
start slave sql_thread;
show slave status \G
En el esclavo, 3307, creamos el usuario de réplica, por si en el futuro
queremos hacer switchover / failover: grant REPLICATION SLAVE on *.* to 'replicant'@'127.0.0.1' identified by
'pkdick' ;
Comenzamos a replicar II
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 14
Replicando
Vamos a comprobar si funciona. En el master: bin/mysql -uroot -poracle -h127.0.0.1 -P3306 nexus
show slave hosts;
insert into replicant (`First Name`,`Last Name`,`Replicant`) values
('Ridley', 'Tyrell','No'), ('Eldon','Scott', 'No');
select `First Name`, `Last Name`, `Replicant` from replicant;
Ver el esclavo: bin/mysql -uroot -poracle -h127.0.0.1 -P3307 nexus
show slave status \G
use nexus; select `First Name`, `Last Name`, `Replicant` from replicant;
Comprobamos
prompt master: \R:\m \d>\_
prompt slave: \R:\m \d>\_
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 15
Referencias
MySQL Réplica tutorial:
http://www.mysql.com/why-mysql/white-papers/mysql-replication-tutorial/
Introducción a réplica
http://www.mysql.com/why-mysql/white-papers/mysql-replication-introduction/
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 16
Questions?
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 17
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 18