I have access to a remote mysql database through ssh port. How can I make this database connection in php?

Thanks!

    as long as everything has been set up properly you access it the same way you access a local db, server name will be something other than 'localhost'

      I will need to use ssh2_connect to connect to the server first and then use mysql connect to local host, right?

      Thanks!

        I have searched some threads. Do I also have to use mysqli ?

        Any sample codes for connect mysql on SSH port I can use?

        Thanks!

          $connection = ssh2_connect('SERVER IP', 22);
          
          ssh2_auth_password($connection, 'username', 'password');
          
          $tunnel = ssh2_tunnel($connection, 'DESTINATION IP', 3307);
          
          $db = mysqli_connect('127.0.0.1', 'DB_USERNAME', 'DB_PASSWORD', 
                                   'dbname', 3307, $tunnel)
              or die ('Fail: '.mysql_error());
          

          I digged this out. But it is still not working on my php page. Any advices?

            Write a Reply...