douchuntang2827 2019-05-13 07:41
浏览 78

如何使用exec.Command登录mysql

I want to login MySql with exec.Command of GoLang, I am sure that I can login success, but I can not enter into MySql script interface.

The program do not output any error, it exit after finished, that is not what I expected, I want to enter into the mysql script interface.

The code is like this:

    host := "localhost"
    user := "root"
    password := "root"

    cmd := exec.Command("mysql","-h"+host,"-u"+user,"-p"+password)
    cmd.Output()

I want to see this after the program run:

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

then I can write mysql script continue. thanks!

  • 写回答

1条回答 默认 最新

  • dongque3797 2019-05-17 02:29
    关注

    After my trying, the answer is this:

    cmd := exec.Command("mysql","-h"+host,"-u"+user,"-p"+password,"-P"+port)
    cmd.Stdin = os.Stdin
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    err := cmd.Run()
    

    only need give os.Stdin to cmd.Stdin, then we will enter into mysql script interface.

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题