英文:
how to write correct dsn in linux
问题
我遵循这个教程https://golang.org/doc/tutorial/database-access,并且我不知道如何在代码中连接到MySQL,只是想知道我已经完成了以下所有步骤:
- 创建一个用于存放代码的文件夹
- 设置一个数据库
- 查找并导入一个数据库驱动程序
我的问题出现在这一步获取数据库句柄并连接
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用户登录上。以下是分解问题的几个步骤:
- 如果你已经创建了用户和密码,请登录并检查是否已经授予了所需的权限。
$ 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;
- 检查是否已创建所需的数据库。
$ mysql -u hashem -p
mysql> CREATE DATABASE recordings;
Query OK, 1 row affected (0.96 sec)
- 检查连接字符串是否按照你的要求生成。在连接之前打印
cfg.FormatDSN()
并进行检查。
username:password@tcp(127.0.0.1:3306)/database?allowNativePasswords=false&checkConnLiveness=false&maxAllowedPacket=0
根据你的评论,连接字符串中没有提到allowNativePasswords
。有关更多信息,请参考Medium页面。
参考资料:
- https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql
- https://www.inmotionhosting.com/support/server/databases/create-a-mysql-database/
- 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.
- 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;
- Check if you have created the required database.
$ mysql -u hashem -p
mysql> CREATE DATABASE recordings;
Query OK, 1 row affected (0.96 sec)
- 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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论