go-sql-driver / mysql

Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package
https://pkg.go.dev/github.com/go-sql-driver/mysql
Mozilla Public License 2.0
14.51k stars 2.31k forks source link

Access denied for user 'root'@'localhost' #1450

Closed hanny007 closed 1 year ago

hanny007 commented 1 year ago

Issue description

I have a mysql(version 8.0) running on my local server (host) I also have one golang service ,want it to use this mysql, and I set my service container using

docker run --network=host myapp

as network=host mode ,hoping I could use 127.0.0.1 to connect my mysql

but I got panic: Error 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

I 've tried many ips , no one works

and I even download mysql-client in the container , and it works for mysql-client , so I am sure it's not because wrong user or password .

Example code

package main

import (
    "database/sql"
    "fmt"

    _ "github.com/go-sql-driver/mysql"
)

func main() {
    db, err := sql.Open("mysql", "username:password@tcp(localhost:3306)/database_name")
    if err != nil {
        panic(err.Error())
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        panic(err.Error())
    }
    fmt.Println("success!")

Error log

panic: Error 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Configuration

image : golang 1.19

*Driver version (or git SHA): I just use go mod tidy

*Go version: 1.19

*Server version: 8.0.31-0ubuntu0.20.04.1

Server OS: Ubuntu 20.04

hanny007 commented 1 year ago

since the mysql-client works , I think it's golang mysql driver's issue ?

methane commented 1 year ago

since the mysql-client works , I think it's golang mysql driver's issue ?

I'm not sure. mysql-client and go app may use different way to connect db. Please provide complete step to reproduce.

methane commented 1 year ago

Note that this driver is used by many Gophers. If there is a simple auth bug, many users would reported it already. "I can connect with mysql cli" highly likely mean mysql cli used different auth method or different network protocol (unix vs TCP, or plain vs TLS).

Without complete step to reproduce, I assume it is your configuration issue and will close this issue.

kkfrank2 commented 7 months ago

@methane I encounter the same problems. Mysqlworkbench worked, but code not. I use the same user(root), and password is empty. both tcp and ip and port.

methane commented 7 months ago

What is your exact code and error you seen? How did you checked your MySQL Workbench uses TCP? Describe complete step, exact command/step you used and what you seen is needed.

Without them, "me too" is just a spam to me.

kkfrank2 commented 7 months ago

@methane sorry, my fault. in my case, the db have password, but my code have not input it. MySQL Workbench have store password before. So when i test again, even when I have not input password, it also worked.

methane commented 7 months ago

It is why complete step to reproduce is necessary. Without it, issue reports and "me too" are just spam notification and noise in issue tracker.