-
Notifications
You must be signed in to change notification settings - Fork 2.3k
'no database selected' when password is empty #852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This comment has been minimized.
This comment has been minimized.
I started having this same issue as soon as I upgraded to MySQL 8. |
duplicate of #825. Use master branch for MySQL 8.0. |
@methane: I'm not sure if this is fixed. #825 is closed, which you mentioned as duplicate, I'm using the latest commit on the master branch (currently
package main
import (
"database/sql"
"fmt"
"strconv"
"time"
_ "github.com/go-sql-driver/mysql"
)
func main() {
// Open connection
db, err := sql.Open("mysql", "root@/")
if err != nil {
panic(err)
}
// Test connection
err = db.Ping()
if err != nil {
panic(err)
}
// Create DB
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS mydb")
if err != nil {
panic(err)
}
// Use DB
_, err = db.Exec("USE mydb")
if err != nil {
panic(err)
}
// Create table
_, err = db.Exec("CREATE TABLE IF NOT EXISTS mytable (k VARCHAR(255) PRIMARY KEY, v BLOB NOT NULL)")
if err != nil {
panic(err)
}
// Insert data concurrently
goroutineCount := 10
for i := 0; i < goroutineCount; i++ {
go func(i int) {
_, err = db.Exec("INSERT INTO mytable (k, v) VALUES (?, ?) ON DUPLICATE KEY UPDATE v = VALUES(v)", strconv.Itoa(i), []byte("some value"))
if err != nil {
fmt.Println(err)
} else {
fmt.Println("inserted value for key " + strconv.Itoa(i))
}
}(i)
}
// Wait a bit for all goroutines to finish.
time.Sleep(2 * time.Second)
} I get the following output:
Sometimes one of the other goroutines is successful, while the rest isn't, so this isn't deterministic. But it's always one that works and the rest that fails. |
@philippgille you need to use a transaction or get a single connection from the pool. Otherwise it is not safe to execute this code, as there is no guarantee that these statements are executed sequentially on the same connection. |
This just *executes" this query on one connection in the connection pool |
I didn't fully understand @julienschmidt's comment at first, but your explanation helped make it clear @methane. Thanks a lot to both of you! |
This is fixed in 1.5.0 in my test, people meeting this problem may take a try with 1.5.0. |
Issue description
The recommended format for DSN for
root
user anddbname
db isroot@tcp(localhost:3306)/dbname
.When specifying this and with allow empty password enabled for mysql, every query complains with
no database selected
.After enabling root password for mysql, I reconfigured the DSN to
root:password@tcp(localhost:3306)/dbname
.Queries worked as expected.
I confirmed that it is possible to connect to mysql without a password using https://github.com/ziutek/mymysql. As that repo is not maintained and does not align with golang db drivers, it would be better for this driver to support this scenario.
While production systems have a password, this cost me a lot of time when configuring a local mysql server via docker.
Example code
The following example requires the user to create a database
dbname
and a tableorganizations
before executing.Configuration
OS: Windows
go-sql-driver/[email protected] (d523deb)
The text was updated successfully, but these errors were encountered: