quick & dirty mysql replication

***** i am not a mysql admin, this info was picked up with info from all over the internet. *****

master:
## hop on the master server

on our master server, we are going to add our replicant user (slave):
mysql> grant replication slave on *.* to 'slave'@'%' identified by 'password';
## remember to set a password and not use “password”

now add these lines to your my.cnf (/etc/my.cnf) under section "[mysqld]":
log-bin=mysql-bin
server-id=1

at this point restart this machines mysql instance.

now enter the mysql monitor and run this command:
mysql> show master status\G;

## output should be similar to this:

mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000001
        Position: 98
    Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

ERROR:
No query specified

this is telling us that the log file in use is
mysql-bin.000001 and that the log position
number is 98 (write this down, we are going to use this in a bit).

slave:
## hop on the slave server now

add these lines in the "[mysqld]" section of the my.cnf (/etc/my.cnf) file:
server-id=2
log-bin=mysql-bin

now restart your mysql instance.

now go back into mysql monitor and enter this command:

mysql> change master to
  > master_host='master_ip',
  > master_user='slave',
  > master_password='password',
  > master_log_file='mysql-bin.000001',
  > master_log_pos=98;

## remember, change “master_ip” to the ip address of the master server
## and “password” to the password of the slave user that was created on
## the master.

now lets start the slave:
mysql> start slave;

lets view the status:
## output should be similar to this

mysql> show slave status\G;
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 10.0.0.41
                Master_User: slave
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000001
        Read_Master_Log_Pos: 98
             Relay_Log_File: d1-relay-bin.000002
              Relay_Log_Pos: 235
      Relay_Master_Log_File: mysql-bin.000001
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
            Replicate_Do_DB: 
        Replicate_Ignore_DB: 
         Replicate_Do_Table: 
     Replicate_Ignore_Table: 
    Replicate_Wild_Do_Table: 
Replicate_Wild_Ignore_Table: 
                 Last_Errno: 0
                 Last_Error: 
               Skip_Counter: 0
        Exec_Master_Log_Pos: 98
            Relay_Log_Space: 235
            Until_Condition: None
             Until_Log_File: 
              Until_Log_Pos: 0
         Master_SSL_Allowed: No
         Master_SSL_CA_File: 
         Master_SSL_CA_Path: 
            Master_SSL_Cert: 
          Master_SSL_Cipher: 
             Master_SSL_Key: 
      Seconds_Behind_Master: 0
1 row in set (0.00 sec)

we want to see either "waiting for master to send event"
or "connecting to master". if "connecting to master"
is displayed for to long (more than 2-3 minutes), then make sure
that you can reach the mysql session from the slave server.

you should also tail your “err” logs, you should expect to see something
similar to this:

080414 10:58:53 [Note] Slave SQL thread initialized, starting replication in log 'mysql-bin.000001' at position 98, relay log './d1-relay-bin.000001' position: 4
080414 10:58:53 [Note] Slave I/O thread: connected to master 'slave@10.0.0.41:3306',  replication started in log 'mysql-bin.000001' at position 98
«
»

    Leave a Reply

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