Skip to content

Instantly share code, notes, and snippets.

@ZsBT
Last active November 28, 2022 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ZsBT/8b4178a3e6a6c02ce32cb987929aaf58 to your computer and use it in GitHub Desktop.
Save ZsBT/8b4178a3e6a6c02ce32cb987929aaf58 to your computer and use it in GitHub Desktop.
create mysql/mariadb master-master replication
#!/bin/bash
#
# setup mysql replication.
# run this script parallel on both servers as we need log file name and position on the other node
#
#
ROOT_PASS="rootP@ssw0rd"
REPLI_PASS="replicationP@ssw0rd"
SIBLING_IP="1.2.3.4"
SERVER_ID=1 # first server is 1, second is 2
###
REPLI_USER="replicator"
MCLIENT="mysql -uroot -p$ROOT_PASS"
CONFFILE="/etc/mysql/conf.d/repli.cnf"
[ -d /etc/mysql/mariadb.conf.d ] && CONFFILE="/etc/mysql/mariadb.conf.d/72-repli.cnf"
set -e
# add config file
echo "[mysqld]
server-id=$SERVER_ID
log-bin=\"mysql-bin\"
binlog-ignore-db=test
binlog-ignore-db=information_schema
replicate-ignore-db=test
replicate-ignore-db=information_schema
relay-log=\"mysql-relay-log\"
auto-increment-increment = 10
auto-increment-offset = $SERVER_ID
bind-address = 0.0.0.0
" >$CONFFILE && echo "$CONFFILE created"
# create replicator user. drop first manually if already exists
echo "Create user $REPLI_USER"
echo "
CREATE USER $REPLI_USER@'%' IDENTIFIED BY '$REPLI_PASS';
GRANT REPLICATION SLAVE ON *.* TO $REPLI_USER@'%' IDENTIFIED BY '$REPLI_PASS';
" | $MCLIENT
service mysql restart
echo "---enter these on $SIBLING_IP:-----------";
echo "SHOW MASTER STATUS;"|$MCLIENT
echo "-------------------------------------";
echo -n "enter File as shown on $SIBLING_IP: ";read repliFile
echo -n "enter Position as shown on $SIBLING_IP: ";read repliPosition
echo "
STOP SLAVE;
CHANGE MASTER TO MASTER_HOST = '$SIBLING_IP', MASTER_USER = '$REPLI_USER', MASTER_PASSWORD = '$REPLI_PASS', \
MASTER_LOG_FILE = '$repliFile', MASTER_LOG_POS = $repliPosition;
START SLAVE;
" |$MCLIENT
service mysql restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment