seckie's programming memo

プログラミングするにあたって調べたことなどのメモ。たまにひどい英語で書く。

MySQLの設定がold_passwords=1になっている時にでくわすエラー

何年も前から運用されていたCentOS5.5のサーバー(さくらのVPS)にPHP5.6をインストールし、Cakephp3を動作させようとした。 その際、MySQLデータベースにうまく繋がらない問題が発生。

MySQLデータベースには専用ユーザーとデータベースは作成してある。ここからMigrationでテーブルを作成しようと、コマンドを実行すると以下のようにエラーで止まってしまう。

$ bin/cake migrations migrate

Warning Error: PDO::__construct(): The server requested authentication method unknown to the client [mysql_old_password] in [/path/to/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php, line 113]

エラーメッセージから調べてみる。

MySQL側で old_passwords というオプションが有効になっているとPHP側とパスワードハッシュの方式が異なってしまってパスワードが復号できなくなる模様。もちろん、PHP側の方が新しい方式。

一時的にMySQL側の設定を変えてしまって、パスワードを生成しなおせばOKだった。

; MySQLにログイン

$ mysql -u root -p

; 一時的に 'old_passwords' オプションを無効に。

mysql> SET @@session.old_passwords = 0;

; 41バイトのハッシュで該当ユーザーのパスワードを生成し直す

mysql> SET PASSWORD FOR username@localhost = PASSWORD('NEW_PASSWORD');

crontabでのMAILTO

crontabの結果はメールで送信される…ということを今日初めて知った。コピペでやってんじゃねぇ、ドキュメント読めって話ですね。

デフォルトの送信先はrootユーザーらしい。

で、その送信先を変更するには

$ crontab -e

MAILTO="someone@somesite.com"
* 10 * * * sometasks
* 10 * * * anytasks

みたいに書けば、そのメールアドレスに報告メールが送信される。

AWS Ubuntu環境ではエラーになったときだけメールが送信されてきた。 正常終了の結果報告のメールはどうやったら送ってもらえるのか。

canvas に次々に drawImage() するときの覚え書き

例えば連番の画像 0.png〜100.png があったとしてそれを次々に drawImage() でcanvasに描きこみたい。

canvas 要素には width, height 属性を必ず書く

そうしないとうまく描画できない。CSS の width, height は代用にならない。

NG:
<canvas id="c"></canvas>

#c {
  width: 640px;
  height: 400px;
}

OK:
<canvas id="c" width="640" height="400"></canvas>

画像はプリロード必須

今回は以下のような関数を書いた。

var preloadImages = function (srcs) {
  if (!srcs.length) {
    return;
  }
  var dfd = $.Deferred();
  var imgs = [];
  for (var i=0, l=srcs.length; i<l; i++) {
    var img = new Image();
    img.src = srcs[i];
    imgs.push(img);
  }
  var check = function () {
    for (var i=0, l=imgs.length; i<l; i++) {
      if (imgs[i].complete !== true) {
        setTimeout(check, 250);
        return false;
      }
    }
    dfd.resolve(imgs);
  };
  check();
  return dfd.promise();
}

画像はすべて別々に image オブジェクトを作る

それらを順に drawImage() する。そうしないと Chrome ではうまく描画できない。Firefox では同じ image オブジェクトを使い回しても描画できた。

// NG:
var srcs = [ '0.png', '2.png' .. '100.png' ]
var img = new Image();
preloadImages(srcs).done(function () {
  for (var i=0, l=srcs.length; i<l; i++) {
    img.src = srcs[i];
    ctx.drawImage(img, 0, 0);
  }
});

// OK:
var srcs = [ '0.png', '2.png' .. '100.png' ];
preloadImages(srcs).done(function () {
  for (var i=0, l=srcs.length; i<l; i++) {
    var img = new Image();
    img.src = srcs[i];
    ctx.drawImage(img, 0, 0);
  }
});

Backbone.js 覚え書き: history.start({ pushState:true/false }) の違い

pushStateについて不勉強だなあ自分。

pattern 1

Backbone.history.start({ pushState: false })

