if [ "$CFG_MIRRORSERVER" = "yes" ] && [ "$CFG_SQLSERVER_CLUSTER" = "galera" ] && [ "$CFG_SETUP_MASTER" = "y" ]; then
	echo "Set Up Master Galera in MySQL..."
	#node1 master
	apt_install rsync
        #CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'my5up8r58cr8tp455w8rd';
	#GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
	#flush privileges;
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'my5up8r58cr8tp455w8rd';" > /dev/null 2>&1
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';" > /dev/null 2>&1
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "flush privileges;" > /dev/null 2>&1

	#openssl genrsa 2048 > ca-key.pem
	#openssl req -new -x509 -nodes -days 730 -key ca-key.pem -out ca-cert.pem
	#openssl req -newkey rsa:2048 -days 730 -nodes -keyout server-key.pem -out server-req.pem
	#openssl rsa -in server-key.pem -out server-key.pem
	#openssl x509 -req -in server-req.pem -days 730 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
	#openssl req -newkey rsa:2048 -days 730 -nodes -keyout client-key.pem -out client-req.pem
	#openssl rsa -in client-key.pem -out client-key.pem
	#openssl x509 -req -in client-req.pem -days 365000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem
	#openssl verify -CAfile ca-cert.pem server-cert.pem client-cert.pem

#	vi /etc/mysql/my.cnf
#[client]
#port            = 3306
#socket          = /var/run/mysqld/mysqld.sock
#ssl-ca=/etc/mysql/ssl/ca-cert.pem
#ssl-cert=/etc/mysql/ssl/client-cert.pem
#ssl-key=/etc/mysql/ssl/client-key.pem
#[mysqld]
#ssl-ca=/etc/mysql/ssl/ca-cert.pem
#ssl-cert=/etc/mysql/ssl/server-cert.pem
#ssl-key=/etc/mysql/ssl/server-key.pem

#Copy certificates to all other nodes:
#ssh root@192.168.0.106 mkdir -p /etc/mysql/ssl/
#scp * root@192.168.0.106:/etc/mysql/ssl/
## if you have 3rd node do above commands on 3rd node too and so on ##
#ssh root@192.168.0.107 mkdir -p /etc/mysql/ssl/
#scp * root@192.168.0.107:/etc/mysql/ssl/

	cat > /etc/mysql/conf.d/galera.cnf <<EOF
[mysqld]
binlog_format			 = ROW
default-storage-engine		 = innodb
innodb_autoinc_lock_mode	 = 2
#innodb_log_file_size		 = 100M
#innodb_file_per_table
#innodb_flush_log_at_trx_commit	 = 2
#innodb_flush_log_at_trx_commit	 = 0
#innodb_buffer_pool_size	 = 256M
#innodb_locks_unsafe_for_binlog  = 1
#innodb_doublewrite		 = 1
#query_cache_size		 = 0
#query_cache_type		 = 0
#query_cache_limit               = 128K
#query_cache_size                = 64M
#bind-address			 = 0.0.0.0  # access all ip address
bind-address			 = 192.168.0.105
# Galera Provider Configuration
wsrep_on			 = ON
#wsrep_causal_reads		 = ON
wsrep_provider			 = /usr/lib/galera/libgalera_smm.so
#wsrep_provider			 = /usr/lib64/galera/libgalera_smm.so
#wsrep_provider_options 	 = "gcache.size=512M"
#wsrep_provider_options		 = "gcache.size=256M; gcache.page_size=128M"
#SSL for Galera
#wsrep_provider_options="socket.ssl_key=/etc/mysql/ssl/server-key.pem;socket.ssl_cert=/etc/mysql/ssl/server-cert.pem;socket.ssl_ca=/etc/mysql/ssl/ca-cert.pem"

# Galera Cluster Configuration
wsrep_cluster_name		 = "galera_cluster"
#wsrep_cluster_address		 = "gcomm://192.168.0.105,192.168.0.106"
#wsrep_cluster_address		 = "gcomm://192.168.0.105,192.168.0.106,192.168.0.107"

#wsrep_slave_threads 		 = 4 # Should be equal to the number of cpu-cores.
#wsrep_sst_auth 		 = "sstuser:my5up3r53cr3tp455w0rd"

# Galera Synchronization Configuration
wsrep_sst_method		 = rsync

# Galera Node Configuration
wsrep_node_address="192.168.0.105"
wsrep_node_name="Node1"

