**\#!/bin/bash**
**\# Function to execute SSH commands using sshpass**
**function execute\_ssh\_command {**
**sshpass -p "$1" ssh -o StrictHostKeyChecking=no -p "$2" root@"$3" "$4"**
**}**
**\# Prompt for SSH port, password, MySQL root password, and server IP for master and slave servers**
**read -p "Enter SSH port for the master server: " ssh\_port\_master**
**read -s -p "Enter password for root user on the master server: " ssh\_password\_master**
**read -s -p "Enter MySQL root password for the master server: " mysql\_root\_pass\_master**
**read -p "Enter master server IP address: " server\_ip\_master**
**echo**
**read -p "Enter SSH port for the slave server: " ssh\_port\_slave**
**read -s -p "Enter password for root user on the slave server: " ssh\_password\_slave**
**read -s -p "Enter MySQL root password for the master server: " mysql\_root\_pass\_slave**
**read -p "Enter slave server IP address: " server\_ip\_slave**
**\# Configuration variables for master**
**bind\_address\_master="$server\_ip\_master"**
**server\_id\_master="1"**
**log\_bin="mysql-bin"**
**\# SSH command block for master server**
**ssh\_command\_master="**
**# Function to check if a line exists in a file**
**function check\_line\_in\_file {**
**grep -qF \\"\\$1\\" \\"\\$2\\"**
**}**
**# Check and append configurations in mysql-server.cnf**
**if ! check\_line\_in\_file \\"bind-address = $bind\_address\_master\\" /etc/my.cnf.d/mysql-server.cnf; then**
**echo \\"bind-address = $bind\_address\_master\\" >> /etc/my.cnf.d/mysql-server.cnf**
**fi**
**if ! check\_line\_in\_file \\"server-id = $server\_id\_master\\" /etc/my.cnf.d/mysql-server.cnf; then**
**echo \\"server-id = $server\_id\_master\\" >> /etc/my.cnf.d/mysql-server.cnf**
**fi**
**if ! check\_line\_in\_file \\"log\_bin = $log\_bin\\" /etc/my.cnf.d/mysql-server.cnf; then**
**echo \\"log\_bin = $log\_bin\\" >> /etc/my.cnf.d/mysql-server.cnf**
**fi**
**# Restart MySQL service**
**systemctl restart mysqld**
**# Check if the replica\_user exists in MySQL**
**if ! mysql -u root -p'$mysql\_root\_pass\_master' -e \\"SELECT User FROM mysql.user WHERE User='replica\_user'\\" | grep -q 'replica\_user'; then**
**mysql -u root -p'$mysql\_root\_pass\_master' -e \\"**
**CREATE USER 'replica\_user'@'%' IDENTIFIED WITH mysql\_native\_password BY '$mysql\_root\_pass\_master';**
**GRANT REPLICATION SLAVE ON \*.\* TO 'replica\_user'@'%';**
**FLUSH PRIVILEGES;**
**\\"**
**echo 'MySQL user replica\_user created.'**
**else**
**echo 'MySQL user replica\_user already exists. Skipping user creation.'**
**fi**
**# Retrieve and print master status from MySQL**
**mysql\_output\_master=\\$(mysql -u root -p'$mysql\_root\_pass\_master' -e \\"SHOW MASTER STATUS\\G\\")**
**echo \\"MySQL Output:\\"**
**echo \\"\\$mysql\_output\_master\\"**
**# Parse and print 'File' and 'Position' from master status**
**mysql\_file=\\$(echo \\"\\$mysql\_output\_master\\" | awk '/File:/ {print \\$2}')**
**mysql\_position=\\$(echo \\"\\$mysql\_output\_master\\" | awk '/Position:/ {print \\$2}')**
**echo \\"MySQL File: \\$mysql\_file\\"**
**echo \\"MySQL Position: \\$mysql\_position\\"**
**# Save the parsed file and position to a temporary file for later use**
**echo \\$mysql\_file > /tmp/mysql\_file.txt**
**echo \\$mysql\_position > /tmp/mysql\_position.txt**
**# Exit the SSH session**
**exit**
**"**
**\# Execute SSH command on master server**
**execute\_ssh\_command "$ssh\_password\_master" "$ssh\_port\_master" "$server\_ip\_master" "$ssh\_command\_master"**
**\# Read the saved file and position from the temporary files**
**mysql\_file=$(sshpass -p "$ssh\_password\_master" ssh -o StrictHostKeyChecking=no -p "$ssh\_port\_master" root@"$server\_ip\_master" "cat /tmp/mysql\_file.txt")**
**mysql\_position=$(sshpass -p "$ssh\_password\_master" ssh -o StrictHostKeyChecking=no -p "$ssh\_port\_master" root@"$server\_ip\_master" "cat /tmp/mysql\_position.txt")**
**echo "MySQL configuration and user setup on master server completed."**
**echo "MySQL File: $mysql\_file"**
**echo "MySQL Position: $mysql\_position"**
**\# Configuration variables for slave**
**bind\_address\_slave="$server\_ip\_slave"**
**server\_id\_slave="2"**
**log\_bin="mysql-bin"**
**\# SSH command block for slave server**
**ssh\_command\_slave="**
**# Function to check if a line exists in a file**
**function check\_line\_in\_file {**
**grep -qF \\"\\$1\\" \\"\\$2\\"**
**}**
**# Check and append configurations in mysql-server.cnf**
**if ! check\_line\_in\_file \\"bind-address = $bind\_address\_slave\\" /etc/my.cnf.d/mysql-server.cnf; then**
**echo \\"bind-address = $bind\_address\_slave\\" >> /etc/my.cnf.d/mysql-server.cnf**
**fi**
**if ! check\_line\_in\_file \\"server-id = $server\_id\_slave\\" /etc/my.cnf.d/mysql-server.cnf; then**
**echo \\"server-id = $server\_id\_slave\\" >> /etc/my.cnf.d/mysql-server.cnf**
**fi**
**if ! check\_line\_in\_file \\"log\_bin = $log\_bin\\" /etc/my.cnf.d/mysql-server.cnf; then**
**echo \\"log\_bin = $log\_bin\\" >> /etc/my.cnf.d/mysql-server.cnf**
**fi**
**# Restart MySQL service**
**systemctl restart mysqld**
**# Access MySQL and execute replication setup queries**
**mysql -u root -p'$mysql\_root\_pass\_slave' -e \\"**
**STOP SLAVE;**
**RESET SLAVE;**
**CHANGE MASTER TO MASTER\_HOST='$server\_ip\_master', MASTER\_USER='replica\_user', MASTER\_PASSWORD='$mysql\_root\_pass\_master', MASTER\_LOG\_FILE='$mysql\_file', MASTER\_LOG\_POS=$mysql\_position;**
**START SLAVE;**
**SHOW SLAVE STATUS\\G;**
**\\" > /tmp/slave\_status\_output.txt**
**# Print the output of SHOW SLAVE STATUS**
**echo 'Slave Status Output:'**
**cat /tmp/slave\_status\_output.txt**
**# Exit the SSH session**
**exit"**
**\# Execute SSH command on slave server**
**execute\_ssh\_command "$ssh\_password\_slave" "$ssh\_port\_slave" "$server\_ip\_slave" "$ssh\_command\_slave"**
**echo "MySQL replication setup on slave server completed."**
|