dongyun4010 2018-04-11 00:59
浏览 152
已采纳

备份DBmssql时出现Golang错误:BACKUP DATABASE异常终止

I am trying to do a full backup for an MSSQL database using go.

However, I tested the connection to the MSSQL server and it works like charm!

By executing also a query into the code SELECT @@VERSION

It gives me the version and that has been connected to mssql using the credentials in the code below.

By running a query BACKUP DATABASE name_db TO DISK = '/tmp/database.bak' it fails with the following error

Error with Backing up the DBmssql: BACKUP DATABASE is terminating abnormally

I am sure that I am missing something, any ideas/thoughts would be much appreciated.

BTW, I am on Ubuntu 16.04 and the mssql is installed there, I am using go version 1.9.

This is my code:

package main                                                                    
  import (                                                                      
     _ "github.com/denisenkom/go-mssqldb"                                       
     "database/sql"                                                             
     "context"                                                                  
     "log"                                                                      
     "fmt" )                                                                    


var server = "server_url"                                     
var port = 1433                                                                 
var user = "user"                                                          
var password = "pass"                                                  

var db *sql.DB                                                                  

func main() {                                                                   
        var err error                                                                                                                                           
        // Create connection string                                               
        connString := fmt.Sprintf("server=%s;user id=%s;password=%s;
        port=%d", server, user, password, port)                                   
        // Create connection pool                                                 
        db, err = sql.Open("sqlserver", connString)                             
        if err != nil {                                                         
                log.Fatal("Error creating connection pool: " 
                + err.Error())     
        }                                                                       
     log.Printf("Connected!
")                                                 
     defer db.Close()                                                           

        Backup()                                                                
        //Restore()                                                             
 }                                                                              

func Backup(){                                                                  
        ctx := context.Background()                                                                                                                     
        err := db.PingContext(ctx)
        if err != nil {
                log.Fatal("Error pinging database :( " + err.Error())
        }
        var result string
        err = db.QueryRowContext(ctx, "BACKUP DATABASE tempdb 
        TO DISK= '/tmp/Backup.bak' WITH NOFORMAT, NOINIT,
        SKIP, NOREWIND, NOUNLOAD, STATS = 10;").Scan(&result)
        //err = db.QueryRowContext(ctx, "select @@version").Scan(&result)
        if err != nil {
                log.Fatal("Error with Backing up the DB", err.Error())
        }
        fmt.Printf("%s
",result)

}   

I have found the solution which is very silly.. You can find it in the 1st reply.

  • 写回答

1条回答 默认 最新

  • doutui8842 2018-04-12 13:51
    关注

    That was obvious though, the code is clean and working as expected.

    The only issue was from MSSQL, where it have a specific value set for the timeout queries.

    That's why I succeeded executing other SQL queries like: SELECT @@VERSION from the example above in the Q.

    Though, I set the value of the timeout for remote queries to 0 this SQL query:

    EXEC SP_CONFIGURE 'remote query timeout', 0
    reconfigure
    EXEC sp_configure

    The 0 in remote query timeout means that the timeout is unlimited.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下