从命令行可以访问master,确认权限也是OK的。
$ mysql -u root -h 192.168.3.3 -P13306 -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.33-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select host,user from mysql.user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| % | slave |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
5 rows in set (0.01 sec)
mysql>
代码里使用了同样的连接信息:
func init() {
db, _ = sql.Open("mysql", "root:123456@tcp(192.168.3.3:13306)/fileserver?charset=utf8")
db.SetMaxOpenConns(1000)
err := db.Ping()
if err != nil {
fmt.Println("Failed to connect to mysql, err:" + err.Error())
os.Exit(1)
}
}
但是运行go的时候却报错说连结被拒绝了。。。
$ go run main.go
Failed to connect to mysql, err:Error 1045: Access denied for user 'root'@'172.17.0.1' (using password: YES)
exit status 1