Environment requirements
OceanBase Shell (obshell) in the environment must be running.
Procedure
Install obshell-sdk-go.
go get github.com/oceanbase/obshell-sdk-goCreate a client instance.
You can create a client instance of a specified version or a set of client instances of different versions.
Create a client instance of a specified versionCreate a set of client instances of different versionspackage main import ( "github.com/oceanbase/obshell-sdk-go/services/v1" ) func main() { client, err := v1.NewClientWithPassword("10.10.10.1", 2886, "${password}") if err != nil { // Handle exceptions panic(err) } }package main import ( "github.com/oceanbase/obshell-sdk-go/services" ) func main() { clientset, err := services.NewClientWithPassword("10.10.10.1", 2886, "${password}") if err != nil { // Handle exceptions panic(err) } }In this example,
10.10.10.1and2886are respectively the IP address and port number of the target obshell node. You can modify them as needed.${password}must be the password of theroot@sysuser of the cluster where the target obshell node belongs.Deploy an OceanBase cluster.
obshell-sdk-go enables you to deploy an OceanBase cluster in two ways: asynchronous task execution and synchronous task execution. For asynchronous task execution, a response is returned immediately after an API request is successfully sent to obshell. For synchronous task execution, a response is returned only after an API request is successfully sent to obshell and the obshell task is completed.
The following sample code deploys a cluster that contains three zones with one server in each zone:
Asynchronous task executionSynchronous task executionpackage main import ( "github.com/oceanbase/obshell-sdk-go/services" "github.com/oceanbase/obshell-sdk-go/services/v1" ) func main() { client, err := services.NewClientWithPassword("10.10.10.1", 2886, "****") if err != nil { return } // Join the current node to the cluster as Master Agent. joinRequest1 := client.V1().NewJoinRequest("10.10.10.1", 2886, "zone1") dag, err := client.V1().JoinSyncWithRequest(joinRequest1) // Handle errors in the production environment. The following similar code serves the same purpose. _, err = client.V1().WaitDagSucceed(dag.GenericID) // Join other nodes to the cluster as Follower Agent. joinRequest2 := client.V1().NewJoinRequest("10.10.10.2", 2886, "zone2") dag, err = client.V1().JoinSyncWithRequest(joinRequest2) _, err = client.V1().WaitDagSucceed(dag.GenericID) joinRequest3 := client.V1().NewJoinRequest("10.10.10.3", 2886, "zone3") dag, err = client.V1().JoinSyncWithRequest(joinRequest3) _, err = client.V1().WaitDagSucceed(dag.GenericID) // obshell prior to 4.2.3.0 should use mysqlPort(rpcPort) instead of mysql_port(rpc_port). configs := map[string]string{ "mysql_port": "2881", "rpc_port": "2882", "datafile_size": "24G", "cpu_count": "16", "memory_limit": "16G", "system_memory": "8G", "log_disk_size": "24G", } // Set configurations for each OBServer node. configObserverReq := client.V1().NewConfigObserverRequest(configs, v1.SCOPE_GLOBAL) dag, err = client.V1().ConfigObserverSyncWithRequest(configObserverReq) _, err = client.V1().WaitDagSucceed(dag.GenericID) // Set configurations for the OceanBase cluster. configObclusterReq := client.V1().NewConfigObclusterRequest("obshell-sdk-test", 12358).SetRootPwd("****") dag, err = client.V1().ConfigObclusterSyncWithRequest(configObclusterReq) _, err = client.V1().WaitDagSucceed(dag.GenericID) // Initialize the cluster. initReq := client.V1().NewInitRequest() dag, err = client.V1().InitSyncWithRequest(initReq) _, err = client.V1().WaitDagSucceed(dag.GenericID) }package main import ( "github.com/oceanbase/obshell-sdk-go/services" "github.com/oceanbase/obshell-sdk-go/services/v1" ) func main() { client, err := services.NewClientWithPassword("10.10.10.1", 2886, "****") if err != nil { return } // Join the current node to the cluster as Master Agent. joinRequest1 := client.V1().NewJoinRequest("10.10.10.1", 2886, "zone1") _, err = client.V1().JoinSyncWithRequest(joinRequest1) // Handle errors in the production environment. The following similar code serves the same purpose. // Join other nodes to the cluster as Follower Agent. joinRequest2 := client.V1().NewJoinRequest("10.10.10.2", 2886, "zone2") _, err = client.V1().JoinSyncWithRequest(joinRequest2) joinRequest3 := client.V1().NewJoinRequest("10.10.10.3", 2886, "zone3") _, err = client.V1().JoinSyncWithRequest(joinRequest3) // obshell prior to 4.2.3.0 should use mysqlPort(rpcPort) instead of mysql_port(rpc_port). configs := map[string]string{ "mysql_port": "2881", "rpc_port": "2882", "datafile_size": "24G", "cpu_count": "16", "memory_limit": "16G", "system_memory": "8G", "log_disk_size": "24G", } // Set configurations for each OBServer node. configObserverReq := client.V1().NewConfigObserverRequest(configs, v1.SCOPE_GLOBAL) _, err = client.V1().ConfigObserverSyncWithRequest(configObserverReq) // Set configurations for the OceanBase cluster. configObclusterReq := client.V1().NewConfigObclusterRequest("obshell-sdk-test", 12358).SetRootPwd("****") _, err = client.V1().ConfigObclusterSyncWithRequest(configObclusterReq) // Initialize the cluster. initReq := client.V1().NewInitRequest() _, err = client.V1().InitSyncWithRequest(initReq) }