通过 SSH 隧道连接 MySQL 数据库

标签 mysql linux database shell ssh

我的新数据库有问题。访问数据库的唯一方法是使用 SSH 隧道。但它不起作用...

我使用以下命令:

sshpass -p <PASSWORD> ssh <USER>@<DOMAIN> -p <PORT> -L 3306:localhost:3306 -f -N

我认为 ssh 隧道有效并已建立。

现在我想通过 Shell-File 连接数据库。

deposit=`mysql -h localhost --port=3306 -u <DATABASEUSER> --password=<DATABASEPASSWORD> --skip-column-names -e "<MYSQLSYNTAX>"`

但是总是出现如下错误:

ERROR 1045 (28000): Access denied for user '<DATABASEUSER>'@'localhost' (using password: YES)

你有什么想法还是我做错了什么?

非常感谢!

最佳答案

假设您的所有权限都没有问题,可能值得将 localhost 换成 127.0.0.1

根据 MySQL 文档:http://dev.mysql.com/doc/refman/5.5/en/connecting.html

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server.

关于通过 SSH 隧道连接 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24959989/

相关文章:

php - 如何使用 PHP 添加保持登录状态?

linux - rsync 一般问题

python - 你如何卸载 python 包/库

mysql - 如何删除MYSQL中某列不包含特定数据的行

java - 如何从 java.sql.timestamp 检索 mysql 时间戳列

mysql - 不带计数功能的计数记录

php - PDO json_encode 不起作用,错误在哪里?

c++ - Linux 上 C 中字符数组和指针的段错误

sql - 加入日期范围查询

php - 在 PHP 的 OOP 编程中设置我的类的正确方法是什么?