Setting up MySQL Replication
For this we’ll assume the following.
1. all databases and tables are replicated, we are not excluding anything from being replicated 2. we have identical /etc/my.cnf files for both master and slaves, the only difference is the server-id needs to be unique on all servers in the cluster. Even a slave of a slave of a slave needs to have a unique server-id 3. we have a username setup that has priviledges for replication (reading replication logs) 4. we’re using the following settings in the /etc/my.cnf on all servers (you can alter these but then you need to update the instructions)
datadir = /var/lib/mysql
log-bin = /var/log/mysql/bin.log
log-bin-index = /var/log/mysql/log-bin.index log-error = /var/log/mysql/error.log #these two do not need to be active if you do not want log = /var/log/mysql/sql.log log-slow-queries = /var/log/mysql/slow_query.log
5. on the slave databases you should add the following to the /etc/my.cnf
read_only = 1
# this is disabled in our config, enable it if you do not want to auto-restart the slave process when you restart mysql #skip-slave-start
6. we’re using the following information in the instructions
mydb1 = master db
mydb2 = new slave db
replication username = repl
- make sure mysql is stopped on both servers (service mysql stop) or (service mysqld stop)
- on all servers remove all the existing error/sql/slow-query/replication logs if they exist (rm -rf /var/log/mysql/*
- on mydb1 run (rsync -ave ssh /var/lib/mysql/* mydb2:/var/lib/mysql/)
- on mydb2 run (chmod -R mysql.mysql /var/lib/mysql)
- on mydb1 run (service mysql start)
- check logs for any errors and make sure that you can query the db
- on mydb2 run (service mysql start)
- check logs for any errors and make sure that you can query the db
- on mydb1 connect to mysql and execute (SHOW MASTER STATUS;), you will need to note the File name. It should be something like “bin.000001″
- on mydb2 connect to mysql and execute the following 3 SQL statements, make sure you do not proceed if there are errors
change master to MASTER_HOST=’mydb1.mydomain.com’, MASTER_USER=’repl’, MASTER_PASSWORD=’your_repl_password’, MASTER_LOG_FILE=’bin.000001′, MASTER_LOG_POS=4;
slave start;
show slave status\G
- at this point replication if there were no errors, replication should be working. If there were errors make sure you read the error log on
mydb2
- to test replication, on mydb1 create a database or table, then check
mydb2 to see if the change replicated