EOF

	#ufw enable
	ufw allow 3306/tcp
	ufw allow 4444/tcp
	ufw allow 4567/tcp
	ufw allow 4568/tcp
	ufw allow 4567/udp
	
	#Before Galera can start, you need to ensure that MariaDB service is stopped on all the nodes.
	systemctl stop mysql
	#Now, start the Galera Cluster on Node1
	galera_new_cluster
	#restart_service mysql
	#mysql -u root -p $CFG_MYSQL_ROOT_PWD -e "show status like 'wsrep_cluster_size'"


    elif [ "$CFG_MIRRORSERVER" = "yes" ] && [ "$CFG_SQLSERVER_CLUSTER" = "replication" ] && [ "$CFG_SETUP_MASTER" = "y" ]; then
	echo "Set Up Master Replication in MySQL..."
	#CREATE USER 'slaveuser2'@'server2.example.tld' IDENTIFIED BY 'slave_user_password';
	#CREATE USER 'slaveuser2'@'192.168.0.106' IDENTIFIED BY 'slave_user_password';
	#CREATE USER 'slaveuser2'@'2001:db8::2' IDENTIFIED BY 'slave_user_password';
	#GRANT REPLICATION SLAVE ON *.* TO 'slaveuser2'@'server2.example.tld';
	#GRANT REPLICATION SLAVE ON *.* TO 'slaveuser2'@'192.168.0.106';
	#GRANT REPLICATION SLAVE ON *.* TO 'slaveuser2'@'2001:db8::2';
	#QUIT;
	mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CREATE USER 'slaveuser2'@'server2.wolftronics.hu' IDENTIFIED BY 'slave_user_password'" > /dev/null 2>&1
	mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CREATE USER 'slaveuser2'@'192.168.0.106' IDENTIFIED BY 'slave_user_password'" > /dev/null 2>&1
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CREATE USER 'slaveuser2'@'2001:db8::2' IDENTIFIED BY 'slave_user_password'" > /dev/null 2>&1
	mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "GRANT REPLICATION SLAVE ON *.* TO 'slaveuser2'@'server2.wolftronics.hu'" > /dev/null 2>&1
	mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "GRANT REPLICATION SLAVE ON *.* TO 'slaveuser2'@'192.168.0.106'" > /dev/null 2>&1
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "GRANT REPLICATION SLAVE ON *.* TO 'slaveuser2'@'2001:db8::2'" > /dev/null 2>&1

	#vi /etc/mysql/my.cnf
	#[mysqld]
	#server-id                = 1
	#replicate-same-server-id = 0
	#auto-increment-increment = 2
	#auto-increment-offset  = 1
	#log_bin                 = mysql-bin.log
	#expire_logs_days        = 10
	#max_binlog_size         = 100M
	#binlog_format            = mixed
	#sync_binlog             = 1
	#relay_log              =  slave-relay.log
	#relay_log_index        = slave-relay-log.index
	#slave_skip_errors        = 1007,1008,1050, 1396
	#bind-address             = ::

	#Feature
        #max_allowed_packet=128M

	sed -i "s/#server-id.*/server-id              = 1/g" /etc/mysql/my.cnf
	sed -i "/server-id              = 1/a replicate-same-server-id = 0" /etc/mysql/my.cnf
	#mysql
	#sed -i "s/#auto-increment-increment.*/auto-increment-increment = 2/g" /etc/mysql/my.cnf
	#sed -i "s/#auto-increment-offset.*/auto-increment-offset   = 1/g" /etc/mysql/my.cnf
	#mariadb
	sed -i "s/#auto_increment_increment.*/auto_increment_increment = 2/g" /etc/mysql/my.cnf
	sed -i "s/#auto_increment_offset.*/auto_increment_offset   = 1/g" /etc/mysql/my.cnf
	sed -i "/max_binlog_size         = 100M/a binlog_format           = mixed" /etc/mysql/my.cnf
	sed -i "s/#sync_binlog.*/sync_binlog             = 1/g" /etc/mysql/my.cnf
	sed -i "/# slaves/a slave_skip_errors        = 1007,1008,1050,1396\nrelay_log              = \/var\/log\/mysql\/slave-relay.log" /etc/mysql/my.cnf
	sed -i "s/#relay_log_index.*/relay_log_index         = \/var\/log\/mysql\/slave-relay-log.index/g" /etc/mysql/my.cnf
	sed -i "s/#bind-address=.*/bind-address= ::/g" /etc/mysql/my.cnf

	#I simply skip some slave-errors:
	#1007 = Can't create database '%s'; database exists
	#1008 = Can't drop database '%s'; database doesn't exist  
	#1050 = Table '%s' already exists  
	#1396 = Operation %s failed for %s  - Can not create user


	#CHANGE MASTER TO MASTER_HOST="server2.wolftronics.hu", MASTER_USER="slaveuser1", MASTER_PASSWORD="slave_user_password", MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=326;
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "SHOW MASTER STATUS \G"
	#mysql
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CHANGE MASTER TO MASTER_HOST="server2.wolftronics.hu", MASTER_USER="slaveuser1", MASTER_PASSWORD="slave_user_password", MASTER_LOG_FILE='/var/log/mysql/mysql-bin.000002', MASTER_LOG_POS=326;" > /dev/null 2>&1
	#mariadb
	mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CHANGE MASTER TO MASTER_HOST="server2.wolftronics.hu", MASTER_USER="slaveuser1", MASTER_PASSWORD="slave_user_password", MASTER_LOG_FILE='/var/log/mysql/mariadb-bin.000002', MASTER_LOG_POS=326;" > /dev/null 2>&1
	#Start the slave:
	mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "START SLAVE;" > /dev/null 2>&1
	#and check the slave-status with
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "SHOW SLAVE STATUS \G"

    
    elif [ "$CFG_MIRRORSERVER" = "yes" ] && [ "$CFG_SQLSERVER_CLUSTER" = "galera" ] && [ "$CFG_SETUP_MASTER" = "n" ]; then
	echo "Set Up Slave Galera in MySQL..."
	#node2 slave
	apt_install rsync
        #CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'my5up8r58cr8tp455w8rd';
	#GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
	#flush privileges;
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'my5up8r58cr8tp455w8rd';" > /dev/null 2>&1
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';" > /dev/null 2>&1
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "flush privileges;" > /dev/null 2>&1

	#openssl genrsa 2048 > ca-key.pem
	#openssl req -new -x509 -nodes -days 730 -key ca-key.pem -out ca-cert.pem
	#openssl req -newkey rsa:2048 -days 730 -nodes -keyout server-key.pem -out server-req.pem
	#openssl rsa -in server-key.pem -out server-key.pem
	#openssl x509 -req -in server-req.pem -days 730 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
	#openssl req -newkey rsa:2048 -days 730 -nodes -keyout client-key.pem -out client-req.pem
	#openssl rsa -in client-key.pem -out client-key.pem
	#openssl x509 -req -in client-req.pem -days 365000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem
	#openssl verify -CAfile ca-cert.pem server-cert.pem client-cert.pem

#	vi /etc/mysql/my.cnf
#[client]
#port            = 3306
#socket          = /var/run/mysqld/mysqld.sock
#ssl-ca=/etc/mysql/ssl/ca-cert.pem
#ssl-cert=/etc/mysql/ssl/client-cert.pem
#ssl-key=/etc/mysql/ssl/client-key.pem
#[mysqld]
#ssl-ca=/etc/mysql/ssl/ca-cert.pem
#ssl-cert=/etc/mysql/ssl/server-cert.pem
#ssl-key=/etc/mysql/ssl/server-key.pem

