【问题标题】:how to write correct dsn in linux如何在linux中编写正确的dsn
【发布时间】:2021-12-02 13:56:14
【问题描述】:

我遵循本教程https://golang.org/doc/tutorial/database-access,但我不知道如何在代码中连接到 mysql,只是为了知道我完成了所有这些: 为您的代码创建一个文件夹, 建立数据库, 查找并导入数据库驱动程序

我在这一步遇到的问题Get a database handle and connecthttps://golang.org/doc/tutorial/database-access#get_handle

代码

// Capture connection properties.
cfg := mysql.Config{
    User:                 "hashem",
    Passwd:               "hashem",
    Net:                  "tcp",
    Addr:                 "127.0.0.1:3306",
    DBName:               "recordings",
    AllowNativePasswords: true,
}
// Get a database handle.
println(cfg.FormatDSN())
var err error
db, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil {
    log.Fatal(err)
}

pingErr := db.Ping()
if pingErr != nil {
    log.Fatal(pingErr)
}
fmt.Println("Connected!")

在这段代码中它应该打印Connected!,但它总是返回错误Error 1045: Access denied for user 'hashem'@'localhost' (using password: YES)用户名和密码以及本地主机我认为它是正确的。只是想知道我什么时候在终端sudo mysql -u root -p登录mysql并要求输入linux密码,然后输入hashem然后要求输入mysql密码,我没有在这里输入任何东西,它的登录很好,我可以做每一个就像选择和插入一样。但在代码中我无法登录

【问题讨论】:

  • 使用mysql cli 程序时,您使用-u root 登录。在 Go 中,您尝试使用 User: "hashem" 登录。它们明显不同。您是否能够使用mysql cli 程序使用-u hashem 进行连接?如果没有,那么您已经得到了答案。
  • @mkopriva 是的,我尝试但不工作-u hashem:hashem@tcp(127.0.0.1:3306)/recordings?checkConnLiveness=false&maxAllowedPacket=0 和错误Error 1045: Access denied for user '-u hashem'@'localhost' (using password: YES)

标签: mysql go


【解决方案1】:

问题在于 mysql 用户登录。只需几个步骤即可解决您的问题。

  1. 如果您已经创建了用户和密码,请登录并检查您是否已授予所需权限。
$ mysql -u hashem -p
mysql> SHOW GRANTS FOR 'hashem'@'localhost';

如果您不确定,请授予所有权限:

$ sudo mysql -u root
mysql> GRANT ALL PRIVILEGES ON * . * TO 'hashem'@'localhost';
mysql> FLUSH PRIVILEGES;
  1. 检查是否已创建所需的数据库。
$ mysql -u hashem -p
mysql> CREATE DATABASE recordings;
Query OK, 1 row affected (0.96 sec)
  1. 检查连接字符串是否按照您希望的方式生成。连接前打印cfg.FormatDSN()并检查。
username:password@tcp(127.0.0.1:3306)/database?allowNativePasswords=false&checkConnLiveness=false&maxAllowedPacket=0

根据您的评论,您的连接字符串中没有提到allowNativePasswords。有关这方面的更多信息,请查看page@Medium

参考资料:

  1. https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql
  2. https://www.inmotionhosting.com/support/server/databases/create-a-mysql-database/
  3. https://medium.com/@crmcmullen/how-to-run-mysql-8-0-with-native-password-authentication-502de5bac661

【讨论】:

  • 好吧,我仍然不知道mysql.Config 有什么问题,因为我尝试了您在终端中所说的内容,但我不知道要在代码中更改什么
  • 代码对我来说似乎没问题。授予后您是否检查过权限列表?显示 'hashem'@'localhost' 的授权;
  • 当我尝试 GRANT ALL PRIVILEGES ON * . * TO 'hashem'@'localhost'; 它返回 ERROR 1045 (28000): Access denied for user 'hashem'@'localhost' (using password: NO) 然后我尝试 FLUSH PRIVILEGES; 它返回 Query OK, 0 rows affected (0.08 sec)
  • 尝试删除用户并重新创建,sudo mysql -u root
    DROP USER 'hashem'@'localhost';
    CREATE USER 'hashem'@'localhost' IDENTIFIED BY 'password';
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 1970-01-01
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多