0

If I run the following from command prompt...

mysql -h xxx.xxx.xxx.xxx -u myuser -p mydb

I can access the database and successfully run a query from the sql> command prompt

However, when I use the database/sql package in Golang get the following...

[0/0]0x0Error 1045: Access denied for user 'myuser'@'xxx.xxx.xxx.xxx' (using password: YES)

Please see code below...

db, err := sql.Open("mysql", myuser:mypassword@tcp(xxx.xxx.xxx.xxx:3306)/mydb)

if err != nil {
    color.Red(err.Error())
}
var objects []map[string]interface{}

rows, err := db.Query("select * from mytable" , args...)
if err != nil {
    return nil, err
}

Please note the db var seems fine as if it connects to the database, it's when i try to populate the rows var i get the error.

Also i created unit tests which create a database and table and test a query these all work fine.

Al firewalls etc are off.

so to summarise...

Remote + Command Prompt - works

local + golang - is fine

Remote + golang - gets the error

Shudipta Sharma
  • 4,260
  • 3
  • 16
  • 28
user3755946
  • 796
  • 1
  • 8
  • 21
  • Your go code is invalid, and won't compile. Please copy and paste your code directly (then edit out the password). – Flimzy Jul 10 '19 at 16:37
  • @Anshu: More likely, the user isn't granted remote access in mysql. – Flimzy Jul 10 '19 at 16:41
  • @flimzy..but the given error.the user don't have authentication to access the database. – Anshu Jul 10 '19 at 16:43
  • i can connect remotely from command prompt – user3755946 Jul 10 '19 at 16:44
  • "i can connect remotly from command prompt" -- Then it must be a bad username or password, or you're connecting over a different network type with different security settings (such as IPv4 vs IPv6) – Flimzy Jul 10 '19 at 16:45
  • @Anshu: I'm pretty sure the error message is the same in both cases. – Flimzy Jul 10 '19 at 16:46
  • Ok @Flimzy sure you're right.... Thanks – Anshu Jul 10 '19 at 16:49
  • Ok part of the unit test was throwing it off as the user only has select rights, it could not create the database so it did not even get the the select query – user3755946 Jul 10 '19 at 17:03

0 Answers0