database

How do I set up replication on my Linux MySQL database server?

1. Install MySQL (mysql-server) on the master and slave servers if it’s not already installed. You can do so using ‘yum install mysql-server’.

2. On the master server, add the following to the /etc/my.cnf file towards the top in the [mysqld] block:

log-bin=mysql-bin
server-id=1

server-id=1 denotes that it’s a MySQL master.

3. For the change to take effect, mysqld needs to be restarted. If it’s safe to do so, run ‘service mysqld restart’.

4. You can confirm that it’s known as a master by running ‘show master status’ from the mysql> prompt.

5. Grant permissions to the replication account of your choice using the example below:

grant replication slave on *.* to ‘replication’@’%’ identified by ‘slavepass’;

6. Next, login to the MySQL slave server and update it’s /etc/my.cnf file adding the following lines to the [mysqld] block:

server-id=2
relay-log=mysqld-relay-bin
report-host=$HOSTNAME
master-host=$IP_OR_HOSTNAME_OF_MASTER
master-user=replication
master-password=slavepass
master-connect-retry=60

7. Login to mysql and execute the following command:

change master to master_host=’$IP_OR_HOSTNAME_OF_MASTER’, master_user=’replication’, master_password=’slavepass’,master_log_file=’mysql-bin.000001′, master_log_pos=98 ;

replacing the values appropriately.

8. On the slave, run ‘start slave;’ then ‘show slave status;’.

NOTE: If this is an existing database you want to replicate, you will need to import the data first using a mysqldump.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top