« MySQL/Réplication » : différence entre les versions

Un livre de Wikilivres.
Contenu supprimé Contenu ajouté
Ligne 99 : Ligne 99 :
Master_Port: 3306
Master_Port: 3306
...
...
</source>

Vérifier en particulier :
Vérifier en particulier :
<source lang=bash>
Slave_IO_Running: Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Slave_SQL_Running: Yes
</source>

On peut supposer une nature réplication asynchrone :
On peut supposer une nature réplication asynchrone :
<source lang=bash>
Seconds_Behind_Master: 0
Seconds_Behind_Master: 0
</source>
</source>

Version du 11 août 2013 à 23:27

Principe

La réplication signifie que les données écrites sur le master MySQL sont envoyées à des slaves faisant office de copies.

Applications :

  • sauvegardes
  • accès en lecture de la même base depuis plusieurs serveurs : augmentation des performances
  • failover

Il y a deux types de réplication :

  • Asynchrone (master/slave)
  • Semi-asynchrone (réplication asynchrone plus avec un slave avant de terminer la requête)

Configurations des réplications :

  • standard : master->slave
  • double maître : master<->master

En Master-Master les deux hôtes sont tour à tour master et slave : le serveur A se réplique sur le serveur B qui se réplique sur le serveur A. Il n'y a pas de vérification de consistance des données, même si auto_increment_increment/auto_increment_offset est configuré les deux serveurs ne doivent pas être utilisés pour des accès concurrents.

Réplication asynchrone

C'est le cas le plus simple, un master écrit un fichier de log binaire, et les slaves peuvent lire ce dernier (potentiellement sélectivement) pour rejouer les commandes de la requête.

Étant asynchrone, le master et les slaves peuvent avoir différents états au même moment. Cette configuration peut résister aux coupures réseau.

Configuration du master

Dans /etc/mysql/my.cnf, section [mysqld] :

  • Définir un identifiant de serveur ; par exemple 1 :
server-id = 1
  • La réplication est basée sur les logs binaires, donc les activer :
log-bin
# ou log-bin = /var/log/mysql/mysql-bin.log

Créer un nouvel utilisateur pour que le slave puisse se connecter :

 CREATE USER 'myreplication';
 SET PASSWORD FOR 'myreplication' = PASSWORD('mypass');
 GRANT REPLICATION SLAVE ON *.* to 'myreplication';

Vérifier l'identifiant de serveur :

 SHOW VARIABLES LIKE 'server_id';

Configuration de chaque slave

Dans /etc/mysql/my.cnf, section [mysqld] :

  • Définir un identifiant de serveur différent du master et des autres slaves :
server-id = 2
  • Vérifier avec :
 SHOW VARIABLES LIKE 'server_id';
  • Il est aussi possible de déclarer le nom de la machine slave dans le master (cf. SHOW SLAVE HOSTS) :
report-host=slave1

Déclarer le master :

 CHANGE MASTER TO MASTER_HOST='master_addr', MASTER_USER='myreplication', MASTER_PASSWORD='mypass';

Si la réplication sert de backup, spécifier le point de départ :

 MASTER_LOG_FILE='<binary_log_from_master>', MASTER_LOG_POS=<master_binary_log_position>;

Démarrer la réplication :

 START SLAVE;

Cela va créer un fichier master.info, typiquement dans /var/lib/mysql/master.info ; contenant la configuration et le statut.

 Si la réplication tombe en panne par la suite, il est possible de la réparer en le relançant :
 START STOP;
 START SLAVE;

Vérifier la réplication

Sur le slave

 SHOW SLAVE STATUS;

Ou bien pour avoir un résultat formaté plus lisible :

 SHOW SLAVE STATUS\G

Exemple :

 *************************** 1. row ***************************
              Slave_IO_State: 
                 Master_Host: master_addr
                 Master_User: myreplication
                 Master_Port: 3306
 ...

Vérifier en particulier :

           Slave_IO_Running: Yes
           Slave_SQL_Running: Yes

On peut supposer une nature réplication asynchrone :

       Seconds_Behind_Master: 0

Voir aussi :

 mysql> SHOW GLOBAL VARIABLES LIKE "%SLAVE%";

Sur le master

Vérifier les connexions des slaves :

 mysql> SHOW PROCESSLIST\G
 [...]
 *************************** 6. row ***************************
      Id: 14485
    User: myreplication
    Host: 10.1.0.106:33744
      db: NULL
 Command: Binlog Dump
    Time: 31272
   State: Has sent all binlog to slave; waiting for binlog to be updated
    Info: NULL

If you enabled <code>report-host</code>, the slave is also visible in:
 mysql> SHOW SLAVE HOSTS;
 +-----------+---------+------+-------------------+-----------+
 | Server_id | Host    | Port | Rpl_recovery_rank | Master_id |
 +-----------+---------+------+-------------------+-----------+
 |         2 | myslave | 3306 |                 0 |         1 | 
 +-----------+---------+------+-------------------+-----------+
 1 row in set (0.00 sec)

Consistency

Note that this replication is a simple replay, similar to feeding a mysqldump output to the mysql client. Consequently, to maintain the consistency:

  • Do not write on the slave (this is possible!!)
  • Start the replication with identical initial data on both the master and the slave
  • To test: we suspect it would be best to use the same version of MySQL on the master and slaves

Fixing

By default, replicate will stop if it meets an error. This can happen if your master and slaves were not consistent in the beginning, or due to a network error causing a malformed query.

In this case, you'll get a trace in the system log (typically /var/log/syslog):

Oct 15 21:11:19 builder mysqld[4266]: 101015 21:11:19 [ERROR] Slave: Error 'Table 'mybase.form'
  doesn't exist' on query. Default database: 'mybase'.  Query:
  'INSERT INTO `form` (`form_id`,`timestamp`,`user_id`) VALUES ('abed',1287172429,0)',
  Error_code: 1146

The best way is to reset the replication entirely.

You can also fix the mistake manually, and then ask MySQL to skip 1 statement this way:

STOP SLAVE;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
START SLAVE;

You can set SQL_SLAVE_SKIP_COUNTER to any number, e.g. 100. Beware that in this case, it will skip both valid and invalid statements, not only errors.


Another way to fix broken replication is to use Maatkit tools.

  • mk-slave-restart (to restart replication on slave it there are more errors and SQL_SLAVE_SKIP_COUNTER can't help)
  • mk-table-checksum (to perform checksumming of tables on master and slave)
  • mk-table-sync (to sync slave with master based on stats generated by mk-table-checksum)

Uninstalling

To erase the replication:

  • Type:
mysql> RESET SLAVE;
  • Note: at this point, MySQL paused the slave and replaced the configuration with default values. The master.info file was also removed.
  • Restart MySQL to clear all configuration.

Warning: STOP SLAVE will stop replication. It can be started manually again or (by default) it will automatically resume if you restart the MySQL server. To avoid auto start of replication during process of startup, add to your configuration file:

slave-skip-start 

If you want to stop the replication for good (and use the server for another purpose), you need to reset the configuration as explained above.

At this point your slave configuration should be completely empty:

mysql> SHOW SLAVE STATUS;
Empty set (0.00 sec)