LoginSignup
8
2

More than 5 years have passed since last update.

ローカル環境のDockerコンテナの中からAWSのMySQL(RDS)にSSH越しで接続する

Last updated at Posted at 2018-07-03

所要で久しぶりにSSHポートフォワーディングやったらちょっと躓いたので簡単な作業メモを残しておく。

手順

1. ホストのループバックインタフェース lo0 にエイリアスを当てる

user@docker-host:~$ sudo ifconfig lo0 alias 10.200.10.1/24 

2. ホストでローカルポートフォワーディングを張る

user@docker-host:~$ ssh \
    -NL 10.200.10.1:13306:xxxx.yyyy.ap-northeast-1.rds.amazonaws.com:3306 \
    ec2-user@example.com

3. コンテナのMySQLクライアントからアクセス

root@docker-container:~$ mysql \
    -h 10.200.10.1 \
    -P 13306 \
    -u dbuser \
    --database example \
    -p

もしくはPHPで

$pdo = new \PDO(
    'mysql:dbname=example;host=10.200.10.1;port=13306;charset=utf8mb4',
    'dbuser',
    'dbpassword',
    [
        \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
    ]
);

参考

8
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2