router.navigate('hoge/fuga', { trigger: true })
# URLは http://HOSTANAME/SOMEPAGE#hoge/fuga になる

pattern 2

Backbone.history.start({ pushState: true })

router.navigate('hoge/fuga', { trigger: true })
# URLは http://HOSTANAME/hoge/fuga になる

Ubuntu に PHPMyAdmin をインストールしてドハマりした話

UbuntuPHPMyAdmin をインストール( $ sudo apt-get phpmyadmin )してドハマりした話。

基本的な設定を行って、ブラウザでアクセスすると

phpMyAdmin 環境保管領域が完全に設定されていないため、いくつかの拡張機能が無効になっています。理由についてはこちらをご覧ください。

という警告メッセージが出ている。 ググってみると config.inc.php を修正せよ、と書かれてある。

ところが、 examples/create_db.sql でできる table name と、confing.ing.php に書いてある table name が一致していない。 具体的に言うと pma__pma_ (アンダースコアが1個か2個か)だったというだけの話。
パッと見分からないし、こんなことで時間を失ったのが非常に悔しい。。

$cfg['Servers'][$i]['bookmarktable'] = 'pma__bookmark';
$cfg['Servers'][$i]['relation'] = 'pma__relation';
$cfg['Servers'][$i]['table_info'] = 'pma__table_info';
$cfg['Servers'][$i]['table_coords'] = 'pma__table_coords';
$cfg['Servers'][$i]['pdf_pages'] = 'pma__pdf_pages';
$cfg['Servers'][$i]['column_info'] = 'pma__column_info';
$cfg['Servers'][$i]['history'] = 'pma__history';
$cfg['Servers'][$i]['table_uiprefs'] = 'pma__table_uiprefs';
$cfg['Servers'][$i]['designer_coords'] = 'pma__designer_coords';
$cfg['Servers'][$i]['tracking'] = 'pma__tracking';
$cfg['Servers'][$i]['userconfig'] = 'pma__userconfig';
$cfg['Servers'][$i]['recent'] = 'pma__recent';

Yeomanを導入。オリジナル generator も作る

参考

インストール

~ $ npm install -g yo
~ $ mkdir yeomanwork
~ $ cd yeomanwork
~/yeomanwork $ npm install -g generator-generator
~/yeomanwork $ mkdir generator-something
~/yeomanwork $ cd generator-something
~/yeomanwork/generator-something $ yo generator
...

yo generator-something で呼べるようにリンクする

~/yeomanwork/generator-something $ npm link
/usr/local/lib/node_modules/generator-something -> /Users/naokis/yeomanwork/generator-something

Homebrew 導入後にやったこと

MacPorts からの乗り換え。Homebrew インストール手順は省略。
PHP + MySQL が動作する環境ができるまで。 参考にさせていただいた記事:
http://qiita.com/livejam_db/items/b70caccdeece036a3797

環境

brew doctor でまずは診断。

$ brew doctor
Warning: Broken symlinks were found. Remove them with `brew prune`:
  /usr/local/lib/node_modules/generator-website1

Warning: You have MacPorts or Fink installed:
  /opt/local/bin/port

This can cause trouble. You don't have to uninstall them, but you may want to
temporarily move them out of the way, e.g.

  sudo mv /opt/local ~/macports

Warning: Unbrewed .pc files were found in /usr/local/lib/pkgconfig.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected .pc files:
    /usr/local/lib/pkgconfig/jack.pc

Warning: You have a non-Homebrew 'pkg-config' in your PATH:
  /opt/local/bin/pkg-config

`./configure` may have problems finding brew-installed packages using
this other pkg-config.

MacPorts管理下のフォルダはどけろ、と文句を言われる

$ sudo mv /opt/local ~/macports
$ brew doctor
Warning: Unbrewed .pc files were found in /usr/local/lib/pkgconfig.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected .pc files:
    /usr/local/lib/pkgconfig/_jack.pc

まだ文句を言われるので直す

$ cd /usr/local/lib/pkgconfig/
$ mv /usr/local/lib/pkgconfig/jack.pc ~/Temp/jack.pc

下準備

$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap homebrew/homebrew-php