#Copy certificates to all other nodes:
#ssh root@192.168.0.106 mkdir -p /etc/mysql/ssl/
#scp * root@192.168.0.106:/etc/mysql/ssl/
## if you have 3rd node do above commands on 3rd node too and so on ##
#ssh root@192.168.0.107 mkdir -p /etc/mysql/ssl/
#scp * root@192.168.0.107:/etc/mysql/ssl/

	cat > /etc/mysql/conf.d/galera.cnf <<EOF
[mysqld]
binlog_format			 = ROW
default-storage-engine		 = innodb
innodb_autoinc_lock_mode	 = 2
#innodb_log_file_size		 = 100M
#innodb_file_per_table
#innodb_flush_log_at_trx_commit	 = 2
#innodb_flush_log_at_trx_commit	 = 0
#innodb_buffer_pool_size	 = 256M
#innodb_locks_unsafe_for_binlog  = 1
#innodb_doublewrite		 = 1
#query_cache_size		 = 0
#query_cache_type		 = 0
#query_cache_limit               = 128K
#query_cache_size                = 64M
#bind-address			 = 0.0.0.0  # access all ip address
bind-address			 = 192.168.0.106
# Galera Provider Configuration
wsrep_on			 = ON
#wsrep_causal_reads		 = ON
wsrep_provider			 = /usr/lib/galera/libgalera_smm.so
#wsrep_provider			 = /usr/lib64/galera/libgalera_smm.so
#wsrep_provider_options 	 = "gcache.size=512M"
#wsrep_provider_options		 = "gcache.size=256M; gcache.page_size=128M"
#SSL for Galera
#wsrep_provider_options="socket.ssl_key=/etc/mysql/ssl/server-key.pem;socket.ssl_cert=/etc/mysql/ssl/server-cert.pem;socket.ssl_ca=/etc/mysql/ssl/ca-cert.pem"

# Galera Cluster Configuration
wsrep_cluster_name		 = "galera_cluster"
#wsrep_cluster_address		 = "gcomm://192.168.0.105,192.168.0.106"
#wsrep_cluster_address		 = "gcomm://192.168.0.105,192.168.0.106,192.168.0.107"

#wsrep_slave_threads 		 = 4 # Should be equal to the number of cpu-cores.
#wsrep_sst_auth 		 = "sstuser:my5up3r53cr3tp455w0rd"

# Galera Synchronization Configuration
wsrep_sst_method		 = rsync

# Galera Node Configuration
wsrep_node_address		 = "192.168.0.106"
wsrep_node_name			 = "Node2"

EOF

	#node3 slave
	#cat > /etc/mysql/conf.d/galera.cnf <<EOF
#binlog_format			 = ROW
#default-storage-engine		 = innodb
#innodb_autoinc_lock_mode	 = 2
#innodb_log_file_size		 = 100M
#innodb_file_per_table
#innodb_flush_log_at_trx_commit	 = 2
#innodb_flush_log_at_trx_commit	 = 0
#innodb_buffer_pool_size	 = 256M
#innodb_locks_unsafe_for_binlog  = 1
#innodb_doublewrite		 = 1
#query_cache_size		 = 0
#query_cache_type		 = 0
#query_cache_limit               = 128K
#query_cache_size                = 64M
#bind-address			 = 0.0.0.0  # access all ip address
#bind-address			 = 192.168.0.107
# Galera Provider Configuration
#wsrep_on			 = ON
#wsrep_causal_reads		 = ON
#wsrep_provider			 = /usr/lib/galera/libgalera_smm.so
#wsrep_provider			 = /usr/lib64/galera/libgalera_smm.so
#wsrep_provider_options 	 = "gcache.size=512M"
#wsrep_provider_options		 = "gcache.size=256M; gcache.page_size=128M"
#SSL for Galera
#wsrep_provider_options="socket.ssl_key=/etc/mysql/ssl/server-key.pem;socket.ssl_cert=/etc/mysql/ssl/server-cert.pem;socket.ssl_ca=/etc/mysql/ssl/ca-cert.pem"

# Galera Cluster Configuration
#wsrep_cluster_name		 = "galera_cluster"
#wsrep_cluster_address		 = "gcomm://192.168.0.105,192.168.0.106"
#wsrep_cluster_address		 = "gcomm://192.168.0.105,192.168.0.106,192.168.0.107"

#wsrep_slave_threads 		 = 4 # Should be equal to the number of cpu-cores.
#wsrep_sst_auth 		 = "sstuser:my5up3r53cr3tp455w0rd"

# Galera Synchronization Configuration
#wsrep_sst_method		 = rsync
#
# Galera Node Configuration
#wsrep_node_address		 = "192.168.0.107"
#wsrep_node_name		 = "Node3"
#
#EOF

