how to write correct dsn in linux

huangapple go评论70阅读模式
英文:

how to write correct dsn in linux

问题

我遵循这个教程https://golang.org/doc/tutorial/database-access,并且我不知道如何在代码中连接到MySQL,只是想知道我已经完成了以下所有步骤:

  1. 创建一个用于存放代码的文件夹
  2. 设置一个数据库
  3. 查找并导入一个数据库驱动程序

我的问题出现在这一步获取数据库句柄并连接https://golang.org/doc/tutorial/database-access#get_handle

以下是代码:

// 捕获连接属性。
cfg := mysql.Config{
	User:                 "hashem",
	Passwd:               "hashem",
	Net:                  "tcp",
	Addr:                 "127.0.0.1:3306",
	DBName:               "recordings",
	AllowNativePasswords: true,
}
// 获取数据库句柄。
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),我认为用户名、密码和本地主机都是正确的。只是想知道,当我在终端中登录MySQL时,我输入sudo mysql -u root -p,然后需要输入Linux密码,我输入hashem,然后需要MySQL密码,我在这里没有输入任何内容,它成功登录,并且我可以正常进行选择和插入等操作。但是在代码中,我无法登录。

英文:

i follow this tutorial https://golang.org/doc/tutorial/database-access and i don't know how to connect to mysql in code just to know i done all this:
Create a folder for your code,
Set up a database,
Find and import a database driver

and my problem in this step Get a database handle and connect https://golang.org/doc/tutorial/database-access#get_handle

the code

// 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!")

in this code it should print Connected! but it always return errors Error 1045: Access denied for user 'hashem'@'localhost' (using password: YES) the user name and password and localhost i think it is correct. and just to know when i login in mysql in the terminal sudo mysql -u root -p and require the linux password and i type hashem and then require mysql password and i don't type any things here and its login very well and i can do every things well like select and insert. but in the code i can't login

答案1

得分: 0

问题出在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。有关更多信息,请参考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
英文:

The issue is with the mysql user login. Few steps to break your issue down.

  1. If you have created the user and password already, login and check if you have granted the required privileges.
$ mysql -u hashem -p
mysql> SHOW GRANTS FOR 'hashem'@'localhost';

If you are not sure, grant all privileges:

$ sudo mysql -u root
mysql> GRANT ALL PRIVILEGES ON * . * TO 'hashem'@'localhost';
mysql> FLUSH PRIVILEGES;
  1. Check if you have created the required database.
$ mysql -u hashem -p
mysql> CREATE DATABASE recordings;
Query OK, 1 row affected (0.96 sec)
  1. Check if the connection string is being generated in the way you want it to be. Print cfg.FormatDSN() before connecting and check.
username:password@tcp(127.0.0.1:3306)/database?allowNativePasswords=false&checkConnLiveness=false&maxAllowedPacket=0

By your comment, the allowNativePasswords is not mentioned in your connection string. For more info on this, check page@Medium

References:

  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

huangapple
  • 本文由 发表于 2021年10月14日 15:13:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/69566464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定