shell - php類比ssh方式登入執行mysql語句

來源:互聯網
上載者:User
關鍵字 php mysql shell

如何使用php類比實現ssh方式登入執行mysql語句輸出結果?類似於navicat的SSH串連資料庫


查詢php.net SSH文檔先驗證SSH:

$conn=ssh2_connect('SSH IP',22);ssh2_auth_password($conn,'SSH USER','SSH PASS');

然後看網上幾種不知道是否正確的寫法:
1.使用ssh2_tunnel

$tunnel = ssh2_tunnel($conn, 'REMOTE MYSQL IP', 3307);$mysqli=new mysqli('127.0.0.1','MYSQL USER','MYSQL PASS','MYSQL DB',3307,$tunnel);$result=$mysqli->query($SQL);

2.使用ssh2_exec

$stream=ssh2_exec($connection,'echo "select * from db.table where id=\"1\";"  | mysql');

3.使用shell_exec

shell_exec("ssh -f -L 127.0.0.1:3307:127.0.0.1:3306 user@remote.rjmetrics.com sleep 60 >> logfile");  $db = mysqli_connect('127.0.0.1', 'MYSQL USER','MYSQL PASS','MYSQL DB', 3307);

...好像還有使用ssh2_shell的,在不知道資料庫伺服器可以執行那些指令碼的情況下怎麼寫這個串連?

回複內容:

如何使用php類比實現ssh方式登入執行mysql語句輸出結果?類似於navicat的SSH串連資料庫


查詢php.net SSH文檔先驗證SSH:

$conn=ssh2_connect('SSH IP',22);ssh2_auth_password($conn,'SSH USER','SSH PASS');

然後看網上幾種不知道是否正確的寫法:
1.使用ssh2_tunnel

$tunnel = ssh2_tunnel($conn, 'REMOTE MYSQL IP', 3307);$mysqli=new mysqli('127.0.0.1','MYSQL USER','MYSQL PASS','MYSQL DB',3307,$tunnel);$result=$mysqli->query($SQL);

2.使用ssh2_exec

$stream=ssh2_exec($connection,'echo "select * from db.table where id=\"1\";"  | mysql');

3.使用shell_exec

shell_exec("ssh -f -L 127.0.0.1:3307:127.0.0.1:3306 user@remote.rjmetrics.com sleep 60 >> logfile");  $db = mysqli_connect('127.0.0.1', 'MYSQL USER','MYSQL PASS','MYSQL DB', 3307);

...好像還有使用ssh2_shell的,在不知道資料庫伺服器可以執行那些指令碼的情況下怎麼寫這個串連?

可以參看我的筆記介紹SSH2的

串連到遠程shell:

$connection = ssh2_connect('example.com', 22);if (ssh2_auth_password($connection, 'root', 'password')) {    echo "Authentication Successful!\n";} else {    die('Authentication Failed...');}

執行命令:

$stream = ssh2_exec($connection, 'mysql -uroot -p密碼;use 資料庫名;select * from table_name');

擷取返回結果:

stream_set_blocking($stream, true);$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);echo stream_get_contents($stream_out);
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.