#CREATE USER 'root'@'192.168.0.106' IDENTIFIED BY 'NM3z99s7cDWYEu3wzRE5srlzWFgeTjPU';
#GRANT ALL PRIVILEGES ON * . * TO 'root'@'192.168.0.106' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;
#CREATE USER 'root'@'server2.wolftronics.hu' IDENTIFIED BY 'NM3z99s7cDWYEu3wzRE5srlzWFgeTjPU';
#GRANT ALL PRIVILEGES ON * . * TO 'root'@'server2.wolftronics.hu' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
#CREATE USER 'root'@'2a01:dddd::2' IDENTIFIED BY 'NM3z99s7cDWYEu3wzRE5srlzWFgeTjPU';
#GRANT ALL PRIVILEGES ON * . * TO 'root'@'2001:db8::2' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
QUIT;

	#ufw enable
	ufw allow 3306/tcp
	ufw allow 4444/tcp
	ufw allow 4567/tcp
	ufw allow 4568/tcp
	ufw allow 4567/udp

	#mysql -u root -p$CFG_MYSQL_ROOT_PWD -e "show status like 'wsrep_cluster_size'"
    
    elif [ "$CFG_MIRRORSERVER" = "yes" ] && [ "$CFG_SQLSERVER_CLUSTER" = "replication" ] && [ "$CFG_SETUP_MASTER" = "n" ]; then
	echo "Set Up Slave Replication in MySQL..."

	#vi /etc/mysql/my.cnf
	#[mysqld]
	#server-id                = 2
	#log_bin                  = mysql-bin.log
	#expire_logs_days         = 10
	#max_binlog_size          = 100M
	#binlog_format            = mixed
	#sync_binlog              = 1
	#slave_skip_errors        = 1007,1008,1050,1396
	#replicate-same-server-id = 0
	#auto-increment-increment = 2
	#auto-increment-offset    = 2
	#relay-log                = slave-relay.log
	#relay-log-index          = slave-relay-log.index

	#Feature
        #max_allowed_packet=128M

	sed -i "s/#server-id.*/server-id              = 2/g" /etc/mysql/my.cnf
	sed -i "/max_binlog_size         = 100M/a binlog_format           = mixed" /etc/mysql/my.cnf
	sed -i "s/#sync_binlog.*/sync_binlog             = 1/g" /etc/mysql/my.cnf
	sed -i "/server-id              = 2/a replicate-same-server-id = 0" /etc/mysql/my.cnf
	#mysql
	#sed -i "s/#auto-increment-increment.*/auto-increment-increment = 2/g" /etc/mysql/my.cnf
	#sed -i "s/#auto-increment-offset.*/auto-increment-offset   = 2/g" /etc/mysql/my.cnf
	#mariadb
	sed -i "s/#auto_increment_increment.*/auto_increment_increment = 2/g" /etc/mysql/my.cnf
	sed -i "s/#auto_increment_offset.*/auto_increment_offset   = 2/g" /etc/mysql/my.cnf
	sed -i "/# slaves/a slave_skip_errors        = 1007,1008,1050,1396\nrelay_log              = \/var\/log\/mysql\/slave-relay.log" /etc/mysql/my.cnf
	sed -i "s/#relay_log_index.*/relay_log_index         = \/var\/log\/mysql\/slave-relay-log.index/g" /etc/mysql/my.cnf


    else

    restart_service mysql
    fi


------------------------
mysql!!!
-----
if [ "$CFG_MIRRORSERVER" = "yes" ] && [ "$CFG_SQLSERVER_CLUSTER" = "galera" ] && [ "$CFG_SETUP_MASTER" = "y" ]; then
	echo "Set Up Master Galera in MySQL..."
	#node1 master
	apt_install rsync
	#CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'my5up8r58cr8tp455w8rd';
	#GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
	#flush privileges;
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'my5up8r58cr8tp455w8rd';" > /dev/null 2>&1
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';" > /dev/null 2>&1
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "flush privileges;" > /dev/null 2>&1

	#openssl genrsa 2048 > ca-key.pem
	#openssl req -new -x509 -nodes -days 730 -key ca-key.pem -out ca-cert.pem
	#openssl req -newkey rsa:2048 -days 730 -nodes -keyout server-key.pem -out server-req.pem
	#openssl rsa -in server-key.pem -out server-key.pem
	#openssl x509 -req -in server-req.pem -days 730 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
	#openssl req -newkey rsa:2048 -days 730 -nodes -keyout client-key.pem -out client-req.pem
	#openssl rsa -in client-key.pem -out client-key.pem
	#openssl x509 -req -in client-req.pem -days 365000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem
	#openssl verify -CAfile ca-cert.pem server-cert.pem client-cert.pem

#	vi /etc/mysql/my.cnf
#[client]
#port            = 3306
#socket          = /var/run/mysqld/mysqld.sock
#ssl-ca=/etc/mysql/ssl/ca-cert.pem
#ssl-cert=/etc/mysql/ssl/client-cert.pem
#ssl-key=/etc/mysql/ssl/client-key.pem
#[mysqld]
#ssl-ca=/etc/mysql/ssl/ca-cert.pem
#ssl-cert=/etc/mysql/ssl/server-cert.pem
#ssl-key=/etc/mysql/ssl/server-key.pem

#Copy certificates to all other nodes:
#ssh root@192.168.0.106 mkdir -p /etc/mysql/ssl/
#scp * root@192.168.0.106:/etc/mysql/ssl/
## if you have 3rd node do above commands on 3rd node too and so on ##
#ssh root@192.168.0.107 mkdir -p /etc/mysql/ssl/
#scp * root@192.168.0.107:/etc/mysql/ssl/

	cat > /etc/mysql/conf.d/galera.cnf <<EOF
[mysqld]
binlog_format			 = ROW
default-storage-engine		 = innodb
innodb_autoinc_lock_mode	 = 2
#innodb_log_file_size		 = 100M
#innodb_file_per_table
#innodb_flush_log_at_trx_commit	 = 2
#innodb_flush_log_at_trx_commit	 = 0
#innodb_buffer_pool_size	 = 256M
#innodb_locks_unsafe_for_binlog  = 1
#innodb_doublewrite		 = 1
#query_cache_size		 = 0
#query_cache_type		 = 0
#query_cache_limit               = 128K
#query_cache_size                = 64M
#bind-address			 = 0.0.0.0  # access all ip address
bind-address			 = 192.168.0.105

# Galera Provider Configuration
wsrep_on			 = ON
#wsrep_causal_reads		 = ON
wsrep_provider			 = /usr/lib/galera/libgalera_smm.so
#wsrep_provider			 = /usr/lib64/galera/libgalera_smm.so
#wsrep_provider_options 	 = "gcache.size=512M"
#wsrep_provider_options		 = "gcache.size=256M; gcache.page_size=128M"
#SSL for Galera
#wsrep_provider_options="socket.ssl_key=/etc/mysql/ssl/server-key.pem;socket.ssl_cert=/etc/mysql/ssl/server-cert.pem;socket.ssl_ca=/etc/mysql/ssl/ca-cert.pem"

# Galera Cluster Configuration
wsrep_cluster_name		 = "galera_cluster"
#wsrep_cluster_address		 = "gcomm://192.168.0.105,192.168.0.106"
#wsrep_cluster_address		 = "gcomm://192.168.0.105,192.168.0.106,192.168.0.107"

#wsrep_slave_threads 		 = 4 # Should be equal to the number of cpu-cores.
#wsrep_sst_auth 		 = "sstuser:my5up3r53cr3tp455w0rd"

# Galera Synchronization Configuration
wsrep_sst_method		 = rsync

# Galera Node Configuration
wsrep_node_address		 = "192.168.0.105"
wsrep_node_name			 = "Node1"

