How To Replicate MySQL Database - Step 1

Configure the Master Serve

First we have to edit /etc/my.cnf, comment out these lines:
#skip-networking
#bind-address            = 127.0.0.1

Now we need to tell MySql to write a bin-log (these logs are used by the slave to see what has changed on the master)
add these lines to /etc/my.cnf in [mysqld] section:
log-bin = /var/log/mysql/mysql-bin.log
server-id=1

If you want to replicate just one database you may add this line also:
binlog-do-db=my_database

Then restart MySql
/etc/init.d/mysqld restart

Then we log into the MySQL database as root and create a user with replication privileges
# mysql -u root -p
Enter password:
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
exit;

Configure the Slave Server

Again, we have to edit /etc/my.cnf file for the salve server,
add these lines to /etc/my.cnf in [mysqld] section:
server-id=2
master-host=192.168.1.100
master-connect-retry=60
master-user=slave_user
master-password=your_password
#replicate-do-db= my_database
relay-log = /var/lib/mysql/slave-relay.log
relay-log-index = /var/lib/mysql/slave-relay-log.index

Master-host – can be IP or host name of the Master Server
Replicate-do-db – add this just if you want replicate one database.
You should also make sure skip-networking has not been enabled.

Then restart MySql:
# /etc/init.d/mysqld restart 


No comments:

Post a Comment