Help - Search - Members - Calendar
Full Version: Mysql replication
The Planet Forums > System Administration > Server Hardware > Private Racks
theuruguayan
HOW-TO: MySQL Replication

This guide is designed to help do the initial setup on a MySQL cluster in which multiple MySQL servers all serve the same content through the use of the replication function. We have successfully deployed this solution for multiple clients and it is a very good option for those needing a more powerful mysql solution. Carlos (theuruguayan) actually created this document so please direct any questions to carlos@totalserversolutions.com .

Be sure your mysql servers are running the same version before starting this guide, yes, is possible to have a few combinations of master-slave versions, for more information about this you can check:

http://dev.mysql.com/doc/refman/4.1/en/rep...patibility.html


1 - Write down which is the setup you are going to do, which server is master and which server/s will be slave.

2 - Select your username/password for replications accounts. You can have one per server if you want, or one for all the mysql network.

3 - mysql> GRANT REPLICATION SLAVE ON *.*

TO 'USERNAME'@'IPFROMTHESLAVE' IDENTIFIED BY 'PASSWORD';

Username: mysql username
IPfromtheslave: ip from the mysql server that will be the one replicating the master db.
PASSWORD: the password for the replicator account.

Just a few side notes.

a) None of the passwords need to be root passwords.
b) Is not recomend to use only 1 user for replication in all the network.

4) In the master server you need to Flush all the tables, this will prevent clients from writing the db so it will keep without change while we copy over.

mysql> FLUSH TABLES WITH READ LOCK;


5) Make sure that the [mysqld] section of the my.cnf file on the master host includes a log-bin option. The section should also have a server-id=master_id option, where master_id must be a positive integer value from 1 to 232 – 1. For example:

[mysqld]

log-bin=mysql-bin

server-id=1
6) Login using another ssh client to the master server and lets create a snapshot.

mkdir /home/slave_db
rsync -vrplogDtH /var/lib/mysql /home/slave_db

You may not want to replicate the mysql database if the slave server has a different set of user accounts from those that exist on the master. In this case, you should exclude it from the archive. When the rsync is finish, just login inside mysql and type:

SHOW MASTER STATUS;

Save this info in a txt file inside the slave_db folder that we will use them laster. After you finish doing this, you can reenable the activity on the master: UNLOCK TABLES;

7) Stop the server that is to be used as a slave server and add the following to its my.cnf file:

[mysqld]

server-id=slave_id

The slave_id value, like the master_id value, must be a positive integer value from 1 to 232 – 1. In addition, it is very important that the ID of the slave be different from the ID of the master. For example:

[mysqld]

server-id=2

Remember that server-id must be unique in all the mysql network.

icon_cool.gif Copy the files over from the slave_db folder to the remote location. You can do this doing the following command:

rsync -e ssh -avz /home/slave_db/ root@REMOTESERVER:/var/lib/mysql

Check that all the permitions and correctly in the /var/lib/mysql folder.Remember files must be own by mysql:mysql



9) Start Mysql and enter to it, write the following changing the values that are needed:

mysql> CHANGE MASTER TO

-> MASTER_HOST='master_host_name',

-> MASTER_USER='replication_user_name',

-> MASTER_PASSWORD='replication_password',

-> MASTER_LOG_FILE='recorded_log_file_name',

-> MASTER_LOG_POS=recorded_log_position;



10) type: START SLAVE;
mlx
I didn't exactly follow your guide, just found it right now trying to fix a problem I had. Running a quite busy vBulletin on one of my servers, I've recently setup a slave MySQL server. This night the replication crashed because I didn't setup log rotation of the binary log file. It stopped working at 1024 MB.

I have now added "max_binlog_size=256M" to my my.cnf and hope that this is all I need to do in order to make MySQL automatically rotate that log file.

You might want to add that to your how-to I guess.
theuruguayan
hmm, not sure. i have loogs way bigger than 1gb..and they are working without problems..

sure that the reason to crash ? did you got out of space on the hd?
mlx
QUOTE (theuruguayan)
hmm, not sure. i have loogs way bigger than 1gb..and they are working without problems..

sure that the reason to crash ? did you got out of space on the hd?


That's weird, but I'm pretty sure that this is the reason. Running RHEL3 with Ensim and MySQL 4.1.15 RHEL3 RPMs. Definitely enough space on HDD and the site doesn't have a disk quota either.
I'm right now still a bit busy getting the slave up again, will have another look at the MySQL docs later. Maybe there's some more info about my problem.

Thanks for your fast reply and this how-to as well icon_smile.gif
mlx
OK, I'm at least coming closer to the actual problem. MySQL tried to rotate the log file at 1 GB (standard max variable) but wasn't able to create a new file in /var/lib/mysql I think. That might be a stupid Ensim related problem.

[EDIT]
I noticed that the Ensim MySQL startup script (/etc/rc.d/init.d/mysqld_app_init) did the following:
CODE
chown root.mysql /var/lib/mysql


I changed that to the following now
CODE
chown mysql.mysql /var/lib/mysql


I think this way it should work now. Let's see ...
[/EDIT]
ramon
You might want to look at this
nice and easy guide to clone a mysql slave:
reez
We use a cron script on the slave server to purge the master log files based on a time.

This script will purge any binlogs that are over four hours old.
There are other scripts out there that are smart enough to verify which log the slave is still reading and purge the rest except that one. I just don't have it handy.

CODE
mysql -uxxxxxxx -hmaster_server -pxxxxxxx -e "PURGE MASTER LOGS BEFORE NOW() - INTERVAL 4 HOUR"


We run this every hour due to the volume of writes the master gets. (10,000 queries per second) which comes out on our end to be about 3gig an hour.
theuruguayan
not a bad idea
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.