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 server and the slave is in a web hosting.
Greetings !!!
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 server and the slave is in a web hosting.
Greetings !!!
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 configuration file mysql
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 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 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
We restart the services and create the dbtest database
mysql> create database dbtest
now we import our database copy that we created in primary database
mysql -u root -p dbtest < dbtest.sql
We connect to standby database
and execute
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:
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 try doing insert in primary database
and then a select in standby
Well you could create a php script with 2 different dblinks and well execute the statements to create the DB, look is not that 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. ";
}
}
}
}
}
?>