Hackosis is an Open Blog. You Can Participate.

  • 09
  • Jun

MySQL is an excellent open source database system. Replication is a great way to keep data redundant in case of a server crash. However, replication should not take the place of backups in case of data corruption or mis-entered data - as this data will also be replicated to the slave.

MySQL Replication

MySQL replication takes place in a master-slave configuration. Be aware that by using the configuration - only changes made on the master are replicated to the slave. Any changes on the slave will not be replicated to the master.

Following the steps below, you can have MySQL replication setup in no time at all.

Source: MySQL Dev Site

1. Open the my.cnf or my.ini (depending on linux or windows).

2. Enter somewhere below ‘[mysqld]‘ on the master server.

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

3. Restart mysqld on the master server

4. Create a user on the master with the ‘REPLICATION SLAVE’ privilege. This user needs no other privileges. Replace X.X.X.X with the IP address of the slave server.

CREATE USER ‘user’@ ‘X.X.X.X’ IDENTIFIED BY ‘password’;
GRANT REPLICATION SLAVE ON *.* TO ‘user’@'X.X.X.X’ IDENTIFIED BY ‘password’;

5. Execute ‘FLUSH TABLES WITH READ LOCK;’ on the master to prevent writing to the databases.

6. Execute ‘SHOW MASTER STATUS;’ on the master and record the values for later.

7. Execute ‘UNLOCK TABLES;’ on the master.

8. Open the my.cnf or my.ini on the slave server.

9. Enter somewhere below ‘[mysqld]‘ on the slave server:

server-id=2

10. Save the file and restart mysqld.

11. Execute the following on the slave server (adjust values accordingly to user setup in step 4 and values retrieved from step 6):

CHANGE MASTER TO
MASTER_HOST=’X.X.X.X’,
MASTER_USER=’user’,
MASTER_PASSWORD=’password’,
MASTER_PORT=3306,
MASTER_LOG_FILE=’mysql-bin.000001′,
MASTER_LOG_POS=98,
MASTER_CONNECT_RETRY=10;

12. Execute the following on the slave server:

START SLAVE;

13. Check the mysql log on the slave to ensure that the connection to the master has been successful. You should see a line similar to the following:

080609 8:47:02 [Note] Slave I/O thread: connected to master ‘root@X.X.X.X:3306′, replication started in log ‘mysql-bin.000001′ at position 98

You should now have a successful MySQL master-slave configuration. If you have any questions please let me know.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Posts


Tags: , , , , , ,

Like this post? Subscibe to the RSS feed.


16 Comments

  1. Configurar básica de replicación en MySQL Says:

    [...] Vía: Hackosis [...]

  2. Stephane Says:

    Hi,

    Great how-to !

    I’d just add that it would be good to limit the binary log files retentions with the expire_logs_days on the master, otherwise they’ll just stay for ever !

    Stephane
    http://www.sakana.fr/blog/

  3. Shane Says:

    Thanks Stephane - any ideas on how to do this automatically?

  4. Daily Find #82 | TechToolBlog Says:

    [...] Setting up mySQL replication in 10 minutes [...]

  5. smcnally Says:

    Hello - A weekly cron job to purge master log files on a rolling 31-day basis:

    0 9 * * mon mysql -uroot -e “PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 31 DAY);”

    http://dev.mysql.com/doc/refman/5.1/en/purge-master-logs.html

    you can also set the system variable expire_logs_days in

    http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#option_mysqld_expire_logs_days

  6. links for 2008-07-09 « Brent Sordyl’s Blog Says:

    [...] How To Setup MySQL Replication in 10 minutes Following the steps below, you can have MySQL replication setup in no time at all. (tags: mysql replication) [...]

  7. links for 2008-07-12 | JeremiahTolbert.com Says:

    [...] How To Setup MySQL Replication in 10 minutes | Hackosis (tags: mysql database howto backup replication) [...]

  8. Jamie Quint Says:

    Shane,

    Try this…

    expire-logs-days = 20
    max_binlog_size = 104857600

  9. Shane Says:

    Thanks for the tips Jamie.

  10. Chris Chandler Says:

    How do I configure the replicaiton to take only certain database:

    *************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
    Master_Host: 10.10.10.15
    Master_User: dhi16_slave
    Master_Port: 3306
    Connect_Retry: 10
    Master_Log_File: mysql-bin.000004
    Read_Master_Log_Pos: 98
    Relay_Log_File: mysql-relay-bin.000006
    Relay_Log_Pos: 235
    Relay_Master_Log_File: mysql-bin.000004
    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

    Master Configuration:

    [mysqld]

    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    user=mysql

    # Default to using old password format for compatibility with mysql 3.x
    # clients (those using the mysqlclient10 compatibility package).

    old_passwords=1

    # Replication Configuraiton
    # C. Chandler - 07.15.2008

    server-id=1
    innodb_flush_log_at_trx_commit=1
    sync_binlog=1
    relay-log=/var/log/mysql/mysql-relay-bin
    relay-log-index=/var/log/mysql/mysql-relay-bin.index
    log-error=/var/log/mysql/mysql.err
    master-info-file=/var/lib/mysql/mysql-master.info
    relay-log-info-file=/var/lib/mysql/mysql-relay-log.info
    datadir=/var/lib/mysql/
    log-bin=/var/lib/mysql/mysql-bin

    [mysqld_safe]

    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid

    Slave Configuration:

    [mysqld]

    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    user=mysql

    # Default to using old password format for compatibility with mysql 3.x
    # clients (those using the mysqlclient10 compatibility package).

    old_passwords=1

    # Replicaiton Configuration
    # C. Chandler - 07.15.2008

    server-id=2
    relay-log=/var/log/mysql/mysql-relay-bin
    relay-log-index=/var/log/mysql/mysql-relay-bin.index
    log-error=/var/log/mysql/mysql.err
    master-info-file=/var/lib/mysql/mysql-master.info
    relay-log-info-file=/var/lib/mysql/mysql-relay-log.info
    datadir=/var/lib/mysql

    [mysqld_safe]

    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid

    Thanks for the help.

    Chris

  11. Shane Says:

    Hrm, I haven’t tried only replicating one database, but I guess you could try this:

    On the slave my.cnf:

    replicate-do-db=databasename

  12. Vocescuola - Replicare un db MySQL Says:

    [...] e veloce per replicare un db in MySQL in modo da avere sempre un backup dei dati. Ritrovo su hackosis una bel tutorial che potrebbe esservi di [...]

  13. Pankaj Gugnani Says:

    Hi,

    Great Tutorial :)

    Just wanted to confirm, that this tutorial is also applicable for two instances of MySQL running on the same server? One on port no. 3306 and other on 3307.

    In case not, then please provide me some insight on that as well.

    Thanks

  14. Shane Says:

    I have not tested this with two instances running on the same machine. Let us know what you find Pankaj.

  15. Pankaj Gugnani Says:

    Hi All,

    This is to inform that the tutorial works perfectly fine for two instance running on same machine but different ports. I will be posting complete steps soon.

  16. links for 2008-08-26 « Where Is All This Leading To? Says:

    [...] How To Setup MySQL Replication in 10 minutes (tags: replication Database cluster mysql tips tech tutorial) [...]

Leave a Comment