2009-11-15
■[MySQL]rootのパスワードが必要なスクリプトを書く場合の注意点
MySQLのrootユーザーのパスワードが必要なスクリプトを書く場合、
#!/bin/bash mysqladmin -uroot -ptiger ping
のようにスクリプト自体にパスワードを書くのではなく、rootのHOMEディレクトリの.my.cnf(/root/.my.cnf)の中にパスワードを書き、rootユーザにしか読めないように600にしておく。
# cat > /root/.my.cnf [client] user = root password = tiger [mysqladmin] user = root password = tiger # chown root:root /root/.my.cnf # chmod 600 /root/.my.cnf
と、ここまではいいのだが、テストとして、
$ sudo mysqladmin ping mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: NO)'
とやったら、意図した通りに動かなくてちょっと困った。
$ man sudo ... -H The -H (HOME) option sets the HOME environment variable to the homedir of the target user (root by default) as specified in passwd(5). By default, sudo does not modify HOME (see set_home and always_set_home in sudoers(5)).
というようにsudoはデフォルトではHOMEを変更しないので、
$ sudo -H mysqladmin ping
mysqld is alive
が正解。トーシローでサーセン。
リンク元
- 281 http://b.hatena.ne.jp/hotentry/it
- 123 http://reader.livedoor.com/reader/
- 109 http://d.hatena.ne.jp/
- 56 http://twitter.com/
- 47 http://b.hatena.ne.jp/entry/d.hatena.ne.jp/perezvon/20091115/1258269745
- 44 http://pipes.yahoo.com/pipes/pipe.info?_id=tDfBdGWF3RGl9XNm1L3fcQ
- 43 http://www.google.co.jp/reader/view/
- 32 http://www.google.co.jp/reader/view/?hl=ja&tab=wy
- 31 http://www.google.com/reader/view/
- 26 http://pg.thumbnailcloud.net/data/frame_358892.aspx