Master-slave solution for MySQL with Percona
MySQL master
On the Master server, run the following commands:
$ export MASTER_NAME=ip-172-31-46-150 $ export SLAVE_NAME=ip-172-31-39-130 $ export BACKUP_DIR=$INSTALL_DIR/backup $ export INSTALL_DIR=/opt/workfusionTurn on
log-bin.Open the
my.cnffile.vi $<INSTALL_DIR>/mysql/usr/my.cnfAdd the following lines to the file:
#init_connect='CALL wfdb.change_isolation_level('wfdb_read@%')' server-id = 10 log-bin = /opt/workfusion/mysql/var/log/mysql-replication.logGrant permission to
$REPL_USER:mysql --host=$MASTER_NAME --user=mysql --password='BhYx!@o5%m' SQL> GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'%' IDENTIFIED BY 'repl';Create a backup file:
$ mkdir -p $BACKUP_DIR $ innobackupex --user=mysql --password='BhYx!@o5%m' $BACKUP_DIR --no-timestamp $ innobackupex --apply-log $BACKUP_DIRRestart the Master server:
wfmanager restart mysql
Slave server
On the slave server, run the following commands:
$ export MASTER_NAME=ip-172-31-39-130 $ export SLAVE_NAME=ip-172-31-46-150 $ export SLAVE_PASSWORD='BhYx!@o5%m' $ export ADMIN_PASSWORD='BhYx!@o5%m' $ export ADMIN_USER=mysql $ export BACKUP_DIR=$INSTALL_DIR/backup $ export INSTALL_DIR=/opt/workfusion $ export REPL_USER=repl $ export REPL_PASS=replCreate a backup directory:
$ mkdir -p $BACKUP_DIR $ cd $BACKUP_DIRCopy a backup file from the master server to the slave one:
rsync -avu $MASTER_NAME:$BACKUP_DIR/* $SLAVE:$BACKUP_DIRStop MySQL on the slave server:
wfmanager stop mysqlMove the old data to the safe location if needed:
mv $INSTALL_DIR/mysql/var/lib/mysql $INSTALL_DIR/mysql/var/lib/mysql_OLDRestore the files from backup to the
$INSTALL_DIR/mysql/data location (by default):$ cd $BACKUP_DIR $ mkdir -p $INSTALL_DIR/mysql/data $ xtrabackup --move-back --target-dir=$BACKUP_DIRCheck connectivity from the slave server to the Master server:
mysql --host=$MASTER_NAME --user=mysql --password='BhYx!@o5%m'Open the
my.cnffile and add the following lines in it:vi $INSTALL_DIR/mysql/etc/my.cnfserver-id = 20Change
MASTER_LOG_FILEandMASTER_LOG_POS. For more information, see$BACKUP_DIR/xtrabackup_binlog_info.cat $BACKUP_DIR/xtrabackup_binlog_infoStart the Master server:
$ wfmanager start mysql $ mysql --user=mysql --password='BhYx!@o5%m' $ SQL> CHANGE MASTER TO MASTER_HOST='ip-172-31-39-130', MASTER_USER='repl', MASTER_PASSWORD='repl', MASTER_LOG_FILE='mysql-replication.000001', MASTER_LOG_POS=154;Start the slave server:
SHOW SLAVE STATUS \G;
Change role from slave to Master
To change the server's role:
On the slave server, run the following command:
$ mysql --user=mysql --password='BhYx!@o5%m'Stop the slave master:
STOP SLAVE;Reset the Master server:
RESET MASTER;Reset Slave All.
Run the check:
SHOW SLAVE STATUS \G;The command must return an empty set.
Restart all servers:
wfmanager restart all