Tablas federadas en MySQL
Transcription
Tablas federadas en MySQL
Tablas federadas en MySQL Desde Windows y CentOS 1. Primera DB en Windows tbnaucalpan Crear la DB local mysql> use tbnaucalpan mysql> show tables; +-----------------------+ | Tables_in_tbnaucalpan | +-----------------------+ | cliente | | transferencia | +-----------------------+ mysql> describe cliente; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | No | int(11) | NO | PRI | NULL | | | nombre | varchar(40) | YES | | NULL | | | saldo | double | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ mysql> describe transferencia; +---------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+---------+------+-----+---------+-------+ | folio | int(11) | NO | PRI | NULL | | | c1 | int(11) | YES | MUL | NULL | | | c2 | int(11) | YES | MUL | NULL | | | importe | double | YES | | NULL | | +---------+---------+------+-----+---------+-------+ Crear la DB local mysql> select * from cliente; +----+---------+-------+ | No | nombre | saldo | +----+---------+-------+ | 4 | Lupita | 10000 | | 5 | Juanito | 10 | | 6 | Josue | 10000 | +----+---------+-------+ mysql> select * from transferencia; +-------+------+------+---------+ | folio | c1 | c2 | importe | +-------+------+------+---------+ | 1 | 4 | 5 | 1000 | +-------+------+------+---------+ Consultar las dos tablas locales mysql> select cliente1.nombre, cliente2.nombre, -> importe -> from cliente as cliente1, cliente as cliente2, -> transferencia -> where cliente1.No = c1 -> and cliente2.No = c2; +--------+---------+---------+ | nombre | nombre | importe | +--------+---------+---------+ | Lupita | Juanito | 1000 | +--------+---------+---------+ 2. Habilitar el engine federated. Verificar el engine federated En Windows, en la consola de mysql: mysql> show engines; +--------------------+---------+---------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+---------------------------------------------------------------+--------------+------+------------+ | FEDERATED | NO | Federated MySQL storage engine | NO | NO | NO | Habilitar el engine federated Editar el archivo mi.ini Habilitar el engine federated Editar el archivo mi.ini # The MySQL server [mysqld] federated port= 3306 … # skip-networking # skip-federated Reiniciar mysqld Verificar el engine federated En Windows, en la consola de mysql: mysql> show engines; +--------------------+---------+---------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+---------------------------------------------------------------+--------------+------+------------+ | FEDERATED | YES | Federated MySQL storage engine | NO | NO | NO | 3. Segunda DB en Windows. tbnaucalpan_r Crear la segunda DB y una tabla de clientes mysql> create database tbnaucalpan_r; mysql> use tbnaucalpan_r; mysql> create table cliente -> (No integer primary key, -> nombre varchar(40), -> saldo double); En la primera DB crear una tabla federada mysql> use tbnaucalpan; mysql> create table cliente_r -> (No integer primary key, -> nombre varchar(40), -> saldo double) -> engine federated -> connection= ->'mysql://root@localhost:3306/tbnaucalpan_r/cliente' -> ; Insertar en la tabla federada mysql> use tbnaucalpan; mysql> show tables; +-----------------------+ | Tables_in_tbnaucalpan | +-----------------------+ | cliente | | cliente_r | | transferencia | +-----------------------+ mysql> insert into cliente_r values (...); Verificar la inserción mysql> use tbnaucalpan; mysql> select * from cliente_r; +-----+--------------+----------+ | No | nombre | saldo | +-----+--------------+----------+ | 4 | margo | 3000 | | 5 | fili | 4| +-----+--------------+----------+ Verificar en la segunda DB mysql> use tbnaucalpan_r mysql> show tables; +---------------------------------------+ | Tables_in_tbnaucalpan_r | +---------------------------------------+ | cliente | +---------------------------------------+ mysql> select * from cliente; +----+----------------+---------+ | No | nombre | saldo | +----+-----------------+---------+ | 4 | margo | 3000 | | 5 | fili | 4| +----+----------------+----------+ Consultar las dos tablas, una local y otra federada mysql> select cliente1.nombre, cliente2.nombre, -> importe -> from cliente_r as cliente1, cliente_r as cliente2, -> transferencia -> where cliente1.No = c1 -> and cliente2.No = c2; +--------+--------+---------+ | nombre | nombre | importe | +--------+--------+---------+ | margo | fili | 1000 | +--------+--------+---------+ 4. Tercera DB en centOS Tbnaucalpan_c Iniciar la máquina virtual y conectarse. (root, r00terio) Check list en centOS Levantar la red: ifup eth0 Checar IP: ifconfig Ping a IP de Windows Levantar mysqld: service mysqld start Abrir puertos: iptables -F Conectarse a mysql Crear la DB en centOS Crear la DB en centOS Crear un usuario en centOS (liliana, 2222) y darle privilegios En la primera DB crear una tabla federada mysql> use tbnaucalpan; mysql> create table cliente_c -> (No integer primary key, -> nombre varchar(40), -> saldo double) -> engine federated -> connection= -> ‘mysql://liliana:2222@192.168.1.71:3306/tbnaucalpan_c/cliente' -> ; Consultar las dos tablas, una local y otra federada mysql> select cliente1.nombre, cliente2.nombre, -> importe -> from cliente_c as cliente1, -> cliente_c as cliente2, -> transferencia -> where cliente1.No = c1 -> and cliente2.No = c2; +--------------+--------------+-------------+ | nombre | nombre | importe | +--------------+--------------+-------------+ | Peter | kity | 1000 | +--------------+--------------+-------------+