写点什么

【我与 openGauss 的故事系列】奇思妙想——通过 Go 语言自制安装 openGauss 二进制程序(二)

作者:daydayup
  • 2023-08-01
    北京
  • 本文字数:2663 字

    阅读完需:约 9 分钟

二、 openGauss 安装 (15s 安装完成)


01 源代码


[root@node1 ~]# cat gaussdb.gopackage main
import ( "fmt" "io/ioutil" "os/exec")
func main() { cmd := exec.Command("/bin/bash", "-c", `useradd omm ;echo "Enmo@123" | passwd --stdin omm > /dev/null ;mkdir -p /opt/mogdb/software;chown -R omm:omm /opt/;tar -xf openGauss-3.1.0-CentOS-64bit.tar.bz2 -C /opt/mogdb/software; su - omm -c "echo 'export GAUSSHOME=/opt/mogdb/software' >> /home/omm/.bashrc ;echo 'export PATH=\$GAUSSHOME/bin:\$PATH' >> /home/omm/.bashrc ;echo 'export LD_LIBRARY_PATH=\$GAUSSHOME/lib:\$LD_LIBRARY_PATH' >> /home/omm/.bashrc;source /home/omm/.bashrc;gs_initdb --pgdata=/opt/mogdb/data --nodename=primary --pwpasswd=Enmo@123 --encoding=UTF-8 --locale=en_US.UTF-8 > /dev/null ;echo \"port=26000\" >> /opt/mogdb/data/postgresql.conf;echo \"listen_addresses = '0.0.0.0'\" >> /opt/mogdb/data/postgresql.conf;echo \"password_encryption_type = 0\" >> /opt/mogdb/data/postgresql.conf;echo \"log_directory = 'pg_log'\" >> /opt/mogdb/data/postgresql.conf;echo \"remote_read_mode=non_authentication\" >> /opt/mogdb/data/postgresql.conf;echo \"host all all 0.0.0.0/0 md5\" >> /opt/mogdb/data/pg_hba.conf;gs_ctl start -D /opt/mogdb/data > /dev/null ;gsql -d postgres -p 26000 -c'select version();select pg_postmaster_start_time();';echo -e 'data_user is omm ! \ndata_port is 26000 ! \ndata_path is /opt/mogdb/data ! \ndata_soft is /opt/mogdb/software !'"`)// cmd := exec.Command("/bin/bash", "-c", `df -h;ls`) //创建获取命令输出管道 stdout, err := cmd.StdoutPipe() if err != nil { fmt.Printf("Error:can not obtain stdout pipe for command:%s\n", err) return }
//执行命令 if err := cmd.Start(); err != nil { fmt.Println("Error:The command is err,", err) return }
//读取所有输出 bytes, err := ioutil.ReadAll(stdout) if err != nil { fmt.Println("ReadAll Stdout:", err.Error()) return }
if err := cmd.Wait(); err != nil { fmt.Println("wait:", err.Error()) return } fmt.Printf("stdout:\n\n %s", bytes)}
复制代码


2 go run 测试运行


  • 准备安装包


[root@node1 ~]# ls openGauss-3.1.0-CentOS-64bit.tar.bz2 gaussdb.gogaussdb.go  openGauss-3.1.0-CentOS-64bit.tar.bz2
复制代码


  • go run


[root@node1 ~]# go run gaussdb.gostdout:
version------------------------------------------------------------------------------------------------------------------------------------------------------ (openGauss 3.1.0 build 2c0ccaf9) compiled at 2022-09-25 19:32:58 commit 0 last mr on x86_64-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit(1 row)
pg_postmaster_start_time------------------------------- 2022-09-28 13:38:28.550462+08(1 row)
data_user is omm !data_port is 26000 !data_path is /opt/mogdb/data !data_soft is /opt/mogdb/software !
复制代码


  • 连接测试


[root@node1 ~]# su - ommLast login: Wed Sep 28 13:39:38 CST 2022[omm@node1 ~]$ gsql -d postgres -p26000 -rgsql ((openGauss 3.1.0 build 2c0ccaf9) compiled at 2022-09-25 19:32:58 commit 0 last mr  )Non-SSL connection (SSL connection is recommended when requiring high-security)Type "help" for help.
openGauss=# select version(); version------------------------------------------------------------------------------------------------------------------------------------------------------ (openGauss 3.1.0 build 2c0ccaf9) compiled at 2022-09-25 19:32:58 commit 0 last mr on x86_64-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit(1 row)
openGauss=# \q[omm@node1 ~]$ logout
复制代码


  • go build 二进制


[root@node1 ~]# go build gaussdb.go[root@node1 ~]# ls gaussdb openGauss-3.1.0-CentOS-64bit.tar.bz2 gaussdb.gogaussdb  gaussdb.go  openGauss-3.1.0-CentOS-64bit.tar.bz2
复制代码


  • 清理环境


[root@node1 ~]# cat a.shpkill -9 gaussdbrm -rf /opt/mogdb/*userdel -r omm[root@node1 ~]# sh a.sh
复制代码


  • 安装


[root@node1 ~]# date;./gaussdb;dateWed Sep 28 13:53:12 CST 2022stdout:
version------------------------------------------------------------------------------------------------------------------------------------------------------ (openGauss 3.1.0 build 2c0ccaf9) compiled at 2022-09-25 19:32:58 commit 0 last mr on x86_64-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit(1 row)
pg_postmaster_start_time------------------------------- 2022-09-28 13:53:27.034021+08(1 row)
data_user is omm !data_port is 26000 !data_path is /opt/mogdb/data !data_soft is /opt/mogdb/software !Wed Sep 28 13:53:27 CST 2022
复制代码


  • 连接测试


[root@node1 ~]# su - ommLast login: Wed Sep 28 13:53:19 CST 2022[omm@node1 ~]$ gsql -d postgres -p26000 -rgsql ((openGauss 3.1.0 build 2c0ccaf9) compiled at 2022-09-25 19:32:58 commit 0 last mr  )Non-SSL connection (SSL connection is recommended when requiring high-security)Type "help" for help.
openGauss=# select version(); version------------------------------------------------------------------------------------------------------------------------------------------------------ (openGauss 3.1.0 build 2c0ccaf9) compiled at 2022-09-25 19:32:58 commit 0 last mr on x86_64-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit(1 row)
openGauss=# \q
复制代码


用户头像

daydayup

关注

还未添加个人签名 2023-07-18 加入

还未添加个人简介

评论

发布
暂无评论
【我与openGauss的故事系列】奇思妙想——通过Go语言自制安装openGauss二进制程序(二)_daydayup_InfoQ写作社区