go get github.com/tencentcloud/ssm-rotation-sdk-golang/lib
import (_ "github.com/go-sql-driver/mysql""github.com/tencentcloud/ssm-rotation-sdk-golang/lib/db""github.com/tencentcloud/ssm-rotation-sdk-golang/lib/ssm")
dbConn := &db.DynamicSecretRotationDb{}err := dbConn.Init(&db.Config{DbConfig: &db.DbConfig{MaxOpenConns: 100,MaxIdleConns: 50,IdleTimeoutSeconds: 100,ReadTimeoutSeconds: 5,WriteTimeoutSeconds: 5,SecretName: "test", // Secret nameIpAddress: "127.0.0.1", // Access address of the database instancePort: 58366, // Access port of the database instanceDbName: "database_name", // Database nameParamStr: "charset=utf8&loc=Local", // Database connection configuration parameters},SsmServiceConfig: &ssm.SsmAccount{SecretId: os.Getenv("SECRET_ID"), // SecretId of the sub-accountSecretKey: os.Getenv("SECRET_KEY"), // SecretKey of the sub-accountRegion: "ap-guangzhou", // Region where the secret resides},WatchChangeInterval: time.Second * 10, // Monitoring interval. Recommended value: 10–60 seconds})if err != nil {log.Fatalf("failed to initialize SSM SDK: %v", err)}defer func() {if err := dbConn.Close(); err != nil {log.Printf("failed to close SSM SDK: %v", err)}}()
c := dbConn.GetConn()// Note: Call GetConn() to obtain the latest connection pool object each time you need to access the database. Do not cache this object in the business code for a long time, as this may cause database access failures!if c == nil {log.Fatal("failed to get database connection")}if err := c.Ping(); err != nil {log.Fatalf("failed to access database: %v", err)}// In this example, the ping() method is called only to test the availability of the account and password. In actual business scenarios, you can perform specific database operations here.// Perform specific database operations here.
c.Close() // When the application exits, you can proactively close the database connection. This is a common operation and has no direct correlation with the database credentials
pip install ssm-rotation-sdk
import osimport loggingfrom ssm_rotation_sdk import (DynamicSecretRotationDb,Config,DbConfig,SsmAccount,)WATCH_FREQ = 10 # Credential monitoring interval. Recommended value: 10–60 secondsdb_config = DbConfig(params={'secret_name': 'test', # Secret name'ip_address': '127.0.0.1', # Database address'port': 58366, # Database port'db_name': 'database_name', # Specify the specific database name. If it is empty, only connect to the database instance without connecting to a specific database'param_str': 'charset=utf8&loc=Local',})ssm_service_config = SsmAccount(params={'secret_id': os.getenv('SECRET_ID'), # Enter the actual available sub-account SecretId'secret_key': os.getenv('SECRET_KEY'), # Enter the actual available sub-account SecretKey'region': 'ap-guangzhou', # Region where the secret resides})config = Config(params={'db_config': db_config,'ssm_service_config': ssm_service_config,'WATCH_FREQ': WATCH_FREQ,})
db_conn = DynamicSecretRotationDb()err = db_conn.init(config)if err:raise RuntimeError("Failed to initialize SSM SDK: {}".format(err))
c = db_conn.get_conn()# Note: Call GetConn() to obtain the latest DB connection each time you need to access the database. Do not cache this object in the business code, as this may cause DB access failures!if c is None:logging.error("Failed to get database connection")else:try:c.ping(reconnect=True) # In this example, the ping() method is called only to test the availability of the account and password# Perform specific database operations here.except Exception:logging.exception("Failed to access database")raisefinally:c.close() # Return the connection to the connection pool
c.close() # When the application exits, you can proactively close the database connection. This is a common operation and has no direct correlation with the database credentials
フィードバック