Reviewed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com> Co-authored-by: Wang , Deng Ke <wangdengke2@huawei.com> Co-committed-by: Wang , Deng Ke <wangdengke2@huawei.com>
18 KiB
Step 4: Connect to a DB Instance Through gsql
gsql is a database command-line connection tool provided by GaussDB(openGauss).
Preparations
To connect to a GaussDB(openGauss) DB instance through an ECS, you must first create an ECS running Euler OS. For details, see the Elastic Cloud Server User Guide.
Remotely Connecting to the Database
- Log in to the ECS.
- Upload the client tool package and configure gsql environment variables.
- Log in to the client as user root.
- Create the /tmp/tools directory.
mkdir /tmp/tools
- Click here to obtain the GaussDB(openGauss) software package. Go to the corresponding directory based on the ECS OS architecture, obtain GaussDB-Kernel-xxx-EULER-64bit-gsql.tar.gz, and upload it to the /tmp/tools directory.
- Decompress the package.
cd /tmp/tools tar -zxvf GaussDB-Kernel-V500R001C00-EULER-64bit-gsql.tar.gz
- Set environment variables.
vi ~/.bashrc
Enter the following content and run the :wq! command to save and then exit.
export PATH=/tmp/tools/bin:$PATH
export LD_LIBRARY_PATH=/tmp/tools/lib:$LD_LIBRARY_PATH
Make the environment variables take effect.source ~/.bashrc
- Run the following command and enter the password as prompted to connect to the database.
After the database is installed, a database named postgres is generated by default.
gsql -d postgres -h 10.0.0.0 -U jack -p 8000 Password for user jack:
postgres is the name of the database to be connected; 10.0.0.0 is the EIP bound to the DB instance; jack is the username for logging in to the database; 8000 is the port number of the DB instance.
SSL Connection
- Log in to the management console.
- Click
in the upper left corner and select a region and a project.
- Click Service List. Under Database, click GaussDB.
- In the navigation pane on the left, choose GaussDB(openGauss) > Instance Management.
- In the DB Information area on the Basic Information page, click
in the SSL field to download the root certificate or certificate bundle.
- Upload the root certificate to the ECS or save it to the device to be connected to the GaussDB(openGauss) DB instance.
Import the root certificate to the Linux OS on the ECS.
- Connect to a DB instance. The Linux OS is used as an example.
export PGSSLMODE=<sslmode> export PGSSLROOTCERT=<ca-file-directory>
gsql -h <host> -p <port> -d <database> -U <user> -W <password>
Table 1 Parameter description Parameter
Description
<host>
IP address of the DB instance. To obtain this parameter, go to the Basic Information page of the DB instance. If the DB instance is accessed through the ECS, the IP address can be found in the Floating IP Address field in the Connection Information area.
<port>
Database port in use. The default value is 8000. To obtain this parameter, go to the Basic Information page of the DB instance. The port number can be found in the Database Port field in the Connection Information area.
<database>
Name of the database (postgres by default).
<user>
Username of the database account. The default administrator is root.
<ca-file-directory>
Directory of the CA certificate for the SSL connection. The certificate should be stored in the directory where the command is executed.
<sslmode>
SSL connection mode. Set it to verify-ca to use a CA to check whether the service is trusted.
For example, to connect to a DB instance through an SSL connection as user root, run the following command:
export PGSSLMODE="verify-ca" export PGSSLROOTCERT="/home/Ruby/ca.pem"
gsql -d postgres -h 10.10.0.11 -U jack -p 8000 -W Test@123
- The SSL connection is established if information similar to the following is displayed after you log in to the database:
SSL connection (cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256)