はじめに
本番環境を仕様書にして検証環境を構築している最中のこと。
本番環境のシェルスクリプトがmysqlコマンドをユーザID、パスワードを渡さずに実行しています。ユーザID、パスワードは、ホームディレクトリの.my.cnfファイルに定義されていたので、検証環境も同じように定義しました。(本番環境、検証環境でパスワードは異なる)
検証環境でシェルスクリプトを実行すると、MySQLのログインに失敗し実行エラーが発生する。
それが解決できたので、同じ事象が起きた時のために情報をまとめます。
環境
- CentOS Linux release 7.8.2003 (Core)
- mysql Ver 8.0.22
ログインできないケース
認証情報の定義
.my.cnf
[client]
user = root
password = Pass1234#
ログイン実行結果
console
[root@dev01 ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
原因
もしかして、パスワードの#がコメントの始まりとして無視されているのではないか?
パスワードをダブルクォーテーションで括ってみます。
.my.cnf
[client]
user = root
password = "Pass1234#"
console
[root@dev01 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 8.0.22 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
今度はログインできました!
最後に
検証環境でシェルスクリプトが正常実行できるようになりました。
設定ファイルの設定値が文字列の場合は、クォーテーションで括ったほうが良いのかな。