# [HOWTO] MySQL Replication

## beandog

[HOWTO] MySQL Replication

About:  It took me days to figure out how to replicate database in MySQL, and the official docs are somewhat sparse.  Hopefully this will help -- it's geared towards any general MySQL installation.

Edit: This guide only works with MyISAM table types (the default for MySQL).

The Master Server

MySQL replicates the data between the two databases by using binary logs stored on the master server.  The first step is to setup the server so it creates them.

Create a directory to store the binary logs

This creates the directories and sets the permissions so only the mysql daemon can read/write to the logs.

```
$ mkdir /var/log/mysql/binary

$ chown mysql: /var/log/mysql/binary

$ chmod 770 /var/log/mysql/binary
```

Edit the MySQL Configuration

This turns on the binary logging, tells it where to store the logs, and creates a server id.

```
$ vim /etc/mysql/my.cnf
```

Add under the [mysqld] section.

It doesn't matter what the value for server-id is, as long as it is different from the slave's (which you will set later).

Also make sure that "bind ip_address" and "skip-networking" are commented out, so that the slave user can log in remotely.

```
log-bin=/var/log/mysql/binary/mysql_binary_log

binlog-do-db=database_name

server-id=1
```

Once you're done, restart the mysql server for the changes to take effect, and for the logging to start.

```
/etc/init.d/mysql restart
```

If you were already using binary logging before this, you don't need to make any changes, except for adding a server-id.  The slave can keep up with your current binary logs, so there's no reason to delete them or flush the logs.

Setup a slave account

This both creates a new account in mysql.user, and grants replication privileges only.

```
$ msyql

mysql> GRANT REPLICATION SLAVE ON database_name TO 'slave_user_name'@'%' IDENTIFIED BY 'slave_password';
```

Adding "@'%'" means that "slave_user_name" can connect from any host.  Change yours to whatever host name options you want to use.

AFAIK, you don't need to run FLUSH PRIVILEGES; in mysql for it to recognize the new account.

Get a database dump

There are two ways to get the data from the master to the slave -- this howto only covers one: exporting the actual data to a file, and then restoring it on the slave.  The other option is LOAD DATA FROM MASTER; which does pretty much the same thing.

Note that you will need to have two shells open at this point -- one mysql shell and one terminal shell.  The mysql shell must stay open while you do the database dump to get a clean export.

Switch to the database you want to replicate.  FLUSH TABLES only locks the database you're currently using.

```
mysql> USE database_name;

mysql> FLUSH TABLES WITH READ LOCK;
```

Once again, do NOT close your mysql session until you do a mysqldump in another shell (screen comes in handy).  If you do, the tables will be unlocked.

```
mysql> SHOW MASTER STATUS;
```

Once you get the MASTER STATUS results, WRITE THEM DOWN because you'll need them in a minute.  Should look something like this:

```
+----------------------+----------+--------------+------------------+

| File                 | Position | Binlog_do_db | Binlog_ignore_db |

+----------------------+----------+--------------+------------------+

| mysql_binary_log.025 | 796947   | database_name|                  |

+----------------------+----------+--------------+------------------+

1 row in set (0.00 sec)
```

Get the db dump:

```
$ mysqldump --opt database_name > database_name.sql
```

and unlock the tables

```
mysql> UNLOCK TABLES;
```

You're done now with the master server.

The Slave Server

Import the mysqldump from the master server:

```
mysql database_name < database_name.sql
```

Edit the slave's MySQL configuration to setup replication:

```
$ vim /etc/mysql/my.cnf
```

Add under the [mysqld] section.

It doesn't matter what the value for server-id is, as long as it is different from the master's.

```
server-id=2

# master server settings

master-host=masterhost.com

master-user=slave_user_name

master-password=slave_password

master-connect-retry=60 # num of seconds, default is 60

replicate-do-db=database_name
```

Restart MySQL -- don't worry, it won't start slaving yet.

```
/etc/init.d/mysql restart
```

Tell the slave specifically where to start logging:

This is where you use the data from SHOW MASTER STATUS; from the master mysql database.

```
mysql> CHANGE MASTER TO MASTER_HOST='master_host_name', MASTER_USER='slave_user_name', MASTER_PASSWORD='slave_password', MASTER_LOG_FILE='mysql_binary_log.025', MASTER_LOG_POS=796947;
```

Note that you don't put quotes around the value for MASTER_LOG_POS or mysql will give you an error.

Now start slaving!

```
mysql> START SLAVE;
```

And that's it!  You should be good to go.  :Smile: 

MySQL Docs

