---
url: /v6.2/en-us/metadb-slave-config.md
---
# Metadb Master-Slave Configuration

* [Standalone/Cluster Deployment](#standalone-cluster-deployment)
* [Container Deployment](#container-deployment)
* [K8s Deployment](#k8s-deployment)

## Configuration Instructions

1. Metadb master-slave configuration, applicable to Metadb database versions higher than 13.5. For Metadb services below this version, refer to [Database Service Upgrade](metadb-upgrade.md).

## Standalone/Cluster Deployment

### Preparation Work

1. Configure the `export PG_METASLAVE_HOST=(10.10.10.xx 10.10.10.xx)` parameter in the `/opt/hengshi/conf/hengshi-sense-env.sh` file, where `10.10.10.xx` represents the IP address of the slave node.\
   Multiple slave configurations are supported, allowing you to choose one or multiple slave nodes, and horizontal scaling of the number of slave nodes is supported.
2. Ensure that the slave server can log in to the master server without a password.
3. Before executing the initialization operation for the slave node, ensure that the master node's `metadb` service is in a running state.\
   **Note**: The slave node port cannot be configured; it uses the `HS_PG_PORT` value configured on the master node. Please ensure that this port is available on the slave node.

### Initialization Operations

**Note**

1. Before initializing metaslave, ensure that the metadb service is running.
2. The initialized slave database has read-only permissions without write permissions and cannot be directly connected for use as a database.

```shell
cd /opt/hengshi
bin/hengshi-sense-bin init metaslave
```

![](/images/metaslave/metaslave_init_sucess.png)

### Startup Operations

```shell
cd /opt/hengshi
bin/hengshi-sense-bin start metaslave
```

![](/images/metaslave/metaslave_start_sucess.png)

### Stop Operation

```shell
cd /opt/hengshi
bin/hengshi-sense-bin stop metaslave
```

### Switching Slave Database Operations

1. Promote write permissions for the slave database. This operation needs to be executed on the server with the original slave database address configured in `HS_PG_HOST`.

```shell
cd /opt/hengshi
bin/hengshi-sense-bin promote metadb
```

2. When the following prompt appears, enter `Y` to confirm the operation. The successful operation interface is shown below:
   ![](/images/metaslave/metaslave_promote.png)
3. Modify the `HS_PG_HOST` parameter in the `/opt/hengshi/conf/hengshi-sense-env.sh` file to the slave database address you want to switch to as the master database.
   **Note**
   1. If the `HS_ENGINE_HOST` parameter is not configured, please configure it. Using the default parameter will reference the `HS_PG_HOST` parameter information, which may lead to an incorrect engine address.
   2. After switching to the slave database, please reconfigure the `PG_METASLAVE_HOST` parameter. The previously configured master-slave relationship will become invalid, and you will need to perform [initialization operations](#initialization-operations) again.
4. Restart the HENGSHI service to complete the master-slave switching operation.

```shell
cd /opt/hengshi
bin/hengshi-sense-bin restart hengshi
```

## Cluster Deployment Notes

1. Uncomment the following content in `/opt/hengshi/ansible/hosts` on the cluster operation node (default Node-A):

```shell
[metaslave] #metadb database slave (optional) can be used as a backup when the master database is down
Node-B
```

2. Specify the `export PG_METASLAVE_HOST=(Node-B)` parameter in `/opt/hengshi/conf/hengshi-sense-env.sh` on the machine node where the metadb slave resides (default Node-B) as Node-B. Use spaces to separate multiple hosts.

Alternatively, you can directly modify `/opt/hengshi/conf/hengshi-sense-env.sh` on Node-A, and then use the following command to synchronize the configuration file to other nodes:

```shell
./hs_install -m config  -c ../cluster-conf/
```

3. Execute initialization and startup operations on the cluster operation node (default Node-A):

```shell
/opt/hengshi/bin/hengshi-sense-bin init metaslave
/opt/hengshi/bin/hengshi-sense-bin start metaslave
```

You will see the word "streaming," indicating that the master-slave configuration is successful.
![](/images/metaslave/metadb-slave-streaming.png)

## Container Deployment

### Preparation Work

1. Deploy Metadb master-slave using a specific docker-compose.yaml file. Please contact support personnel.
2. Configure the hsenv file:
   * HS_MASTER_PG_HOST: Deployment service node, server IP information (not the internal IP of the docker container).
   * HS_SLAVE_PG_HOST: Deployment slave node, server IP information (not the internal IP of the docker container).
3. Ensure that the HS_MASTER_PG_HOST and HS_SLAVE_PG_HOST servers can communicate normally and that the HS_PG_EXPOSE_PORT is mutually open.

### Initialization Operations

**Note**

1. Before initializing metaslave, ensure that the metadb service is running.
2. The initialized slave database has read-only permissions and no write permissions. It cannot be directly connected for use as a database.

```shell
cd single
docker-compose run --rm metadb init metaslave
```

![](/images/metaslave/metaslave_init_sucess.png)

### Start Operation

```shell
cd single
docker-compose up -d
```

![](/images/metaslave/metaslave_start_sucess.png)

### Stop Operation

```shell
cd single
docker-compose down
```

### Switching Slave Operations

1. Promote the read-write permissions of the slave. This operation needs to be executed on the server with the original slave address configured in `HS_SLAVE_PG_HOST`.

```shell
cd single
docker-compose run --rm metadb promote metadb
```

2. When the following prompt appears, enter `Y` to confirm the operation. The successful operation interface is shown below:
   ![](/images/metaslave/metaslave_promote.png)
3. Modify the `hengshi docker-compose/.env` configuration file, changing `HS_PG_HOST:xxx` to the IP of the slave server.
4. Restart the HENGSHI service to complete the master-slave switching operation.

```shell
cd single
docker-compose down
docker-compose up
```

## k8s Deployment

### Preparation Work

1. Please contact the after-sales personnel to obtain the `metaslave.yaml` configuration file.

### Startup Operations

**Note**

1. Before executing the `metaslave` operation, ensure that the `metadb` service is in a running state.
2. The initialized slave database has read-only permissions and no write permissions; it cannot be directly connected for use as a database.
3. Please confirm that the `LOCAL_HOST_NAME` parameter is configured in the `configmap.yaml` file.

```shell
kubectl -n hengshi apply -f configmap.yaml
kubectl -n hengshi apply -f metaslave.yaml
```

### Switching Slave Database Operations

1. Promote slave database read-write permissions

```shell
kubectl -n hengshi exec -it metaslave-0 -- /docker-entrypoint.sh promote metadb single
```

2. When the following prompt appears, enter Y to confirm the operation. The successful operation interface is shown below:
   ![](/images/metaslave/metaslave_promote.png)
3. Modify the `configmap.yaml` file, change the parameter `HS_PG_HOST: **metadb-0.hengshi-hs**` to `metaslave-0.hengshi-hs`.
4. Restart the HENGSHI service to complete the master-slave switching operation:

```shell
kubectl -n hengshi apply -f hengshi.yaml
```
