ホームページ  >  記事  >  データベース  >  SSHチャネル経由でMySQLにアクセスする方法の詳細な説明

SSHチャネル経由でMySQLにアクセスする方法の詳細な説明

藏色散人
藏色散人転載
2020-01-28 13:39:162279ブラウズ

SSHチャネル経由でMySQLにアクセスする方法の詳細な説明

Mysql を使用すると、次のような状況に遭遇することがよくあります:

1. 情報は重要なので、通信を暗号化したい。

2. ポート 3306 などの一部のポートはルーターによって無効になっています。

最初の問題に対するより直接的な解決策は、mysql コードを変更するか、いくつかの証明書を使用することですが、この方法は明らかにそれほど単純ではありません。

推奨される関連学習ビデオ チュートリアル: mysql ビデオ チュートリアル

ここでは、SSH チャネルを使用してリモート Mysql に接続する別の方法を紹介します。は非常に簡単です。

#1. SSH チャネルを確立します

次のコマンドをローカルで入力するだけです:

ssh -fNg -L 3307:127.0.0.1:3306 myuser@remotehost.com
The command tells ssh to log in to remotehost.com as myuser, go into the background (-f) and not execute any remote command (-N), and set up port-forwarding (-L localport:localhost:remoteport ). In this case, we forward port 3307 on localhost to port 3306 on remotehost.com.

2. Mysql## に接続します。 #これで、ローカル データベースにアクセスするのと同じように、リモート データベースにローカルで接続できるようになります。

mysql -h 127.0.0.1 -P 3307 -u dbuser -p db
The command tells the local MySQL client to connect to localhost port 3307 (which is forwarded via ssh to remotehost.com:3306). The exchange of data between client and server is now sent over the encrypted ssh connection.

または、Mysql クエリ ブラウザを使用してクライアントの 3307 ポートにアクセスします。

同様に、PHP を使用して、

<?php
$smysql = mysql_connect( "127.0.0.1:3307", "dbuser", "PASS" );
mysql_select_db( "db", $smysql );
?>
Making It A Daemon
A quick and dirty way to make sure the connection runs on startup and respawns on failure is to add it to /etc/inittab and have the init process (the, uh, kernel) keep it going.
Add the following to /etc/inittab on each client:
sm:345:respawn:/usr/bin/ssh -Ng -L 3307:127.0.0.1:3306 myuser@remotehost.com
And that should be all you need to do. Send init the HUP signal ( kill -HUP 1 ) to make it reload the configuration. To turn it off, comment out the line and HUP init again.
にアクセスします。

以上がSSHチャネル経由でMySQLにアクセスする方法の詳細な説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はcnblogs.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。