https://github.com/Homebrew/homebrew-php
"Install Multiple Versions" を確認

Apache インストール

$ brew install httpd
To have launchd start httpd at login:
    ln -sfv /usr/local/opt/httpd/*.plist ~/Library/LaunchAgents
Then to load httpd now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.httpd.plist
==> Summary
🍺  /usr/local/Cellar/httpd/2.2.27: 1322 files, 21M, built in 102 seconds

PHP 5.5 インストール

$ brew options php55 # optionsを確認
$ brew install php55

エラー出て止まる。いろいろ文句を言われたので直す

$ mv ~/.pearrc ~/Temp/_.pearrc
$ brew doctor
Warning: /usr/bin occurs before /usr/local/bin
This means that system-provided programs will be used instead of those
provided by Homebrew. The following tools exist at both paths:

    apr-1-config
    apu-1-config
    phar
    phar.phar
    php
    php-config
    phpize

Consider setting your PATH so that /usr/local/bin
occurs before /usr/bin. Here is a one-liner:
    echo export PATH='/usr/local/bin:$PATH' >> ~/.bash_profile

Warning: Homebrew's sbin was not found in your PATH but you have installed
formulae that put executables in /usr/local/sbin.
Consider setting the PATH for example like so
    echo export PATH='/usr/local/sbin:$PATH' >> ~/.bash_profile

パスが通っていないぞ、と。

$ vim ~/.bash_profile

# 以下一行を追加
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"

再度試す

$ brew install php55
==> Downloading http://www.php.net/get/php-5.5.11.tar.bz2/from/this/mirror
Already downloaded: /Library/Caches/Homebrew/php55-5.5.11
==> ./configure --prefix=/usr/local/Cellar/php55/5.5.11 --localstatedir=/usr/local/var --sysconfdir=
3. Apache was not built using --enable-so (the apxs usage page is displayed)

The output of /usr/sbin/apxs follows:
./configure: /usr/sbin/apxs: /usr/bin/perl: bad interpreter: No such file or directory
configure: error: Aborting

READ THIS: https://github.com/Homebrew/homebrew/wiki/troubleshooting
If reporting this issue please do so at (not Homebrew/homebrew):
  https://github.com/homebrew/homebrew-homebrew-php/issues

... まだダメ

$ brew search perl
perl-build   perl514      perl516      perl518      perlmagick
$ brew install perl518
$ cd /usr/local/bin
$ ln -s /usr/local/Cellar/perl518/5.18.2/bin/perl ./perl
$ cd /usr/bin
$ sudo ln -s /usr/local/Cellar/perl518/5.18.2/bin/perl ./perl

どうやら MacPorts のファイル群をどけた時に perl へのパスが切れていたらしい

$ brew install php55
...
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php5_module    /usr/local/opt/php55/libexec/apache2/libphp5.so

The php.ini file can be found in:
    /usr/local/etc/php/5.5/php.ini

✩✩✩✩ PEAR ✩✩✩✩

If PEAR complains about permissions, 'fix' the default PEAR permissions and config:
    chmod -R ug+w /usr/local/Cellar/php55/5.5.11/lib/php
    pear config-set php_ini /usr/local/etc/php/5.5/php.ini

✩✩✩✩ Extensions ✩✩✩✩

If you are having issues with custom extension compiling, ensure that
you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:

      PATH="/usr/local/bin:$PATH"

PHP55 Extensions will always be compiled against this PHP. Please install them
using --without-homebrew-php to enable compiling against system PHP.

✩✩✩✩ PHP CLI ✩✩✩✩

If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc,
~/.zshrc, ~/.profile or your shell's equivalent configuration file:

      export PATH="$(brew --prefix homebrew/php/php55)/bin:$PATH"

To have launchd start php55 at login:
    ln -sfv /usr/local/opt/php55/*.plist ~/Library/LaunchAgents
Then to load php55 now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist
==> Summary
🍺  /usr/local/Cellar/php55/5.5.11: 492 files, 40M, built in 3.1 minutes

ようやくインストール成功

Xdebug インストール

$ brew install php55-xdebug
...
To finish installing xdebug for PHP 5.5:
  * /usr/local/etc/php/5.5/conf.d/ext-xdebug.ini was created,
    do not forget to remove it upon extension removal.
  * Validate installation via one of the following methods:
  *
  * Using PHP from a webserver:
  * - Restart your webserver.
  * - Write a PHP page that calls "phpinfo();"
  * - Load it in a browser and look for the info on the xdebug module.
  * - If you see it, you have been successful!
  *
  * Using PHP from the command line:
  * - Run "php -i" (command-line "phpinfo()")
  * - Look for the info on the xdebug module.
  * - If you see it, you have been successful!
==> Summary
🍺  /usr/local/Cellar/php55-xdebug/2.2.4: 5 files, 248K, built in 9 seconds

一発で入る

APCu インストール

$ brew install php55-apcu
==> Installing php55-apcu dependency: pcre
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/pcre-8.35.mountain_lion.bottle.
######################################################################## 100.0%
==> Pouring pcre-8.35.mountain_lion.bottle.tar.gz
🍺  /usr/local/Cellar/pcre/8.35: 146 files, 5.8M
==> Installing php55-apcu
==> Downloading http://pecl.php.net/get/apcu-4.0.2.tgz
######################################################################## 100.0%
==> PHP_AUTOCONF="/usr/local/opt/autoconf/bin/autoconf" PHP_AUTOHEADER="/usr/local/opt/autoconf/bin/
==> ./configure --prefix=/usr/local/Cellar/php55-apcu/4.0.2 --with-php-config=/usr/local/Cellar/php5
==> make
==> Caveats
To finish installing apcu for PHP 5.5:
  * /usr/local/etc/php/5.5/conf.d/ext-apcu.ini was created,
    do not forget to remove it upon extension removal.
  * Validate installation via one of the following methods:
  *
  * Using PHP from a webserver:
  * - Restart your webserver.
  * - Write a PHP page that calls "phpinfo();"
  * - Load it in a browser and look for the info on the apcu module.
  * - If you see it, you have been successful!
  *
  * Using PHP from the command line:
  * - Run "php -i" (command-line "phpinfo()")
  * - Look for the info on the apcu module.
  * - If you see it, you have been successful!
==> Summary
🍺  /usr/local/Cellar/php55-apcu/4.0.2: 6 files, 116K, built in 9 seconds

一発で入る

MySQL インストール

$ brew install mysql
==> Installing mysql dependency: openssl
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/openssl-1.0.1g.mountain_lion.bo
######################################################################## 100.0%
==> Pouring openssl-1.0.1g.mountain_lion.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, so it was not symlinked into /usr/local.

Mac OS X already provides this software and installing another version in
parallel can cause all kinds of trouble.

The OpenSSL provided by OS X is too old for some software.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.1g: 429 files, 15M
==> Installing mysql
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/mysql-5.6.17_1.mountain_lion.bo
######################################################################## 100.0%
==> Pouring mysql-5.6.17_1.mountain_lion.bottle.tar.gz
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

To connect:
    mysql -uroot

To have launchd start mysql at login:
    ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
    mysql.server start
==> /usr/local/Cellar/mysql/5.6.17_1/bin/mysql_install_db --verbose --user=naokis --basedir=/usr/loc
==> Summary
🍺  /usr/local/Cellar/mysql/5.6.17_1: 9510 files, 342M

$ mysql.server restart

これも問題なく入る

root ユーザーにパスワード設定

手順を忘れたのでこちらを参考に:
http://webkaru.net/mysql/mysql-root-password/

$ mysql -u root
mysql> update mysql.user set password=password('root用の任意パスワード') where user = 'root';
mysql> flush privileges;
mysql> exit;

$ mysql -u root -p
Enter password: 

httpdの設定

PHPが動作するように設定(VirtualHost の設定は省略)

$ sudo vim  /usr/local/etc/apache2/httpd.conf
---
# php5 のモジュールの追加
LoadModule php5_module /usr/local/opt/php55/libexec/apache2/libphp5.so

# IfModule mime_module 内
AddType application/x-httpd-php .php

# IfModule dir_module 内
DirectoryIndex index.php index.html
---
$ sudo httpd -k restart