EOF

	#ufw enable
	ufw allow 3306/tcp
	ufw allow 4444/tcp
	ufw allow 4567/tcp
	ufw allow 4568/tcp
	ufw allow 4567/udp
	
	#Before Galera can start, you need to ensure that MariaDB service is stopped on all the nodes.
	systemctl stop mysql
	#Now, start the Galera Cluster on Node1
	galera_new_cluster
	#restart_service mysql
	#mysql -u root -p "$CFG_MYSQL_ROOT_PWD" -e "show status like 'wsrep_cluster_size'"
	#mysql -u root -p "$CFG_MYSQL_ROOT_PWD" -e "show status like 'wsrep_%'"

	#HAProxy monitoring for Galera
#https://www.howtoforge.com/setting-up-a-high-availability-load-balancer-with-haproxy-heartbeat-on-debian-lenny
#https://www.howtoforge.com/loadbalanced_mysql_cluster_debian
 
#wget -O /usr/bin/clustercheck https://raw.githubusercontent.com/olafz/percona-clustercheck/master/clustercheck
#chmod +x /usr/bin/clustercheck
#yum -y install xinetd
#echo "mysqlchk   9200/tcp" | tee -a /etc/services
#tee /etc/xinetd.d/mysqlchk << hapcheck
## default: on
## description: mysqlchk
#service mysqlchk
#{
#disable = no
#flags = REUSE
#socket_type = stream
#port = 9200
#wait = no
#user = nobody
#server = /usr/bin/clustercheck
#log_on_failure += USERID
#only_from = 0.0.0.0/0
#per_source = UNLIMITED
#}
#hapcheck

#mysql -u root -p XXXXXXXX
#grant select on *.* to cmon@'192.168.0.%' identified by 'dshdj4fh44nofd';
#flush privileges;

#apt_install haproxy

#HAProxy configuration
#vi /etc/haproxy/haproxy.cfg
#listen galera *:3306
#         mode tcp
#         option httpchk
#         balance leastconn
#         server galera01 192.168.0.105:3306 check port 9200
#         server galera02 192.168.0.106:3306 check port 9200
#         server galera03 192.168.0.107:3306 check port 9200

#vi /etc/haproxy/haproxy.cfg:
#listen galera
#        bind 192.168.0.105:3306
#        balance source
#        mode tcp
#        option tcpka
#        option mysql-check user haproxy
#        server mariadb1 192.168.0.105:3306 check weight 1
#        server mariadb2 192.168.0.106:3306 check weight 1

#sed -i "s/ENABLED=0/ENABLED=1/g" /etc/default/haproxy
#service haproxy start
#mysql -u root -pgalera
#CREATE USER 'haproxy'@'192.168.1.1';

#load balancing in the /etc/haproxy/haproxy.cfg add
#balance roundrobin

#restart_service haproxy

#haproxy v2
#apt_install haproxy keepalived
#Edit/append the following entry in /etc/sysctl.conf:
#net.ipv4.ip_nonlocal_bind=1

#sysctl -p

#Choice 1: MASTER/BACKUP
#MASTER node’s /etc/keepalived/keepalived.conf:

#global_defs {
#  router_id <HOSTNAME>
#}
#vrrp_script haproxy {
#  script "killall -0 haproxy" 
#  interval 2
#  weight 2
#}
#vrrp_instance 50 {
#  virtual_router_id 50
#  advert_int 1
#  priority <PRIORITY>
#  state MASTER
#  interface eth1
#  virtual_ipaddress {
#    192.168.2.24 dev eth1
#  }
#  track_script {
#    haproxy
#  }
#}

#Fill in <HOSTNAME> and choose <PRIORITY> between and including 101 and 254.

#BACKUP node’s /etc/keepalived/keepalived.conf:

#global_defs {
#  router_id <HOSTNAME>
#}
#vrrp_script haproxy {
#  script "killall -0 haproxy" 
#  interval 2
#  weight 2
#}
#vrrp_instance 50 {
#  virtual_router_id 50
#  advert_int 1
#  priority <PRIORITY>
#  state BACKUP
#  interface eth1
#  virtual_ipaddress {
#    192.168.2.24 dev eth1
#  }
#  track_script {
#    haproxy
#  }
#}

#Fill in <HOSTNAME> and choose <PRIORITY> between and including 1 and 100.

#WARNING: never set the ‘priority’ of BACKUP routers higher than 100, as this will lead to them stealing the MASTER’s state regardless if the MASTER has a higher priority still!

#Choice 2: BACKUP/BACKUP (sticky VIP)
#Any node’s /etc/keepalived/keepalived.conf:

#global_defs {
#  router_id <HOSTNAME>
#}
#vrrp_script haproxy {
#  script "killall -0 haproxy" 
#  interval 2
#  weight 2
#}
#vrrp_instance 50 {
#  virtual_router_id 50
#  advert_int 1
#  nopreempt
#  priority <PRIORITY>
#  state BACKUP
#  interface eth1
#  virtual_ipaddress {
#    192.168.2.24 dev eth1
#  }
#  track_script {
#    haproxy
#  }
#}

#Fill in <HOSTNAME> and choose <PRIORITY> between and including 1 and 254.

#Configure HAProxy

#frontend mariadb
#        bind 192.168.2.24:3306
#        mode tcp
#        default_backend mariadb_galera

#backend mariadb_galera
#        balance source
#        mode tcp
#        option tcpka
#        option mysql-check user haproxy
#        server mariadb1 192.168.0.105:3306 check weight 1
#        server mariadb2 192.168.0.106:3306 check weight 1
#        server mariadb2 192.168.0.107:3306 check weight 1

#restart_service haproxy

