简体   繁体   English

通过SSH隧道的MySQL连接

[英]MySQL connection through SSH Tunnel

I tried to connect on remote server with a ssh tunnel in order to access to a database. 我试图通过ssh隧道在远程服务器上进行连接,以便访问数据库。

To summarize the situation: 总结情况:

1- When I launch the OS, Mysql service is starting. 1-当我启动操作系统时,Mysql服务正在启动。 I can access to local database. 我可以访问本地数据库。 I also can connect to the remote server with the command: ssh -i /Path_To_Key user@IP_Server Once connected, the prompt change and I can connect and browse the database on the server. 我还可以使用以下命令连接到远程服务器: ssh -i /Path_To_Key user@IP_Server连接后,提示更改就可以连接并浏览服务器上的数据库。 Everything is working fine!! 一切都很好!

2- So I want to create a SSH tunnel to access to the remote database from the local port 3306 2-因此,我想创建一个SSH隧道以从本地端口3306访问远程数据库

I'm doing the following steps: 我正在执行以下步骤:

  • Stop Mysql service sudo service mysql stop 停止Mysql服务sudo service mysql stop
  • Create the tunnel on the port 3306 ssh -i /Path_To_Key -L 3306:127.0.0.1:3306 user@IP_Server -f -N 在端口3306上创建隧道ssh -i /Path_To_Key -L 3306:127.0.0.1:3306 user@IP_Server -f -N
  • Start Mysql service sudo service mysql start 启动Mysql服务sudo service mysql start

When I try to start Mysql, I have the error message: 当我尝试启动Mysql时,出现错误消息:

Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details."

Here are the results of the command 这是命令的结果

systemctl status mysql.service systemctl状态mysql.service

mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: activating (start-post) (Result: exit-code) since mer. 2018-04-25 21:50:15 CEST; 5s ago
Process: 3771 ExecStart=/usr/sbin/mysqld (code=exited, status=1/FAILURE)
Process: 3764 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 3771 (code=exited, status=1/FAILURE);         : 3772 (mysql-systemd-s)
   CGroup: /system.slice/mysql.service
           control
             3772 /bin/bash /usr/share/mysql/mysql-systemd-start post
             3810 sleep 1

journalctl -xe journalctl -xe

Subject: L'unité (unit) mysql.service a commencé à démarrer
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- L'unité (unit) mysql.service a commencé à démarrer.
avril 25 21:54:49 Aspire-A515 audit[4686]: AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/4686/status" pid=4686 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=121 ouid=121
avril 25 21:54:49 Aspire-A515 audit[4686]: AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/sys/devices/system/node/" pid=4686 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=121 ouid=0
avril 25 21:54:49 Aspire-A515 audit[4686]: AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/4686/status" pid=4686 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=121 ouid=121
avril 25 21:54:49 Aspire-A515 kernel: audit: type=1400 audit(1524686089.710:65): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/4686/status" pid=4686 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=121 ouid=121
avril 25 21:54:49 Aspire-A515 kernel: audit: type=1400 audit(1524686089.710:66): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/sys/devices/system/node/" pid=4686 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=121 ouid=0
avril 25 21:54:49 Aspire-A515 kernel: audit: type=1400 audit(1524686089.710:67): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/4686/status" pid=4686 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=121 ouid=121

Do you understand what is the problem? 你明白是什么问题吗?

Are you starting the SSH Connection on the box where MySQL is running? 您是否正在运行MySQL的盒子上启动SSH连接?

If yes, you are blocking the port 3306, so MySQL can't start anymore. 如果是,则您阻止了端口3306,因此MySQL无法启动。

In this case your SSH command should be with -R and not with -L: 在这种情况下,您的SSH命令应使用-R而不是-L:

ssh -i /Path_To_Key -R 3306:127.0.0.1:3306 user@IP_Server -f -N

If my guess is not right, please clarify on which system you make the commands and from where you want to connect to the MySQL-Database through the ssh tunnel. 如果我的猜测不对,请说明您在哪个系统上执行命令以及从何处通过ssh隧道连接到MySQL数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM