MySQL BD mirror

I would like to know if it is possible to create a mirror of a local database. That is, the master database is on an intranet-level server and the slave is on a web hosting.

Greetings!!!

 0
Author: jachguate, 2016-06-08

2 answers

You can do it like this:
database primary

mysql>  CREATE USER ' repuser' @' localhost'  IDENTIFIED BY ' 123'  
Query OK, 0 rows affected (1.45 sec)

mysql>  GRANT ALL ON rep.* TO ' repuser' @' localhost'  
Query OK, 0 rows affected (0.04 sec)

mysql>  flush privileges 
Query OK, 0 rows affected (0.01 sec)

mysql>  exit

We look for the mysql configuration file
vim /etc/my.cnf or in my.ini and we add these lines

server-id=1
binlog-do-db=dbtest #cambiamos dbtest por el nombre de tu base de datos
log-bin=mysql-bin
max_allowed_packet=16M

reiniciamos el servico de mysql
we reconnect to the primary database:

mysql>  GRANT REPLICATION SLAVE ON *.* TO ' repuser' @' %'  IDENTIFIED BY ' 123'  
Query OK, 0 rows affected (0.00 sec)

mysql>  FLUSH PRIVILEGES 
Query OK, 0 rows affected (0.00 sec)

mysql>  use rep 
Database changed
mysql>  FLUSH TABLES WITH READ LOCK 
Query OK, 0 rows affected (0.00 sec)

mysql>  SHOW MASTER STATUS 
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      323 | dbtest       |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

mysql>  exit
Bye

We Save the parameters Position and File that will be used more adelate
now we make a backup of the database:

mysqldump -u root -p dbtest >  dbtest.sql

We reconnect to the primary database

mysql>  UNLOCK TABLES

Standby server
now we go to server 2 or in this case our backup server which we will call standby database and edit the file vim /etc/my.cnf

server-id=2
binlog-do-db=dbtest
log-bin=mysql-bin

Restart services and create dbtest database

mysql>  create database dbtest

Now we import our database copy that we created in the primary database

mysql -u root -p dbtest <  dbtest.sql

We connect to the standby database and run

mysql>  CREATE USER ' repuser' @' localhost'  IDENTIFIED BY ' 123'  
Query OK, 0 rows affected (0.02 sec)

mysql>  GRANT ALL ON rep.* TO ' repuser' @' localhost'  
Query OK, 0 rows affected (0.00 sec)

mysql>  GRANT REPLICATION SLAVE ON *.* TO ' repuser' @' %'  IDENTIFIED BY ' 123'  
Query OK, 0 rows affected (0.00 sec)

mysql>  FLUSH PRIVILEGES 
Query OK, 0 rows affected (0.00 sec)

Now the parameters:

  1. MASTER_HOST= '192.168.5.103' server ip primary
  2. MASTER_USER= 'repuser' user created on primary server
  3. MASTER_PASSWORD= ' 123 ' * * the password we gave the user in the
  4. primary * * MASTER_LOG_POS=323 the Saved position parameter previously
  5. MASTER_LOG_FILE= ' mysql-bin.000001 ' the saved file parameter previous month

Now we connect again:

mysql>  CHANGE MASTER TO MASTER_HOST=' 192.168.5.103' , MASTER_USER='repuser' , MASTER_PASSWORD='123' , MASTER_LOG_FILE='mysql-bin.000001' , MASTER_LOG_POS=323 
Query OK, 0 rows affected (0.15 sec)

mysql>  SLAVE START 

And that would be all to create a replica of our database you can test by doing insert in the primary database and then a select in the standby

 2
Author: Bryro, 2018-11-22 14:54:51

Well you could create a php script with 2 different dblinks and well execute the statements to create the DB, look it's not so complicated, it's something like this:

<?php
//Datos servidor 1
$ip1 = '';
$usuario1 = '';
$password1='';
$baseDatos1='';
//Datos servidor 2
$ip2 = '';
$usuario2='';
$password2='';
$baseDatos2='';

//Conectamos el primer servidor
$server1 = new mysqli($ip1, $usuario1, $password1, $baseDatos1);
if ($mysqli->connect_errno) {
echo "No se logro conectar Servidor 1 : ".$mysqli->connect_errno." ".$mysqli->connect_error;
die();
}else{
    //Conectamos el server 2
    $server2 = new mysqli($ip2, $usuario2, $password2, $baseDatos2);
    if ($mysqli->connect_errno) {
       echo "No se logro conectar Servidor 2 : ".$mysqli->connect_errno." ".$mysqli->connect_error;
      die();
   }else{
     //Que tabla vamos a copiar del servidor 1
     $tabla='tutabla';
     //Hacemos un select del Schema de la tabla que vamos a copiar en el Servidor 1
     if($rs1 = $servidor1->query("SHOW CREATE TABLE $tabla")){
        if($rs1->num_rows > 0){
           $data1 = $rs1->fetch_array(MYSQLI_NUM);
             if($rs2 = $servidor2->query($data1[1])){
                 echo "Perfecto todo funciono como debe ser. ";
             }
        }
     }
   }
}
?>
 -2
Author: Alfonso Carrasco, 2016-06-08 18:03:37