I created a user ‘gou’ for all my golang projects.
create user 'gou'@'localhost' identified by 'gop';
grant select, insert, update, delete, create, drop, alter on gdb.* to 'gou'@'localhost';
This is the code in go:
...
_ "github.com/go-sql-driver/mysql"
...
db, err := sql.Open("mysql", "gou:gop@tcp(localhost:3306)/gdb?parseTime=true")
When I run the code I am getting (IP omitted):
Error 1045 (28000): Access denied for user 'gou'@'my.very.own.ip' (using password: YES) exit status 1
Note: I can actually run the code:
- using root user, but I don’t want to use root
- useing user ‘gou’@’%’, but I only want to access from localhost
2
Answers
set password for user gou and access your database using
cfg := mysql.Config{
User: gou,
Passwd: password,
Net: "tcp",
Addr: "127.0.0.1:3306",
DBName: "dbname", }
db, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil { log.Fatal(err) }