How to setup replication

The Binary Log

The mysqlbinlog Binary Log Utility

The mysqldump Database BackupProgram

Please lemme know about errors / fixes / suggestions whatever.

Steve

----------

## oliver

great! thanks.  We just had a requirement to do this at work so this guide is extremely timely

----------

## maztaim

I like it when it's simple.  That's EXACTLY what I was looking for.  Now to give it a try...

----------

## thrasher6670

Hey mind if I post this on http://gentoo-wiki.com ?

----------

## beandog

Of course, the wiki rocks.  :Smile: 

----------

## Cagnulein

mmm...exactly what i was looking for...if i understood is possibile to "share" a mysql db, right?

Example:

at the moment i have a database (100 mb of stuff) on one machine. The traffic and the cpu load push me to thinking about split this db in 2 parts.

The first part that contains the recent data and it stores to the actual machine, and the second part that contains the other stuff (in on other machine  :Smile:  ).

So all the users will have to log in into the first machine and this mechaninsm will have to be same as now for their. 

Is it possibile to create?

Thanks.

----------

## Sfynx

 *Cagnulein wrote:*   

> mmm...exactly what i was looking for...if i understood is possibile to "share" a mysql db, right?
> 
> Example:
> 
> at the moment i have a database (100 mb of stuff) on one machine. The traffic and the cpu load push me to thinking about split this db in 2 parts.
> ...

 

The replicated information on the master and the slave is the same, it's like a mirror. You may not edit the replicated data on the slave with normal clients, or things will get messed up (there is a server option to prevent that from happening accidentally). The master gets edited and the changes get propagated to the slave, not the other way around.

Some extra notes:

- You don't have to write down the log file number and offset when you call mysqldump with the --master-data option, it will include the right data in the dump file in that case and works for both MyISAM and InnoDB tables. From the MySQL replication HOWTO on http://dev.mysql.com/doc/mysql/en/replication-howto.html:

 *Quote:*   

> An alternative that works for both MyISAM and InnoDB  tables is to take an SQL dump of the master instead of a binary copy as described in the preceding discussion. For this, you can use mysqldump --master-data on your master and later load the SQL dump file into your slave. However, this is slower than doing a binary copy.

 

- Replication over an SSL connection (which is wise to do through an unstrusted network like the Internet) requires MySQL 4.1 or higher if you don't want to use an external tunneling program like stunnel. Should be unmasked in Portage someday, it reached production level a while ago.

----------

## efratk

Hello,

I managed to set a replication server, but now I want the replication server to be the master for other replication servers.

I enabled the bin-log in the slave too, but when "show master status;" returns static position of 79, although it's being updated

from his master. Did anyone do it?

Thanks

Efrat Kadosh

----------

## Klavs

Just wanted to note, that atleast in MySQL 4.0 this won't work:

log-bin=/backup/bigdisk/mysqlbinlogs

(when mysqlbinlogs is a directory) - you need to give a filename too - according to MySQL (took me a while to find this annoying detail  :Smile: 

This will work:

log-bin=/backup/bigdisk/mysqlbinlogs/binlog

----------

## meulie

 *Sfynx wrote:*   

>  *Cagnulein wrote:*   mmm...exactly what i was looking for...if i understood is possibile to "share" a mysql db, right?
> 
> Example:
> 
> at the moment i have a database (100 mb of stuff) on one machine. The traffic and the cpu load push me to thinking about split this db in 2 parts.
> ...

 

What is the recommended/easy solution for 2-way replication where data can be added/edited on either host, and gets replicated (or synced later if the other is down for the count).

Can I configure both servers as both master & slave for each other?

----------

## kashou

 *beandog wrote:*   

> MySQL replicates the data between the two databases by using binary logs stored on the master server.  The first step is to setup the server so it creates them.
> 
> Please lemme know about errors / fixes / suggestions whatever.

 

This is a great resource. I was wondering if you could specify how to do this for specific databases only? I have 5 databases on my server, but I only want to replicate 3 of them to the slave.

Any help would be great!

-kash

----------

## SeeksTheMoon

can I use the same slave server to replicate databases from two different master computers?

----------

## lakshmi.g

Could you please provide me the details regarding this documentation. This doc applies to Mysql Cluster also?. Shall I use this for database synchronization through WAN?.

Thanks in Advance.

----------

## 9h1lo

Does this apply when I have about 5 databases on 1 mysql server ? will they all be synced from master to slave? I need the slave to do mysqldumps without locking the database

----------

## 9h1lo

when i try this i get 

ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log

----------

