Skip to content

x/website: Got an error with code in "Tutorial: Accessing a relational database" #50531

Closed
@taroooth

Description

@taroooth

What is the URL of the page with the issue?

page: https://go.dev/doc/tutorial/database-access

What is your user agent?

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Screenshot

スクリーンショット 2022-01-10 18 41 59

What did you do?

I wrote the code as per the documentation and ran it and got the following error.

% export DBUSER=username
% export DBPASS=password
% go run .
[mysql] 2022/01/10 17:16:10 connector.go:95: could not use requested auth plugin 'mysql_native_password': this user requires mysql native password authentication.
2022/01/10 17:16:10 this user requires mysql native password authentication.
exit status 1

I then rewrote the code using this issue as a reference. -> go-sql-driver/mysql#815 (comment)

(before)
cfg := mysql.Config{
    User:   os.Getenv("DBUSER"),
    Passwd: os.Getenv("DBPASS"),
    Net:    "tcp",
    Addr:   "127.0.0.1:3306",
    DBName: "recordings",
}

(after)
cfg := mysql.Config{
    User:   os.Getenv("DBUSER"),
    Passwd: os.Getenv("DBPASS"),
    Net:    "tcp",
    Addr:   "127.0.0.1:3306",
    DBName: "recordings",
    AllowNativePasswords: true, // Add this line.
}

And when I ran go run . again, this time I got the following error.

% go run .
2022/01/10 17:18:16 Error 1045: Access denied for user 'username'@'localhost' (using password: YES)
exit status 1

I found out that he did not have permission to the recordings database, so I granted him permission.

mysql> grant all on *.* to username@localhost;

I did the above and it worked.

% go run .
Connected!
Albums found: [{1 Blue Train John Coltrane 56.99} {2 Giant Steps John Coltrane 63.99}]
Album found: {2 Giant Steps John Coltrane 63.99}
ID of added album: 5

What did you expect to see?

I would like to see the following added to the documentation.

AllowNativePasswords: true,

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions