import ("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", // Credential nameIpAddress: "127.0.0.1", // Access address of the database instancePort: 58366, // Access port of the database instanceDbName: "database_name", // Specifies the specific database name; if empty, only connects to the database instance without connecting to a specific databaseParamStr: "charset=utf8&loc=Local", // Database connection configuration parameters},SsmServiceConfig: &ssm.SsmAccount{SecretId: os.Getenv("secret_id"), // Sub-account's SecretIdSecretKey: os.Getenv("secret_key"), // Sub-account's SecretKeyRegion: "ap-guangzhou", // Select the region where the credentials are actually stored. Please fill in according to the actual situation. The example given is the Guangzhou region.},WatchChangeInterval: time.Second * 10, // Interval for monitoring changes in credential content. Typically, the interval is set between 10 seconds and 60 seconds})
```golangc := dbConn.GetConn() // Warning: You must call GetConn() to obtain the latest DB connection every time you access the database. Do not cache this object in your business code to avoid DB access failures!if err := c.Ping(); err != nil { // This example simply uses the ping() method to test the availability of the account password. In actual business, you can perform specific db operations here.log.Fatal("failed to access db with err: ", err)return}```
```golangc.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```
```shellpip install -r requirements.txt```
```pythonWATCH_FREQ = 10 # Interval for monitoring changes in credential content, usually set between 10 to 60 secondsdb_config = DbConfig(params={'secret_name': "test", # Credential name'ip_address': "127.0.0.1", # Database address'port': 58366, # Database port'db_name': "database_name", # Specify the actual database name, if empty, then only connect to the database instance, not a specific database'param_str': "charset=utf8&loc=Local",})ssm_service_config = SsmAccount(params={'secret_id': os.getenv('SECRET_ID'), # You must fill in the actual usable SecretId'secret_key': os.getenv('SECRET_KEY'), # You must fill in the actual usable SecretKey'url': test_url,'region': "ap-guangzhou" # Region where the credential is stored})config = Config(params={'db_config': db_config,'ssm_service_config': ssm_service_config,'WATCH_FREQ': WATCH_FREQ})```
```pythondb_conn = DynamicSecretRotationDb()err = db_conn.init(config)```
```pythonc = db_conn.get_conn() # Warning: You must call GetConn() to obtain the most recent DB connection every time you access the database. Do not cache this object in your business code to avoid DB access failures!try:c.ping() # This example simply uses the ping() method to test the availability of the account password. In actual business, you can perform specific DB operations here.except TencentCloudSDKException as e:logging.error("failed to access db with err: {0}".format(str(e.args[0])).encode("utf-8"))```
```pythonc.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```
피드백