首页 > 解决方案 > 即使我可以从 mariadb-client 访问,也无法从 go-sql-driver 访问 MariaDB

问题描述

我已经在 Debian 10 Linux 服务器上安装了带有 Galera 集群的 MariaDB 10.3。

我创建了一个授予所有权限的用户ueda,如下所示:

MariaDB [koshinto]> show grants for 'ueda'@'localhost';
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for ueda@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO `ueda`@`localhost` IDENTIFIED BY PASSWORD '*923D7E4A76755E0B4924C29266AFA3E675D990DC' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)

我可以使用用户 ueda 登录并发出 sql 命令,如下所示:

ueda@scw-a:~$ mariadb -u ueda -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 169
Server version: 10.3.27-MariaDB-0+deb10u1 Debian 10

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use koshinto
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [koshinto]> select * from test;
+----------+------------------------------+
| bindid   | bind                         |
+----------+------------------------------+
| kerokero | {"kero":"kerokero","aho":20} |
+----------+------------------------------+
1 row in set (0.001 sec)

但是,从下面的代码中,

package main

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
    "log"
)

func main() {
    log.SetFlags(log.Lshortfile)
    db, err := sql.Open("mysql", "ueda:somepassword@/koshinto")
    defer db.Close()
    if err != nil {
        log.Println(err)
    } else {
        _, err := db.Query("select * from test") //
        if err != nil {
            log.Println(err)
        }
    }
}

即使在同一台服务器上使用相同的用户和密码,也会出现“访问被拒绝”错误。

ueda@scw-a:~$ go run mysql.go 
mysql.go:18: Error 1045: Access denied for user 'ueda'@'localhost' (using password: YES)

标签: mysqlgomariadb

解决方案


您是否使用了正确的连接字符串?不确定您是否从代码片段中掩盖了某些内容,但请确保使用ueda:somepassword@protocol(ip:port)/koshinto最有可能ueda:somepassword@tcp(localhost:3306)/koshinto在同一台机器上运行时出现的内容


推荐阅读