How to execute Mysql Script in golang using exec.Command

Ramiro Nava Castro :

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?

Bill Karwin :

+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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using exec.Command in golang, how do I open a new terminal and execute a command?

Read input from command line into the shell script exec using golang

How to execute a MySQL command from a shell script?

How to execute linux terminal command diff within a program using exec( )

How to execute command for 2 location using PHP exec()

How to hide command prompt window when using Exec in Golang?

How to send bytes data from golang to python using exec command?

How can I execute an MySQL command through a shell script using if -else for condition based?

How to pass a command with $() to exec.command() in golang

using exec to execute a system command in a new process

Cannot execute command using exec() in php

golang exec.Command execute bash exit status 1

How to execute ps1 script using docker run command?

need to "build" a mysql command and exec it in a shell script

How to execute command in Python script?

Execute Tmux with Golang Exec

How to escape "\" in exec.Command in Golang?

How to kill command Exec in difference Function in Golang

'exec' command blocks the running of php script when execute SchTasks

How to execute source command on a file in a specific directory using mysql

how to execute a script within a script in shell command

PHP script does not execute the MYSQL command

How to pass variable to exec command in expect script?

How to execute netsh command from Golang on Windows

How to execute a simple Windows command in Golang?

How to exec a command installed with `npm` command in PHP using `exec()`?

when use golang exec.Command or os.StartProcess with mysql

Execute a command inside a particular directory using os/exec in golng

how to execute command using php