1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure Database for MySQLでレプリケーションをしたい

Posted at

Azure Database for MySQLでmysql8.0をレプリケーションしたコードをTerraformで書きました。

main.tf
provider "azurerm" {
  features {}
  subscription_id = "" # AzureサブスクリプションIDを指定
}

# リソースグループの作成
resource "azurerm_resource_group" "example" {
  name     = "mysql-replication-rg"
  location = "Japan East" # 必要に応じてリージョンを変更
}

# プライマリMySQLサーバーの作成
resource "azurerm_mysql_flexible_server" "master" {
  name                   = "mysql-master-server"
  resource_group_name    = azurerm_resource_group.example.name
  location               = azurerm_resource_group.example.location
  administrator_login    = "mysqladmin"
  administrator_password = "SecurePassword123!" # 強力なパスワードを使用
  sku_name               = "GP_Standard_D2ds_v4"    
  version                = "8.0.21"             # サポートされているバージョン
  zone                   = "2"                   # 可用性ゾーンを明示(例: "1", "2", "3")

  # ストレージ設定
  storage {
    size_gb = 20  # 20GB(最小値)
  }

  # バックアップ設定(オプション)
  backup_retention_days = 7
}

# レプリカMySQLサーバーの作成
resource "azurerm_mysql_flexible_server" "replica" {
  name                   = "mysql-replica-server"
  resource_group_name    = azurerm_resource_group.example.name
  location               = azurerm_resource_group.example.location
  create_mode            = "Replica"              # レプリカモードを明示
  source_server_id       = azurerm_mysql_flexible_server.master.id # プライマリを参照
  sku_name               = "GP_Standard_D2ds_v4"  #B1MSではレプリケーションできない 
  version                = "8.0.21"              # プライマリと一致
  zone                   = "2"                   # 可用性ゾーンを明示(例: "1", "2", "3")

  depends_on = [azurerm_mysql_flexible_server.master] # 依存関係を明示
}

# 出力設定(サーバーのFQDNを確認)
output "master_server_fqdn" {
  value = azurerm_mysql_flexible_server.master.fqdn
}

output "replica_server_fqdn" {
  value = azurerm_mysql_flexible_server.replica.fqdn
}

注意点
AzureサブスクリプションIDをいれてください。
コメントでも書いていますがB1MSではレプリケーションできないので気を付けてください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?