0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MySQLの.mylogin.cnfを使ってみた

Last updated at Posted at 2022-07-09

環境情報

OS : CentOS (Docker)
DB : MySQL8.0

.mylogin.cnfとは

ログインファイルに認証情報を保存できます。
設定するとログインパスを指定するだけでMySQLにログインできるようになります。

このオプションファイルは暗号化されたファイルであり、mysql_config_editorユーティリティを使用して作成します。

使い方

MySQL接続用のユーザーを追加と切り替え

まず、MySQLに接続するためのユーザーを作成し、ユーザーを切り替えます。`

sh-4.2# useradd -m hoge && 
sh-4.2# echo "hoge:hoge" | chpasswd && 
sh-4.2# echo "hoge ALL=(ALL) ALL" >> /etc/sudoers
sh-4.2# sudo su - hoge

ログインパスを設定

mysql_config_editorユーティリティを使用してログインパスを設定します。

構文は以下の通りになります。

mysql_config_editor set --host=<接続するサーバ> --login-path=<ログインパス名> --user=<使用するMySQLのユーザー> --password

今回はローカルのMySQLにmysqlユーザーで接続できるように設定します。
コマンドを実行し、パスワードも入力します。

[hoge@2476896863b5 ~]$ mysql_config_editor set --host=localhost --login-path=myloginpath --user=mysql --password
Enter password:
[hoge@2476896863b5 ~]$

設定内容と確認

mysql_config_editorのprintコマンドを使えば、設定内容も確認できます。

[hoge@2476896863b5 ~]$ mysql_config_editor print --all
[myloginpath]
user = "mysql"
password = *****
host = "localhost"

MySQLに接続

[hoge@2476896863b5 ~]$ mysql --login-path=myloginpath
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 8.0.29 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

設定したログインパスを削除

設定したログインパスを削除するにはremoveコマンドを使用します。

[hoge@2476896863b5 ~]$ mysql_config_editor remove --login-path=myloginpath
[hoge@2476896863b5 ~]$ mysql_config_editor print --all
[hoge@2476896863b5 ~]$ 

ちなみに、以下のように--userなどの指定した部分だけを削除することもできます。

[hoge@2476896863b5 ~]$ mysql_config_editor remove --login-path=myloginpath --user
[hoge@2476896863b5 ~]$ mysql_config_editor print --all
[myloginpath]
password = *****
host = "localhost"

参考文献

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?