Top Banner
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 1
18

MySQL Replication: Demo Réplica en Español

Jan 15, 2015

Download

Software

Keith Hollman

MySQL Replication technical example in Spanish.
Ejemplo técnico de réplica de MySQL en Español.

Es una guía muy rápida para quitar el miedo a empezar a jugar con réplica.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MySQL Replication: Demo Réplica en Español

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 1

Page 2: MySQL Replication: Demo Réplica en Español

Demo: Replica

Keith Hollman

MySQL Principal Sales Consultant

Page 3: MySQL Replication: Demo Réplica en Español

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.

Page 4: MySQL Replication: Demo Réplica en Español

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

Page 5: MySQL Replication: Demo Réplica en Español

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

Page 6: MySQL Replication: Demo Réplica en Español

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

Page 7: MySQL Replication: Demo Réplica en Español

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');

Page 8: MySQL Replication: Demo Réplica en Español

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

Page 9: MySQL Replication: Demo Réplica en Español

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.

Page 10: MySQL Replication: Demo Réplica en Español

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

Page 11: MySQL Replication: Demo Réplica en Español

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

Page 12: MySQL Replication: Demo Réplica en Español

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

Page 13: MySQL Replication: Demo Réplica en Español

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

Page 14: MySQL Replication: Demo Réplica en Español

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>\_

Page 15: MySQL Replication: Demo Réplica en Español

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/

Page 16: MySQL Replication: Demo Réplica en Español

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 16

Questions?

Page 17: MySQL Replication: Demo Réplica en Español

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 17

Page 18: MySQL Replication: Demo Réplica en Español

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 18