【问题标题】:Can't access MariaDB from go-sql-driver even I can access it from mariadb-client即使我可以从 mariadb-client 访问,也无法从 go-sql-driver 访问 MariaDB
【发布时间】:2021-05-05 14:03:14
【问题描述】:

我已经在 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)

但是,从下面的 go 代码来看,

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)

【问题讨论】:

  • 注意:千万不要使用go run <file>.go

标签: mysql go mariadb


【解决方案1】:

您是否使用了正确的连接字符串?不确定您是否从代码 sn-p 中隐藏了某些内容,但请确保使用 ueda:somepassword@protocol(ip:port)/koshinto,当您在同一台机器上运行时,它很可能是 ueda:somepassword@tcp(localhost:3306)/koshinto

【讨论】:

  • 他被服务器拒绝访问的事实表明它确实联系了服务器。服务器返回 1000 中的所有错误代码。如果它无法到达服务器,那将是 2000 年代的客户端错误代码。
  • 谢谢你。我试过tcp(localhost:3306),但结果是一样的1045: Access denied
猜你喜欢
  • 2020-10-26
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 2017-10-02
  • 1970-01-01
  • 2016-01-15
  • 2018-09-19
相关资源
最近更新 更多