mysql - 即使我可以从 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)
解决方案
您是否使用了正确的连接字符串?不确定您是否从代码片段中掩盖了某些内容,但请确保使用ueda:somepassword@protocol(ip:port)/koshinto
最有可能ueda:somepassword@tcp(localhost:3306)/koshinto
在同一台机器上运行时出现的内容
推荐阅读
- javascript - Javascript innerHTML live console.log 更改
- java - 使用 mockito 的空指针问题
- github - Github 操作:使用 mysql 服务会拒绝用户 'root'@'localhost' 的访问
- javascript - 如何使用 replaceWith 移动解释文本代替问题并删除原始 div?
- apache-spark - spark - 在大型数据帧上执行 groupby 和聚合时,java 堆内存不足
- zend-framework3 - Doctrine Migrations 找不到生成的版本
- sql - 我正在通过分期付款销售一系列产品,并尝试预测成本/税收和利润的付款分配
- webdriver-io - @wdio/cli:utils:“onPrepare”钩子中的服务失败
- java - 如何在 Tomcat 服务器中部署 Angular 8 应用程序?
- python - django rest framework 离线文档