2

Hi i am trying to execute a script to fill data into a database using Golang

func executeTestScript(){
    cmd := exec.Command("/usr/local/mysql/bin/mysql", "-h127.0.0.1", "-P3333", "-uusr", "-pPassxxx", "-Ddtb_test",  "< /Users/XXX/Documents/test/scripts/olds/SCRIPT_XXX.sql")

    var out, stderr bytes.Buffer

    cmd.Stdout = &out
    cmd.Stderr = &stderr

    err := cmd.Run()
    if err != nil {
        fmt.Println(fmt.Sprintf("Error executing query. Command Output: %+v\n: %+v, %v", out.String(), stderr.String(), err))
        log.Fatalf("Error executing query. Command Output: %+v\n: %+v, %v", out.String(), stderr.String(), err)
    }
}

The problem is that i am getting the error:

ERROR 1049 (42000): Unknown database '< /Users/XXX/Documents/test/scripts/olds/SCRIPT_XXX.sql'

i think the problem is the last param (the sql script path) that the exec thinks is the dbname

The following command in the terminal is working:

/usr/local/mysql/bin/mysql --host=127.0.0.1 --port=3333 --user=usr --password=Passxxx --database=dtb_test < /Users/XXX/Documents/roseula/scripts/olds/SCRIPT_XXX.sql

but i try to replicate in Go to automatize the execution of the script.

The script have drop tables, create tables, inserts, and PK with FK relationships its a very complete one so i cant execute line by line, because of that i decided to execute de mysql program to insert the data in the database.

Any suggestions?

Ramiro Nava Castro
  • 337
  • 1
  • 7
  • 14

2 Answers2

6

+1 to answer from @MatteoRagni for showing how to do stdin redirection in Golang.

But here's a simple alternative that I use:

cmd := exec.Command("/usr/local/mysql/bin/mysql", "-h127.0.0.1", "-P3333", 
  "-uusr", "-pPassxxx", "-Ddtb_test",
  "-e", "source /Users/XXX/Documents/test/scripts/olds/SCRIPT_XXX.sql")

You don't have to make the mysql client read the script using stdin redirection. Instead, you can make the mysql client execute a specific command, which is source <scriptname>.

P.S.: I also would not put the host, port, user, and password in your code. That means you have to recompile your program any time you change those connection parameters. Also it's not secure to use passwords in plaintext on the command-line. Instead, I'd put all the connection parameters into a defaults file and use mysql --defaults-file=FILENAME.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • 1
    Ahah knowledge wins again! So a dump is actually an SQL script? – Matteo Ragni Oct 23 '17 at 17:25
  • @MatteoRagni: The OP didn't say they were running a dump file, but yes, a dump file produced by `mysqldump` is an SQL script by default (mysqldump can also optionally dump CSV data files). – Bill Karwin Oct 23 '17 at 17:29
  • I understand... sorry but I'm quite ignorant in SQL, almost never used them apart for one or two dumps, thus I was curious... – Matteo Ragni Oct 23 '17 at 17:33
  • @Bill Karwi: Your sample works as expected i made the changes you suggested and the script executes and make all the changes to the db. – Ramiro Nava Castro Oct 23 '17 at 18:38
2

This is a little example that runs something like:

cat < test.txt

that is what I think you are missing in your code:

package main

import (
  "fmt"
  "os/exec"
  "os"
)

func main() {

  cmd := exec.Command("cat")
  file, _ := os.Open("test.txt")

  cmd.Stdin = file

  out, _ := cmd.Output()

  fmt.Printf("%s\n", out)
}

That prints in the console the content of test.txt, as read by cat. You will need to adapt it to your problem.

Something like:

func executeTestScript(){
    cmd := exec.Command("/usr/local/mysql/bin/mysql", "-h127.0.0.1", "-P3333", "-uusr", "-pPassxxx", "-Ddtb_test")

    dump, dump_err = os.Open("/Users/XXX/Documents/test/scripts/olds/SCRIPT_XXX.sql")
    if dump_err != nil {
      /* Handle the error if file not opened */
    }
    cmd.Stdin = dump

    var out, stderr bytes.Buffer

    cmd.Stdout = &out
    cmd.Stderr = &stderr

    err := cmd.Run()
    if err != nil {
        fmt.Println(fmt.Sprintf("Error executing query. Command Output: %+v\n: %+v, %v", out.String(), stderr.String(), err))
        log.Fatalf("Error executing query. Command Output: %+v\n: %+v, %v", out.String(), stderr.String(), err)
    }
}

if I'm not wrong...

Matteo Ragni
  • 2,837
  • 1
  • 20
  • 34