Réplication MySQL sur la Même Machine (Master → Slave)
Étape 1 : Configuration du MASTER
Modifier le fichier [Link] (exemples possibles sous C:\ProgramData\
MySQL\...)
Ajouter :
server-id=1
port=3306
log_bin=mysql-bin
binlog_format=ROW
Redémarrer MySQL.
Créer la base de test :
CREATE DATABASE replication_db;
USE replication_db;
CREATE TABLE test (id INT AUTO_INCREMENT PRIMARY KEY,
msg VARCHAR(100));
INSERT INTO test(msg) VALUES ('hello master');
Étape 2 : Création de l’utilisateur de réplication sur le MASTER
CREATE USER 'repl'@'[Link]' IDENTIFIED BY 'repl_password';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'[Link]';
FLUSH PRIVILEGES;
Étape 3 : Obtenir la position du binaire log
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
ql-bin.000001) et Position (ex: 154)
Étape 4 : Exporter la base du MASTER
mysqldump -u root -p --databases replication_db --single-transaction --quick
--master-data=2 > C:\backup\replication_db_dump.sql
UNLOCK TABLES;
Étape 5 : Création de l’instance SLAVE
Créer un deuxième dossier MySQL (par ex. C:\MySQL\MySQL-Slave) et
un datadir dédié :
C:\MySQL-Data-Slave\
Créer un fichier [Link] pour le SLAVE :
server-id=2
port=3307
datadir="C:/MySQL-Data-Slave"
relay_log="mysql-relay-bin"
read_only=ON
Installer le service :
mysqld --install MySQLSlave --defaults-file="C:\MySQL\MySQL-Slave\
[Link]"
Démarrer :
net start MySQLSlave
Étape 6 : Importer la base dans le SLAVE
mysql -u root -p -P 3307 -h [Link] < C:\backup\replication_db_dump.sql
Étape 7 : Configurer la réplication sur le SLAVE
STOP SLAVE;
CHANGE MASTER TO
MASTER_HOST='[Link]',
MASTER_PORT=3306,
MASTER_USER='repl',
MASTER_PASSWORD='repl_password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=154;
START SLAVE;
Étape 8 : Vérifier la réplication
SHOW SLAVE STATUS\G
Vérifier :
Slave_IO_Running = Yes
Slave_SQL_Running = Yes
Seconds_Behind_Master = 0
Étape 9 : Tester
MASTER :
INSERT INTO replication_db.test(msg) VALUES('replication OK');
SLAVE :
SELECT * FROM replication_db.test;