Skip to content

Instantly share code, notes, and snippets.

@Yamakiroshi
Created February 16, 2018 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yamakiroshi/d334c3648dd6a6954da21aba9ddb2043 to your computer and use it in GitHub Desktop.
Save Yamakiroshi/d334c3648dd6a6954da21aba9ddb2043 to your computer and use it in GitHub Desktop.
resource "digitalocean_droplet" "mysql" {
count = 3
image = "ubuntu-16-04-x64"
name = "mysql-${count.index}"
region = "lon1"
size = "s-1vcpu-1gb"
private_networking = true
ssh_keys = [
"${var.ssh_fingerprint}"
]
connection {
user = "root"
type = "ssh"
private_key = "${file(var.pvt_key)}"
timeout = "2m"
}
provisioner "remote-exec" {
inline = [
#install mysql
#"curl -OL https://dev.mysql.com/get/mysql-apt-config_0.8.9-1_all.deb",
#"sudo dpkg -i mysql-apt-config*",
"sudo apt update",
#"rm mysql-apt-config*",
"sudo apt upgrade -y",
"sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password ${var.mysql_password}'",
"sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password ${var.mysql_password}'",
"sudo apt -y install mysql-server",
"sudo wget https://gist.githubusercontent.com/Yamakiroshi/bb39a8fb9d591b3297331e704da7c88a/raw/05f2dd885f3e2ad629a253755e51f5cfd18f84eb/my.cnf -O /etc/mysql/my.cnf",
"sudo sed -i 's/MYSQL_GROUP_UUID/${var.mysql_group_uuid}/g' /etc/mysql/my.cnf",
"sudo sed -i 's/MYSQL_SERVER_1/$(digitalocean_droplet.mysql-1.ipv4_address_private)/g' /etc/mysql/my.cnf",
"sudo sed -i 's/MYSQL_SERVER_2/$(digitalocean_droplet.mysql-2.ipv4_address_private)/g' /etc/mysql/my.cnf",
"sudo sed -i 's/MYSQL_SERVER_3/$(digitalocean_droplet.mysql-3.ipv4_address_private)/g' /etc/mysql/my.cnf",
"sudo sed -i 's/MYSQL_SERVER_ID/${count.index}/g' /etc/mysql/my.cnf",
"sudo sed -i 's/MYSQL_PRIVATE_ADDRESS/$(digitalocean_droplet.mysql-$(count.index).ipv4_address_private)/g' /etc/mysql/my.cnf",
"sudo systemctl restart mysql",
"sudo ufw allow 33061",
"sudo ufw allow 3306",
"mysql -u root -p${var.mysql_password} -Bse SET SQL_LOG_BIN=0;CREATE USER 'repl'@'%' IDENTIFIED BY '$(var.mysql_password)' REQUIRE SSL;GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';FLUSH PRIVILEGES;SET SQL_LOG_BIN=1;CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='${var.mysql_password}' FOR CHANNEL 'group_replication_recovery';INSTALL PLUGIN group_replication SONAME 'group_replication.so';SET GLOBAL group_replication_bootstrap_group=ON;START GROUP_REPLICATION;SET GLOBAL group_replication_bootstrap_group=OFF;"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment