2016年10月18日火曜日

.mylogin.cnfはmysqldの設定に影響を及ぼすのか

なんかこう、如何にも `.my.login.cnf` が当たり前のように読み取られる前提でドキュメントに書いてあって、実際問題 `mysqld` も `~/.mylogin.cnf` をstatsしているので、MySQLサーバーも `.mylogin.cnf` を読むんじゃないかと思った次第。

MySQL :: MySQL 5.6 リファレンスマニュアル :: 4.2.6 オプションファイルの使用


$ strace -f -e stat,open bin/mysqld_safe |& grep "cnf"
[pid 10841] stat("/etc/my.cnf", {st_mode=S_IFREG|0644, st_size=4317, ...}) = 0
[pid 10841] open("/etc/my.cnf", O_RDONLY) = 3
[pid 10841] stat("/etc/mysql/my.cnf", 0x7ffd4860c300) = -1 ENOENT (No such file or directory)
[pid 10841] stat("/usr/local/mysql/etc/my.cnf", 0x7ffd4860c300) = -1 ENOENT (No such file or directory)
[pid 10841] stat("/usr/mysql/5.7.16/my.cnf", 0x7ffd4860c300) = -1 ENOENT (No such file or directory)
[pid 10841] stat("/home/yoku0825/.my.cnf", 0x7ffd4860c300) = -1 ENOENT (No such file or directory)
[pid 10841] stat("/home/yoku0825/.mylogin.cnf", {st_mode=S_IFREG|0600, st_size=24, ...}) = 0
[pid 10841] open("/home/yoku0825/.mylogin.cnf", O_RDONLY) = 3
[pid 11202] stat("/etc/my.cnf", {st_mode=S_IFREG|0644, st_size=4317, ...}) = 0
[pid 11202] open("/etc/my.cnf", O_RDONLY) = 3
[pid 11202] stat("/etc/mysql/my.cnf", 0x7fffcf68d980) = -1 ENOENT (No such file or directory)
[pid 11202] stat("/usr/local/mysql/etc/my.cnf", 0x7fffcf68d980) = -1 ENOENT (No such file or directory)
[pid 11202] stat("/usr/mysql/5.7.16/my.cnf", 0x7fffcf68d980) = -1 ENOENT (No such file or directory)
[pid 11202] stat("/home/yoku0825/.my.cnf", 0x7fffcf68d980) = -1 ENOENT (No such file or directory)
[pid 11202] stat("/home/yoku0825/.mylogin.cnf", {st_mode=S_IFREG|0600, st_size=24, ...}) = 0
[pid 11202] open("/home/yoku0825/.mylogin.cnf", O_RDONLY) = 3

しっかり開いてしっかり読んでる。
とはいえ、 `.mylogin.cnf` を作る `mysql_config_editor` は変な書式をしていて、


$ mysql_config_editor --help
mysql_config_editor Ver 1.0 Distrib 5.7.16, for Linux on x86_64
Copyright (c) 2012, 2016, 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.

MySQL Configuration Utility.
Usage: mysql_config_editor [program options] [command [command options]]
  -#, --debug[=#]     This is a non-debug version. Catch this and exit.
  -?, --help          Display this help and exit.
  -v, --verbose       Write more information.
  -V, --version       Output version information and exit.

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}  Value (after reading options)
--------------------------------- ----------------------------------------
verbose                           FALSE

Where command can be any one of the following :
       set [command options]     Sets user name/password/host name/socket/port
                                 for a given login path (section).
       remove [command options]  Remove a login path from the login file.
       print [command options]   Print all the options for a specified
                                 login path.
       reset [command options]   Deletes the contents of the login file.
       help                      Display this usage/help information.


`mysql_config_editor set --help` とサブコマンドで `--help` しないと全然わからない。。


$ mysql_config_editor set --help
mysql_config_editor Ver 1.0 Distrib 5.7.16, for Linux on x86_64
Copyright (c) 2012, 2016, 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.

MySQL Configuration Utility.

Description: Write a login path to the login file.
Usage: mysql_config_editor [program options] [set [command options]]
  -?, --help          Display this help and exit.
  -h, --host=name     Host name to be entered into the login file.
  -G, --login-path=name
                      Name of the login path to use in the login file. (Default
                      : client)
  -p, --password      Prompt for password to be entered into the login file.
  -u, --user=name     User name to be entered into the login file.
  -S, --socket=name   Socket path to be entered into login file.
  -P, --port=name     Port number to be entered into login file.
  -w, --warn          Warn and ask for confirmation if set command attempts to
                      overwrite an existing login path (enabled by default).
                      (Defaults to on; use --skip-warn to disable.)

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}  Value (after reading options)
--------------------------------- ----------------------------------------
host                              (No default value)
login-path                        client
user                              (No default value)
socket                            (No default value)
port                              (No default value)
warn                              TRUE

`.mylogin.cnf` に書き込む内容を指定するには何故かオプション形式で指定しないといけない(思うに、難読化しちゃって部分修正が面倒なので、入力の段階でバリデーションをかける意図なのかしらん)ので、ソースをゴニョらない限りは `host`, `user`, `socket`, `port` 以外のオプションは埋め込めない。

が、セクションの名前を決める `--login-path` は好きに埋め込めそうだし、 `user`, `socket`, `port` は `[mysqld]` セクションにも同じ名前のオプションがあるので案外埋め込めるんじゃないかと思って埋め込んでみた。


$ mysql_config_editor set --login-path=mysqld --socket=/home/yoku0825/percona-xtrabackup-2.4.4/mysql.sock --port=11111

$ mysql_config_editor print --all
[mysqld]
socket = /home/yoku0825/percona-xtrabackup-2.4.4/mysql.sock
port = 11111

$ bin/mysqld_safe &
[1] 13416
2016-10-18T01:52:25.335653Z mysqld_safe Logging to '/usr/mysql/5.7.16/data/error.log'.
2016-10-18T01:52:25.374749Z mysqld_safe Starting mysqld daemon with databases from /usr/mysql/5.7.16/data

$ bin/mysql -S /home/yoku0825/percona-xtrabackup-2.4.4/mysql.sock -uroot
mysql> SELECT @@socket;
+----------------------------------------------------+
| @@socket                                           |
+----------------------------------------------------+
| /home/yoku0825/percona-xtrabackup-2.4.4/mysql.sock |
+----------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT @@port;
+--------+
| @@port |
+--------+
|  11111 |
+--------+
1 row in set (0.00 sec)

うん、しっかりがっつり `.mylogin.cnf` 読んでやがる。

ところで読んでるってことは…


$ mysql_config_editor reset
$ mysql_config_editor set --login-path=mysqld --socket="$(echo '/home/yoku0825/percona-xtrabackup-2.4.4/mysql.sock' ; echo 'skip-grant-tables')"
$ mysql_config_editor print --all
[mysqld]
socket = /home/yoku0825/percona-xtrabackup-2.4.4/mysql.sock
skip-grant-tables

( ゚д゚) あっ


mysql> SHOW GRANTS;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

( ゚д゚) あっ、あっ。


嫌がらせくらいにしか使えなさそうだけれども。


【2016/10/18 19:14】
Verifiedいただきました。

MySQL Bugs: #83420: mysql_config_editor should validate parameters

0 件のコメント :

コメントを投稿