#Galera-Maxscale
#cd /tmp
#aptitude update; aptitude -y install libaio1
#wget https://downloads.mariadb.com/files/MaxScale/2.2.0/ubuntu/dists/xenial/main/binary-amd64/maxscale-2.2.0-1.ubuntu.xenial.x86_64.deb
#dpkg -i maxscale-2.2.0-1.ubuntu.xenial.x86_64.deb
#update-rc.d maxscale defaults
#MariaDB [(none)]> CREATE USER 'maxscale'@'%' IDENTIFIED BY 'you-password-here';
#MariaDB [(none)]> GRANT SELECT ON mysql.db TO 'maxscale'@'%';
#MariaDB [(none)]> GRANT SELECT ON mysql.user TO 'maxscale'@'%';
#MariaDB [(none)]> GRANT SHOW DATABASES ON *.* TO 'maxscale'@'%';
#vi /etc/maxscale.cnf
#[maxscale]
#threads=1 #number of CPU's here
#syslog=0 #log to syslog
#maxlog=1 #log to /var/log/maxscale
#log_to_shm=1 #log to memory
#log_warning=1 #log warnings
#log_notice=1 #log notices
#log_info=0 #log info
#log_debug=0 #log full debug for dev only

#[server1] #common name
#type=server
#address=172.16.20.125 #address
#port=3306
#protocol=MySQLBackend

#[server2]
#type=server
#address=172.16.20.141
#port=3306
#protocol=MySQLBackend

#[server3]
#type=server
#address=172.16.20.97
#port=3306
#protocol=MySQLBackend

#[Galera Monitor]
#type=monitor
#module=galeramon
#servers=server1,server2,server3 #which servers should be in the pool
#user= #Add user dedicated MaxScale user
#passwd= #Add the MaxScale password
#monitor_interval=2000 #How frequent to check
#disable_master_failback=1 #Once master fails stay don't fall back to old master
#available_when_donor=1 #In Galera while in donor mode, continue to be available

#[qla]
#type=filter
#module=qlafilter
#options=/tmp/QueryLog

#[fetch]
#type=filter
#module=regexfilter
#match=fetch
#replace=select

#[RW Split Router]
#type=service
#router=readwritesplit
#servers=server1,server2,server3 #which servers should be in the pool
#user= #Same dedicated user from Galera Monitor
#passwd= #MaxScale user password
#max_slave_connections=100%
#max_slave_replication_lag=30 #The amount of time to allow the slave to be beind

#[CLI]
#type=service
#router=cli

#[RW Split Listener]
#type=listener
#service=RW Split Router
#protocol=MySQLClient
#port=3306

#[CLI Listener]
#type=listener
#service=CLI
#protocol=maxscaled
#address=127.0.0.1
#port=6603
#EOF

#maxadmin -pmariadb list servers

    fi
    if [ "$CFG_MIRRORSERVER" = "yes" ] && [ "$CFG_SQLSERVER_CLUSTER" = "replication" ] && [ "$CFG_SETUP_MASTER" = "y" ]; then
	echo "Set Up Master Replication in MySQL..."
#https://www.ryadel.com/en/replication-stops-working-analysis-resync-mysql-replication/

	#CREATE USER 'slaveuser2'@'server2.example.tld' IDENTIFIED BY 'slave_user_password';
	#CREATE USER 'slaveuser2'@'192.168.0.106' IDENTIFIED BY 'slave_user_password';
	#CREATE USER 'slaveuser2'@'2001:db8::2' IDENTIFIED BY 'slave_user_password';
	#GRANT REPLICATION SLAVE ON *.* TO 'slaveuser2'@'server2.example.tld';
	#GRANT REPLICATION SLAVE ON *.* TO 'slaveuser2'@'192.168.0.106';
	#GRANT REPLICATION SLAVE ON *.* TO 'slaveuser2'@'2001:db8::2';
	#QUIT;
	mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CREATE USER 'slaveuser2'@'server2.wolftronics.hu' IDENTIFIED BY 'NM3z89s7cDPYEu8wzRE6srlzWFgETjPu'" > /dev/null 2>&1
	mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CREATE USER 'slaveuser2'@'192.168.0.106' IDENTIFIED BY 'NM3z89s7cDPYEu8wzRE6srlzWFgETjPu'" > /dev/null 2>&1
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CREATE USER 'slaveuser2'@'2001:db8::2' IDENTIFIED BY 'slave_user_password'" > /dev/null 2>&1
	mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "GRANT REPLICATION SLAVE ON *.* TO 'slaveuser2'@'server2.wolftronics.hu'" > /dev/null 2>&1
	mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "GRANT REPLICATION SLAVE ON *.* TO 'slaveuser2'@'192.168.0.106'" > /dev/null 2>&1
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "GRANT REPLICATION SLAVE ON *.* TO 'slaveuser2'@'2001:db8::2'" > /dev/null 2>&1

	#vi /etc/mysql/my.cnf
	#[mysqld]
	#server-id                = 1
	#replicate-same-server-id = 0
	#auto-increment-increment = 2
	#auto-increment-offset  = 1
	#log_bin                 = mysql-bin.log
	#expire_logs_days        = 10
	#max_binlog_size         = 100M
	#binlog_format            = mixed
	#sync_binlog             = 1
	#relay_log              =  slave-relay.log
	#relay_log_index        = slave-relay-log.index
	#slave_skip_errors        = 1007,1008,1050, 1396
	#bind-address             = ::

	#Feature
	#binlog-ignore-db=test
	#binlog-ignore-db=information_schema
	#replicate-ignore-db=test
	#replicate-ignore-db=information_schema
        #max_allowed_packet=128M

	sed -i "s/#server-id.*/server-id              = 1/g" /etc/mysql/my.cnf
	sed -i "/server-id              = 1/a replicate-same-server-id = 0" /etc/mysql/my.cnf
	#mysql
	sed -i "s/#auto-increment-increment.*/auto-increment-increment = 2/g" /etc/mysql/my.cnf
	sed -i "s/#auto-increment-offset.*/auto-increment-offset   = 1/g" /etc/mysql/my.cnf
	#mariadb
	#sed -i "s/#auto_increment_increment.*/auto_increment_increment = 2/g" /etc/mysql/my.cnf
	#sed -i "s/#auto_increment_offset.*/auto_increment_offset   = 1/g" /etc/mysql/my.cnf
	sed -i "/max_binlog_size         = 100M/a binlog_format           = mixed" /etc/mysql/my.cnf
	sed -i "s/#sync_binlog.*/sync_binlog             = 1/g" /etc/mysql/my.cnf
	sed -i "/# slaves/a slave_skip_errors        = 1007,1008,1050,1396\nrelay_log              = \/var\/log\/mysql\/slave-relay.log" /etc/mysql/my.cnf
	sed -i "s/#relay_log_index.*/relay_log_index         = \/var\/log\/mysql\/slave-relay-log.index/g" /etc/mysql/my.cnf
	sed -i "s/#bind-address=.*/bind-address= ::/g" /etc/mysql/my.cnf

	#I simply skip some slave-errors:
	#1007 = Can't create database '%s'; database exists
	#1008 = Can't drop database '%s'; database doesn't exist  
	#1050 = Table '%s' already exists  
	#1396 = Operation %s failed for %s  - Can not create user

