資料庫遷移,為什麼重要?
您是否曾經遇到過這樣的情況:當您使用更新的資料庫架構在生產環境中部署新的更新時,但之後出現錯誤並需要恢復內容......這就是遷移出現的情況。
資料庫遷移有幾個關鍵目的:
- 架構演變:隨著應用程式的演變,它們的資料模型也會改變。遷移允許開發人員系統地更新資料庫架構以反映這些更改,確保資料庫結構與應用程式程式碼相符。
- 版本控制:遷移提供了一種對資料庫架構進行版本控制的方法,允許團隊追蹤一段時間內的變更。此版本控制有助於理解資料庫的演變並有助於開發人員之間的協作。
- 跨環境的一致性:遷移確保資料庫架構在不同環境(開發、測試、生產)中保持一致。這降低了可能導致錯誤和整合問題的差異風險。
- 回滾功能:許多遷移工具都支援回滾更改,允許開發人員在遷移導致問題時恢復到資料庫之前的狀態。這增強了開發和部署過程中的穩定性。
- 自動部署:遷移可以作為部署過程的一部分自動化,確保將必要的架構變更應用於資料庫,而無需手動幹預。這簡化了發布流程並減少了人為錯誤。
在golang專案中應用
要使用 GORM 和 MySQL 為 Golang 服務建立全面的生產級設置,以便輕鬆遷移、更新和回滾,您需要包含遷移工具、處理資料庫連接池並確保正確的結構定義。這是一個完整的範例來引導您完成整個過程:
專案結構
/golang-service |-- main.go |-- database | |-- migration.go |-- models | |-- user.go |-- config | |-- config.go |-- migrations | |-- ... |-- go.mod
1.資料庫配置(config/config.go)
package config import ( "fmt" "log" "os" "time" "github.com/joho/godotenv" "gorm.io/driver/mysql" "gorm.io/gorm" ) var DB *gorm.DB func ConnectDB() { err := godotenv.Load() if err != nil { log.Fatal("Error loading .env file") } // charset=utf8mb4: Sets the character set to utf8mb4, which supports all Unicode characters, including emojis. // parseTime=True: Tells the driver to automatically parse DATE and DATETIME values into Go's time.Time type. // loc=Local: Uses the local timezone of the server for time-related queries and storage. dsn := fmt.Sprintf( "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", os.Getenv("DB_USER"), os.Getenv("DB_PASS"), os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_NAME"), ) db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { panic("failed to connect database") } sqlDB, err := db.DB() if err != nil { panic("failed to configure database connection") } // Set connection pool settings sqlDB.SetMaxIdleConns(10) sqlDB.SetMaxOpenConns(100) sqlDB.SetConnMaxLifetime(time.Hour) // 1.sqlDB.SetMaxIdleConns(10) // Sets the maximum number of idle (unused but open) connections in the connection pool. // A value of 10 means up to 10 connections can remain idle, ready to be reused. // 2. sqlDB.SetMaxOpenConns(100): // Sets the maximum number of open (active or idle) connections that can be created to the database. // A value of 100 limits the total number of connections, helping to prevent overloading the database. // 3. sqlDB.SetConnMaxLifetime(time.Hour): // Sets the maximum amount of time a connection can be reused before it’s closed. // A value of time.Hour means that each connection will be kept for up to 1 hour, after which it will be discarded and a new connection will be created if needed. DB = db }
2. 資料庫遷移(database/migration.go)
package database import ( "golang-service/models" "golang-service/migrations" "gorm.io/gorm" ) func Migrate(db *gorm.DB) { db.AutoMigrate(&models.User{}) // Apply additional custom migrations if needed }
3.模型(models/user.go)
package models import "gorm.io/gorm" type User struct { gorm.Model Name string `json:"name"` }
4.環境配置(.env)
DB_USER=root DB_PASS=yourpassword DB_HOST=127.0.0.1 DB_PORT=3306 DB_NAME=yourdb
5. 主入口點(main.go)
package main import ( "golang-service/config" "golang-service/database" "golang-service/models" "github.com/gin-gonic/gin" "gorm.io/gorm" ) func main() { config.ConnectDB() database.Migrate(config.DB) r := gin.Default() r.POST("/users", createUser) r.GET("/users/:id", getUser) r.Run(":8080") } func createUser(c *gin.Context) { var user models.User if err := c.ShouldBindJSON(&user); err != nil { c.JSON(400, gin.H{"error": err.Error()}) return } if err := config.DB.Create(&user).Error; err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } c.JSON(201, user) } func getUser(c *gin.Context) { id := c.Param("id") var user models.User if err := config.DB.First(&user, id).Error; err != nil { c.JSON(404, gin.H{"error": "User not found"}) return } c.JSON(200, user) }
6、說明:
- 資料庫配置:管理連接池以獲得生產級效能。
- 遷移檔案:(在遷移資料夾中)幫助資料庫架構進行版本控制。
- GORM 模型:將資料庫表對應到 Go 結構。
- 資料庫遷移:(在資料庫資料夾中)隨著時間的推移更改表的自訂邏輯,以便輕鬆回滾。
- 測試:您可以使用 httptest 和 testify 為此設定建立整合測試。
7. 建立第一個遷移
-
對於生產環境,我們可以使用像 golang-migrate 這樣的遷移程式庫來應用、回滾或重做遷移。
安裝golang-migrate:
go install -tags 'mysql' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
-
為使用者表產生遷移檔案
migrate create -ext=sql -dir=./migrations -seq create_users_table
運行指令後,我們將得到一對 .up.sql (用於更新架構)和 down.sql (用於稍後可能的回滾)。數字000001是自動產生的遷移索引。
/golang-service |-- migrations | |-- 000001_create_users_table.down.sql | |-- 000001_create_users_table.up.sql
新增相關sql指令到.up檔和.down檔。
000001_create_users_table.up.sql
CREATE TABLE users ( id BIGINT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, created_at DATETIME, updated_at DATETIME, deleted_at DATETIME);
000001_create_users_table.down.sql
DROP TABLE IF EXISTS users;
運行向上遷移並使用以下命令將變更套用到資料庫(-verbose 標誌以查看更多日誌詳細資訊):
migrate -path ./migrations -database "mysql://user:password@tcp(localhost:3306)/dbname" -verbose up
如果我們遇到遷移問題,我們可以使用以下命令查看目前的遷移版本及其狀態:
migrate -path ./migrations -database "mysql://user:password@tcp(localhost:3306)/dbname" version
如果因為某些原因導致遷移失敗,我們可以考慮使用強制(謹慎使用)指令以及髒遷移的版本號。如果版本是1(可以在migrations或schema_migrations表中檢查),我們將運行:
migrate -path ./migrations -database "mysql://user:password@tcp(localhost:3306)/dbname" force 1
8.改變計劃
-
在某個時間點,我們可能想要新增功能,其中一些可能需要更改資料方案,例如我們想要在使用者表格中新增電子郵件欄位。我們將按照以下方式進行。
進行新的遷移,將電子郵件列新增至使用者表
migrate create -ext=sql -dir=./migrations -seq add_email_to_users
現在我們有了一對新的 .up.sql 和 .down.sql
/golang-service |-- migrations | |-- 000001_create_users_table.down.sql | |-- 000001_create_users_table.up.sql | |-- 000002_add_email_to_users.down.sql | |-- 000002_add_email_to_users.up.sql
-
將以下內容加入*_add_email_to_users.*.sql檔案
000002_add_email_to_users.up.sql
/golang-service |-- main.go |-- database | |-- migration.go |-- models | |-- user.go |-- config | |-- config.go |-- migrations | |-- ... |-- go.mod
000002_add_email_to_users.down.sql
package config import ( "fmt" "log" "os" "time" "github.com/joho/godotenv" "gorm.io/driver/mysql" "gorm.io/gorm" ) var DB *gorm.DB func ConnectDB() { err := godotenv.Load() if err != nil { log.Fatal("Error loading .env file") } // charset=utf8mb4: Sets the character set to utf8mb4, which supports all Unicode characters, including emojis. // parseTime=True: Tells the driver to automatically parse DATE and DATETIME values into Go's time.Time type. // loc=Local: Uses the local timezone of the server for time-related queries and storage. dsn := fmt.Sprintf( "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", os.Getenv("DB_USER"), os.Getenv("DB_PASS"), os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_NAME"), ) db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { panic("failed to connect database") } sqlDB, err := db.DB() if err != nil { panic("failed to configure database connection") } // Set connection pool settings sqlDB.SetMaxIdleConns(10) sqlDB.SetMaxOpenConns(100) sqlDB.SetConnMaxLifetime(time.Hour) // 1.sqlDB.SetMaxIdleConns(10) // Sets the maximum number of idle (unused but open) connections in the connection pool. // A value of 10 means up to 10 connections can remain idle, ready to be reused. // 2. sqlDB.SetMaxOpenConns(100): // Sets the maximum number of open (active or idle) connections that can be created to the database. // A value of 100 limits the total number of connections, helping to prevent overloading the database. // 3. sqlDB.SetConnMaxLifetime(time.Hour): // Sets the maximum amount of time a connection can be reused before it’s closed. // A value of time.Hour means that each connection will be kept for up to 1 hour, after which it will be discarded and a new connection will be created if needed. DB = db }
再次執行向上遷移指令以更新資料模式
package database import ( "golang-service/models" "golang-service/migrations" "gorm.io/gorm" ) func Migrate(db *gorm.DB) { db.AutoMigrate(&models.User{}) // Apply additional custom migrations if needed }
我們還需要更新 golang users 結構(新增 Email 欄位)以使其與新模式保持同步..
package models import "gorm.io/gorm" type User struct { gorm.Model Name string `json:"name"` }
9. 回滾遷移:
如果因為某些原因我們在新更新的模式中出現錯誤,並且需要回滾,這種情況下我們將使用 down 指令:
DB_USER=root DB_PASS=yourpassword DB_HOST=127.0.0.1 DB_PORT=3306 DB_NAME=yourdb
數字 1 表示我們要回滾 1 個遷移。
這裡我們還需要手動更新 golang users 結構(刪除 Email 欄位)以反映資料架構變更。
package main import ( "golang-service/config" "golang-service/database" "golang-service/models" "github.com/gin-gonic/gin" "gorm.io/gorm" ) func main() { config.ConnectDB() database.Migrate(config.DB) r := gin.Default() r.POST("/users", createUser) r.GET("/users/:id", getUser) r.Run(":8080") } func createUser(c *gin.Context) { var user models.User if err := c.ShouldBindJSON(&user); err != nil { c.JSON(400, gin.H{"error": err.Error()}) return } if err := config.DB.Create(&user).Error; err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } c.JSON(201, user) } func getUser(c *gin.Context) { id := c.Param("id") var user models.User if err := config.DB.First(&user, id).Error; err != nil { c.JSON(404, gin.H{"error": "User not found"}) return } c.JSON(200, user) }
10. 與 Makefile 一起使用
為了簡化遷移和回滾的過程,我們可以新增一個Makefile。
go install -tags 'mysql' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
Makefile 內容如下。
migrate create -ext=sql -dir=./migrations -seq create_users_table
現在我們只需在 CLI 上執行 make migrate_up 或 make migrate_down 即可進行遷移和回滾。
11.注意事項:
- 回滾期間資料遺失:回滾刪除列或表的移轉可能會導致資料遺失,因此在執行回溯之前始終備份資料。
- CI/CD 整合:將遷移過程整合到 CI/CD 管道中,以在部署期間自動執行架構變更。
- 資料庫備份:安排定期資料庫備份,以防止遷移錯誤時遺失資料。
關於資料庫備份
在回滾遷移或進行可能影響資料庫的變更之前,以下是需要考慮的一些關鍵點。
- 架構更改:如果遷移涉及更改架構(例如,新增或刪除列、更改資料類型),則回滾到先前的遷移可能會導致儲存在這些變更的列或表中的任何資料遺失.
- 資料刪除:如果遷移包含刪除資料的命令(例如刪除表或截斷表),則回滾將執行相應的「向下」遷移,這可能會永久刪除該資料。
- 事務處理:如果您的遷移工具支援事務,則回滾可能會更安全,因為變更是在事務中套用的。但是,如果您在交易之外手動執行 SQL 命令,則存在遺失資料的風險。
- 資料完整性:如果您以取決於目前架構的方式修改了數據,則回滾可能會使您的資料庫處於不一致的狀態。
所以備份資料至關重要。這是一個簡短的指南:
-
資料庫轉儲:
使用特定於資料庫的工具建立資料庫的完整備份。對於 MySQL,您可以使用:
/golang-service |-- main.go |-- database | |-- migration.go |-- models | |-- user.go |-- config | |-- config.go |-- migrations | |-- ... |-- go.mod
這將建立一個檔案 (backup_before_rollback.sql),其中包含 dbname 資料庫的所有資料和架構。
-
匯出特定表:
如果您只需要備份某些表,請在 mysqldump 指令中指定它們:
package config import ( "fmt" "log" "os" "time" "github.com/joho/godotenv" "gorm.io/driver/mysql" "gorm.io/gorm" ) var DB *gorm.DB func ConnectDB() { err := godotenv.Load() if err != nil { log.Fatal("Error loading .env file") } // charset=utf8mb4: Sets the character set to utf8mb4, which supports all Unicode characters, including emojis. // parseTime=True: Tells the driver to automatically parse DATE and DATETIME values into Go's time.Time type. // loc=Local: Uses the local timezone of the server for time-related queries and storage. dsn := fmt.Sprintf( "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", os.Getenv("DB_USER"), os.Getenv("DB_PASS"), os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_NAME"), ) db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { panic("failed to connect database") } sqlDB, err := db.DB() if err != nil { panic("failed to configure database connection") } // Set connection pool settings sqlDB.SetMaxIdleConns(10) sqlDB.SetMaxOpenConns(100) sqlDB.SetConnMaxLifetime(time.Hour) // 1.sqlDB.SetMaxIdleConns(10) // Sets the maximum number of idle (unused but open) connections in the connection pool. // A value of 10 means up to 10 connections can remain idle, ready to be reused. // 2. sqlDB.SetMaxOpenConns(100): // Sets the maximum number of open (active or idle) connections that can be created to the database. // A value of 100 limits the total number of connections, helping to prevent overloading the database. // 3. sqlDB.SetConnMaxLifetime(time.Hour): // Sets the maximum amount of time a connection can be reused before it’s closed. // A value of time.Hour means that each connection will be kept for up to 1 hour, after which it will be discarded and a new connection will be created if needed. DB = db }
驗證備份:
確保備份檔案已建立並檢查其大小或開啟它以確保它包含必要的資料。安全地儲存備份:
將備份副本保存在安全位置,例如雲端儲存或單獨的伺服器,以防止回溯過程中資料遺失。
雲端備份
要在使用 Golang 並部署在 AWS EKS 上時備份 MySQL 數據,您可以按照以下步驟操作:
-
使用mysqldump進行資料庫備份:
使用 Kubernetes cron 作業建立 MySQL 資料庫的 mysqldump。
package database import ( "golang-service/models" "golang-service/migrations" "gorm.io/gorm" ) func Migrate(db *gorm.DB) { db.AutoMigrate(&models.User{}) // Apply additional custom migrations if needed }
將其儲存在持久性磁碟區或 S3 儲存桶中。
-
使用 Kubernetes CronJob 實現自動化:
使用 Kubernetes CronJob 自動化 mysqldump 流程。
YAML 設定範例:yaml
package models import "gorm.io/gorm" type User struct { gorm.Model Name string `json:"name"` }
` 使用 AWS RDS 自動備份(如果使用 RDS):
如果您的 MySQL 資料庫位於 AWS RDS 上,您可以利用 RDS 自動備份和快照。
設定備份保留期並手動拍攝快照或使用 Lambda 函數自動拍攝快照。使用 Velero 備份持久卷 (PV):
使用 Kubernetes 備份工具 Velero 來備份保存 MySQL 資料的持久卷。
在您的 EKS 叢集上安裝 Velero 並將其配置為備份到 S3。
透過使用這些方法,您可以確保您的 MySQL 資料得到定期備份和安全儲存。
以上是資料庫遷移對於 Golang 服務,為什麼重要?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

MySQL和SQLite的主要區別在於設計理念和使用場景:1.MySQL適用於大型應用和企業級解決方案,支持高性能和高並發;2.SQLite適合移動應用和桌面軟件,輕量級且易於嵌入。

MySQL中的索引是數據庫表中一列或多列的有序結構,用於加速數據檢索。 1)索引通過減少掃描數據量提升查詢速度。 2)B-Tree索引利用平衡樹結構,適合範圍查詢和排序。 3)創建索引使用CREATEINDEX語句,如CREATEINDEXidx_customer_idONorders(customer_id)。 4)複合索引可優化多列查詢,如CREATEINDEXidx_customer_orderONorders(customer_id,order_date)。 5)使用EXPLAIN分析查詢計劃,避

在MySQL中使用事務可以確保數據一致性。 1)通過STARTTRANSACTION開始事務,執行SQL操作後用COMMIT提交或ROLLBACK回滾。 2)使用SAVEPOINT可以設置保存點,允許部分回滾。 3)性能優化建議包括縮短事務時間、避免大規模查詢和合理使用隔離級別。

選擇PostgreSQL而非MySQL的場景包括:1)需要復雜查詢和高級SQL功能,2)要求嚴格的數據完整性和ACID遵從性,3)需要高級空間功能,4)處理大數據集時需要高性能。 PostgreSQL在這些方面表現出色,適合需要復雜數據處理和高數據完整性的項目。

MySQL數據庫的安全可以通過以下措施實現:1.用戶權限管理:通過CREATEUSER和GRANT命令嚴格控制訪問權限。 2.加密傳輸:配置SSL/TLS確保數據傳輸安全。 3.數據庫備份和恢復:使用mysqldump或mysqlpump定期備份數據。 4.高級安全策略:使用防火牆限制訪問,並啟用審計日誌記錄操作。 5.性能優化與最佳實踐:通過索引和查詢優化以及定期維護兼顧安全和性能。

如何有效監控MySQL性能?使用mysqladmin、SHOWGLOBALSTATUS、PerconaMonitoringandManagement(PMM)和MySQLEnterpriseMonitor等工具。 1.使用mysqladmin查看連接數。 2.用SHOWGLOBALSTATUS查看查詢數。 3.PMM提供詳細性能數據和圖形化界面。 4.MySQLEnterpriseMonitor提供豐富的監控功能和報警機制。

MySQL和SQLServer的区别在于:1)MySQL是开源的,适用于Web和嵌入式系统,2)SQLServer是微软的商业产品,适用于企业级应用。两者在存储引擎、性能优化和应用场景上有显著差异,选择时需考虑项目规模和未来扩展性。

在需要高可用性、高級安全性和良好集成性的企業級應用場景下,應選擇SQLServer而不是MySQL。 1)SQLServer提供企業級功能,如高可用性和高級安全性。 2)它與微軟生態系統如VisualStudio和PowerBI緊密集成。 3)SQLServer在性能優化方面表現出色,支持內存優化表和列存儲索引。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

記事本++7.3.1
好用且免費的程式碼編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),