SSH tunnel: local => gateway => MySQL server

前端 未结 3 1976
-上瘾入骨i
-上瘾入骨i 2021-02-01 06:50

I need to access a MySQL database on a remote server at my lab. The server is only accessible once I log in to a gateway server on the remote network:

local serv         


        
相关标签:
3条回答
  • 2021-02-01 07:51

    With the command:

    $ ssh -f user@gateway -L 3307:1.2.3.4:3306 -N 
    

    This states that all connections to client localhost 3307 will be forwarded via the SSH tunnel to gateway and then connected to host 1.2.3.4 to port 3306.

    edit: If the SSH is on port 24222 then

    $ ssh -f user@gateway -p 24222 -L 3307:1.2.3.4:3306 -N 
    
    0 讨论(0)
  • 2021-02-01 07:51

    Using the tunnel:

    ssh -f user@gateway -L 3307:1.2.3.4:3306 -N 
    

    you will be able to connect to the database on localhost port 3307

    0 讨论(0)
  • 2021-02-01 07:52

    If You need to use multiple hops to access MySQL server I first recommend to create .ssh/config file and use ProxyCommand like so:

      Host gateway
         HostName example.com
         User foo
         Port 22
         IdentityFile ~/.ssh/id_rsa.pub
    
      Host mysql_access_server
          HostName example-web.com
          Port 22
          User foo
          ProxyCommand ssh -A gateway nc %h %p
    

    Then forward port like so:

    ssh -f mysql_access_server -L 3309:sqlmaster.example.com:3306 -N
    

    Then You can access MySQL server like so:

    mysql --user=root --host=127.0.0.1 --password=root --port=3309 some_db_name
    
    0 讨论(0)
提交回复
热议问题