#mysql -u root -p [root_password]
#CREATE USER 'replicator'@'%' IDENTIFIED BY '[replicator_password]';
#GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%' IDENTIFIED BY '[replicator_password]';

#STOP SLAVE;
#CHANGE MASTER TO MASTER_HOST = 'Server A IP Address', MASTER_USER = 'replicator', MASTER_PASSWORD = '[replicator_password]', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 107; 
#START SLAVE;

	#CHANGE MASTER TO MASTER_HOST="server2.wolftronics.hu", MASTER_USER="slaveuser1", MASTER_PASSWORD="slave_user_password", MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=326;
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "SHOW MASTER STATUS \G"
	#mysql
	mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CHANGE MASTER TO MASTER_HOST="server2.wolftronics.hu", MASTER_USER="slaveuser1", MASTER_PASSWORD="slave_user_password", MASTER_LOG_FILE='/var/log/mysql/mysql-bin.000002', MASTER_LOG_POS=326;" > /dev/null 2>&1
	#mariadb
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CHANGE MASTER TO MASTER_HOST="server2.wolftronics.hu", MASTER_USER="slaveuser1", MASTER_PASSWORD="slave_user_password", MASTER_LOG_FILE='/var/log/mysql/mariadb-bin.000002', MASTER_LOG_POS=326;" > /dev/null 2>&1
	#Start the slave:
	mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "START SLAVE;" > /dev/null 2>&1
	#and check the slave-status with
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "SHOW SLAVE STATUS \G"
    fi
    if [ "$CFG_MIRRORSERVER" = "yes" ] && [ "$CFG_SQLSERVER_CLUSTER" = "galera" ] && [ "$CFG_SETUP_MASTER" = "n" ]; then
	echo "Set Up Slave Galera in MySQL..."
	#node2 slave
	apt_install rsync
        #CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'my5up8r58cr8tp455w8rd';
	#GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
	#flush privileges;
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'my5up8r58cr8tp455w8rd';" > /dev/null 2>&1
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';" > /dev/null 2>&1
	#mysql -uroot -p$CFG_MYSQL_ROOT_PWD -e "flush privileges;" > /dev/null 2>&1

	#openssl genrsa 2048 > ca-key.pem
	#openssl req -new -x509 -nodes -days 730 -key ca-key.pem -out ca-cert.pem
	#openssl req -newkey rsa:2048 -days 730 -nodes -keyout server-key.pem -out server-req.pem
	#openssl rsa -in server-key.pem -out server-key.pem
	#openssl x509 -req -in server-req.pem -days 730 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
	#openssl req -newkey rsa:2048 -days 730 -nodes -keyout client-key.pem -out client-req.pem
	#openssl rsa -in client-key.pem -out client-key.pem
	#openssl x509 -req -in client-req.pem -days 365000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem
	#openssl verify -CAfile ca-cert.pem server-cert.pem client-cert.pem

#	vi /etc/mysql/my.cnf
#[client]
#port            = 3306
#socket          = /var/run/mysqld/mysqld.sock
#ssl-ca=/etc/mysql/ssl/ca-cert.pem
#ssl-cert=/etc/mysql/ssl/client-cert.pem
#ssl-key=/etc/mysql/ssl/client-key.pem
#[mysqld]
#ssl-ca=/etc/mysql/ssl/ca-cert.pem
#ssl-cert=/etc/mysql/ssl/server-cert.pem
#ssl-key=/etc/mysql/ssl/server-key.pem

#Copy certificates to all other nodes:
#ssh root@192.168.0.106 mkdir -p /etc/mysql/ssl/
#scp * root@192.168.0.106:/etc/mysql/ssl/
## if you have 3rd node do above commands on 3rd node too and so on ##
#ssh root@192.168.0.107 mkdir -p /etc/mysql/ssl/
#scp * root@192.168.0.107:/etc/mysql/ssl/

	cat > /etc/mysql/conf.d/galera.cnf <<EOF
[mysqld]
binlog_format			 = ROW
default-storage-engine		 = innodb
innodb_autoinc_lock_mode	 = 2
#innodb_log_file_size		 = 100M
#innodb_file_per_table
#innodb_flush_log_at_trx_commit	 = 2
#innodb_flush_log_at_trx_commit	 = 0
#innodb_buffer_pool_size	 = 256M
#innodb_locks_unsafe_for_binlog  = 1
#innodb_doublewrite		 = 1
#query_cache_size		 = 0
#query_cache_type		 = 0
#query_cache_limit               = 128K
#query_cache_size                = 64M
#bind-address			 = 0.0.0.0  # access all ip address
bind-address			 = 192.168.0.106

# Galera Provider Configuration
wsrep_on			 = ON
#wsrep_causal_reads		 = ON
wsrep_provider			 = /usr/lib/galera/libgalera_smm.so
#wsrep_provider			 = /usr/lib64/galera/libgalera_smm.so
#wsrep_provider_options 	 = "gcache.size=512M"
#wsrep_provider_options		 = "gcache.size=256M; gcache.page_size=128M"
#SSL for Galera
#wsrep_provider_options="socket.ssl_key=/etc/mysql/ssl/server-key.pem;socket.ssl_cert=/etc/mysql/ssl/server-cert.pem;socket.ssl_ca=/etc/mysql/ssl/ca-cert.pem"

