title: How to connect to mysql with go?
body: <p>I have been trying to connect mysql with go, but i can seem to succeed. I'm just trying to get a value from table and set it to variable and print it. What i'm missing maybe just something obvious</p>
  
  <p>this is from the database called bankdata</p>
  
  <pre><code>mysql&gt; select * from accounts ;
  +----+----------+-----------------+----------+---------+
  | id | username | email           | facebook | twitter |
  +----+----------+-----------------+----------+---------+
  |  1 | user1    | email1@mail.com | userfb1  | NULL    |
  |  2 | user2    | email2@mail.com | NULL     | NULL    |
  |  3 | user3    | email3@mail.com | NULL     | NULL    |
  +----+----------+-----------------+----------+---------+
  </code></pre>
  
  <p>there's two drivers that i used, and i can seem to understand what wrong with both of them</p>
  
  <p>using <a href="https://github.com/go-sql-driver/mysql" rel="noreferrer">go-sql-driver</a></p>
  
  <pre><code>package main
  
  import (
      "database/sql"
      _ "github.com/Go-SQL-Driver/MySQL"
      "log"
  )
  
  const (
      DB_HOST = "tcp(127.0.0.1:3306)"
      DB_NAME = "bankdata"
      DB_USER = /*"root"*/ "bankadmin"
      DB_PASS = /*""*/ "1234"
  )
  
  func main() {
      dsn := DB_USER + ":" + DB_PASS + "@" + DB_HOST + "/" + DB_NAME + "?charset=utf8"
      db, err := sql.Open("mysql", dsn)
      if err != nil {
          log.Fatal(err)
      }
      defer db.Close()
      var str string
      q := "select username from bankadmin.accounts where id = 1"
      err = db.QueryRow(q).Scan(&amp;str)
      if err != nil {
          log.Fatal(err)
      }
      log.Println(str)
  }
  </code></pre>
  
  <p>this returns</p>
  
  <pre><code>D:\_&gt;go run dbtest.go
  2013/03/29 13:14:11 Error 1045: Access denied for user 'bankadmin'@'localhost'
  (using password: YES)
  exit status 1
  </code></pre>
  
  <p>the password is actually right, for the both user root and bankadmin, and the mysql in running in port 3306. But then again, if i change the DB_HOST to "tcp(127.0.0.1:3000)" or change the 127.0.0.1 to localhost, it gave me the same error.</p>
  
  <p>using <a href="https://github.com/ziutek/mymysql" rel="noreferrer">mymysql driver</a></p>
  
  <pre><code>package main
  
  import (
      "database/sql"
      "fmt"
      _ "github.com/ziutek/mymysql/godrv"
      "log"
      "strconv"
  )
  
  const (
      DB_HOST = "tcp(127.0.0.1:3306)"
      DB_NAME = "bankdata"
      DB_USER = "bankadmin"
      DB_PASS = "1234"
  )
  
  type User struct {
      Id       int    
      Username string 
      Email    string 
      Facebook string
  }
  
  func OpenDB() *sql.DB {
      db, err := sql.Open("mymysql", fmt.Sprintf("%s/%s/%s", DB_NAME, DB_USER, DB_PASS))
      if err != nil {
          panic(err)
          log.Fatal(err)
      }
      return db
  }
  
  func UserById(id int) *User {
      db := OpenDB()
      defer db.Close()
      row := db.QueryRow("SELECT id, username, email FROM accounts WHERE id=?", id)
      user := new(User)
      row.Scan(&amp;user.Id, &amp;user.Username, &amp;user.Email, &amp;user.Facebook)
      return user
  }
  
  func main() {
      db := OpenDB()
      defer db.Close()
      row := db.QueryRow("SELECT id, username, email, facebook FROM accounts WHERE id=2")
      user := new(User)
      row.Scan(&amp;user.Id, &amp;user.Username, &amp;user.Email, &amp;user.Facebook)
      fmt.Println("id    : " + strconv.Itoa(user.Id) + "\nname  : " + user.Username + "\nemail : \n" + user.Email)
  
  }
  </code></pre>
  
  <p>this returns:</p>
  
  <pre><code>D:\_&gt;go run dbtest.go
  id    : 0
  name  :
  email :
  </code></pre>
  
  <p>it looks like both the id and the strings is empty, like it's never received any values from row.Scan function. And i don't know how to make sure that they are connected even though they didn't give me the same error as the previous driver</p>
  
  <p>Oh, and i'm using Windows 7, MySQL 5.5.8 that comes with WAMP.</p>
  
  <p>If i made any mistake in this post, pardon me. This is my first post.</p>