LoginSignup
0
0

More than 1 year has passed since last update.

RHELのMariaDBのCLIからコンテナ上のMySQLデータベースに接続する

Posted at

背景

MySQLデータベースサーバーをコンテナで起動し、動作確認のため、RHELにMariaDBを入れて、mysqlコマンドで接続出来ずにハマった時のメモ。

環境

  • サーバー
    • Red Hat OpenShift on IBM Cloud (ROKS)
    • コンテナイメージはmysql:5.7
  • クライアント
    • RHEL7
    • ocコマンド導入し、ROKSクラスターへ接続済み
    • yum install mysqlでMariaDBを導入済み

事象

クライアント環境にて、以下を実施してServiceに向けてポートフォワード。

# oc port-forward svc/mysql 3306:3306
Forwarding from 127.0.0.1:3306 -> 3306

Podを直撃する場合はこのように。

# oc port-forward pod/mysql-abcdefghij-abcde 3306:3306
Forwarding from 127.0.0.1:3306 -> 3306

クライアントから接続を試みるとこのようなエラー。

# mysql -u mysql -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
# mysql -h localhost -u mysql -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)

解決策

sockファイルをtouchしても解決せず。結論は --protocol=tcp を指定する必要がありました。

# mysql --protocol=tcp -u mysql -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

hostname無指定あるいはlocalhost指定の場合、プロセス間通信しようとしてしまうのを避ける必要がある、ということかしら。

IPアドレス指定でもいけるみたい。

# mysql -h 127.0.0.1 -u mysql -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> q

解決してから--protocol=tcpで改めて調べると、既出でしたね。勉強になりました。

0
0
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
0
0