# Galera Cluster Configuration
wsrep_cluster_name		 = "galera_cluster"
#wsrep_cluster_address		 = "gcomm://192.168.0.105,192.168.0.106"
#wsrep_cluster_address		 = "gcomm://192.168.0.105,192.168.0.106,192.168.0.107"

#wsrep_slave_threads 		 = 4 # Should be equal to the number of cpu-cores.
#wsrep_sst_auth 		 = "sstuser:my5up3r53cr3tp455w0rd"

# Galera Synchronization Configuration
wsrep_sst_method		 = rsync

# Galera Node Configuration
wsrep_node_address="192.168.0.106"
wsrep_node_name="Node2"

EOF

	#node3 slave
	#cat > /etc/mysql/conf.d/galera.cnf <<EOF
#[mysqld]
#binlog_format			 = ROW
#default-storage-engine		 = innodb
#innodb_autoinc_lock_mode	 = 2
#innodb_log_file_size		 = 100M
#innodb_file_per_table
#innodb_flush_log_at_trx_commit	 = 2
#innodb_flush_log_at_trx_commit	 = 0
#innodb_buffer_pool_size	 = 256M
#innodb_locks_unsafe_for_binlog  = 1
#innodb_doublewrite		 = 1
#query_cache_size		 = 0
#query_cache_type		 = 0
#query_cache_limit               = 128K
#query_cache_size                = 64M
#bind-address			 = 0.0.0.0  # access all ip address
#bind-address			 = 192.168.0.107
# Galera Provider Configuration
#wsrep_on			 = ON
#wsrep_causal_reads		 = ON
#wsrep_provider			 = /usr/lib/galera/libgalera_smm.so
#wsrep_provider			 = /usr/lib64/galera/libgalera_smm.so
#wsrep_provider_options 	 = "gcache.size=512M"
#wsrep_provider_options		 = "gcache.size=256M; gcache.page_size=128M"
#SSL for Galera
#wsrep_provider_options="socket.ssl_key=/etc/mysql/ssl/server-key.pem;socket.ssl_cert=/etc/mysql/ssl/server-cert.pem;socket.ssl_ca=/etc/mysql/ssl/ca-cert.pem"

# Galera Cluster Configuration
#wsrep_cluster_name		 = "galera_cluster"
#wsrep_cluster_address		 = "gcomm://192.168.0.105,192.168.0.106"
#wsrep_cluster_address		 = "gcomm://192.168.0.105,192.168.0.106,192.168.0.107"

#wsrep_slave_threads 		 = 4 # Should be equal to the number of cpu-cores.
#wsrep_sst_auth 		 = "sstuser:my5up3r53cr3tp455w0rd"

# Galera Synchronization Configuration
#wsrep_sst_method		 = rsync
#
# Galera Node Configuration
#wsrep_node_address="192.168.0.107"
#wsrep_node_name="Node3"
#
#EOF

	#ufw enable
	ufw allow 3306/tcp
	ufw allow 4444/tcp
	ufw allow 4567/tcp
	ufw allow 4568/tcp
	ufw allow 4567/udp

	#mysql -u root -p "$CFG_MYSQL_ROOT_PWD" -e "show status like 'wsrep_cluster_size'"
	#mysql -u root -p "$CFG_MYSQL_ROOT_PWD" -e "show status like 'wsrep_%'"

    fi
    if [ "$CFG_MIRRORSERVER" = "yes" ] && [ "$CFG_SQLSERVER_CLUSTER" = "replication" ] && [ "$CFG_SETUP_MASTER" = "n" ]; then
   	echo "Set Up Slave Replication in MySQL..."
#https://www.ryadel.com/en/replication-stops-working-analysis-resync-mysql-replication/

	#vi /etc/mysql/my.cnf
	#[mysqld]
	#server-id                = 2
	#log_bin                  = mysql-bin.log
	#expire_logs_days         = 10
	#max_binlog_size          = 100M
	#binlog_format            = mixed
	#sync_binlog              = 1
	#slave_skip_errors        = 1007,1008,1050,1396
	#replicate-same-server-id = 0
	#auto-increment-increment = 2
	#auto-increment-offset    = 2
	#relay-log                = slave-relay.log
	#relay-log-index          = slave-relay-log.index

	#Feature
	#binlog-ignore-db=test
	#binlog-ignore-db=information_schema
	#replicate-ignore-db=test
	#replicate-ignore-db=information_schema
        #max_allowed_packet=128M

	sed -i "s/#server-id.*/server-id              = 2/g" /etc/mysql/my.cnf
	sed -i "/max_binlog_size         = 100M/a binlog_format           = mixed" /etc/mysql/my.cnf
	sed -i "s/#sync_binlog.*/sync_binlog             = 1/g" /etc/mysql/my.cnf
	sed -i "/server-id              = 2/a replicate-same-server-id = 0" /etc/mysql/my.cnf
	#mysql
	sed -i "s/#auto-increment-increment.*/auto-increment-increment = 2/g" /etc/mysql/my.cnf
	sed -i "s/#auto-increment-offset.*/auto-increment-offset   = 2/g" /etc/mysql/my.cnf
	#mariadb
	#sed -i "s/#auto_increment_increment.*/auto_increment_increment = 2/g" /etc/mysql/my.cnf
	#sed -i "s/#auto_increment_offset.*/auto_increment_offset   = 2/g" /etc/mysql/my.cnf
	sed -i "/# slaves/a slave_skip_errors        = 1007,1008,1050,1396\nrelay_log              = \/var\/log\/mysql\/slave-relay.log" /etc/mysql/my.cnf
	sed -i "s/#relay_log_index.*/relay_log_index         = \/var\/log\/mysql\/slave-relay-log.index/g" /etc/mysql/my.cnf


#mysql -u root -p [root_password]
#CREATE USER 'replicator'@'%' IDENTIFIED BY '[replicator_password]';
#GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%' IDENTIFIED BY '[replicator_password]';
#STOP SLAVE;
#CHANGE MASTER TO MASTER_HOST = 'Server B IP Address', MASTER_USER = 'replicator', MASTER_PASSWORD = 'replicator_password', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 107; 
#START SLAVE;

    fi
    restart_service mysql
