
Linode の Ubuntu VPS で MySQL を使用して Rails を使ってアプリケーションを稼働させています。
mysql のバージョンは次のとおりです。
mysql Ver 14.14 Distrib 5.5.35, for debian-linux-gnu (x86_64) using readline 6.2
通常はすべて正常に動作しますが、新しい Rails コンソールを開くと、次のエラーが発生することがあります。
/var/www/app/shared/bundle/ruby/2.0.0/gems/mysql2-0.3.14/lib/mysql2/client.rb:67:in `connect': Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) (Mysql2::Error)
from /var/www/app/shared/bundle/ruby/2.0.0/gems/mysql2-0.3.14/lib/mysql2/client.rb:67:in `initialize'
from /var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `new'
from /var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `mysql2_connection'
from /var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection'
from /var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
from /var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout'
from /var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
from /var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout'
from /home/user/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
from /var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
from /var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection'
from /home/user/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
そして、それらからコンソールに入るすべての試みは失敗します。なぜアプリケーションは依然として正常に動作し、データベースをまだ使用しているのでしょうか。
シェルで実行する場合:
$ sudo /etc/init.d/mysql status
* MySQL is stopped.
$ sudo /etc/init.d/mysql stop
* Stopping MySQL database server mysqld ... [ OK ]
$ sudo /etc/init.d/mysql start
* Starting MySQL database server mysqld ... [fail]
しかし、アプリケーションは常に正常に動作し続けます。
これら 3 つのコマンドの前は空でした/var/log/mysql/error.log
が、その後に次のように読みました。
140510 19:14:32 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
140510 19:14:32 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.
140510 19:14:32 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.
140510 19:14:32 [Note] Plugin 'FEDERATED' is disabled.
140510 19:14:32 InnoDB: The InnoDB memory heap is disabled
140510 19:14:32 InnoDB: Mutexes and rw_locks use GCC atomic builtins
140510 19:14:32 InnoDB: Compressed tables use zlib 1.2.8
140510 19:14:32 InnoDB: Using Linux native AIO
140510 19:14:32 InnoDB: Initializing buffer pool, size = 128.0M
140510 19:14:32 InnoDB: Completed initialization of buffer pool
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
140510 19:14:32 InnoDB: Retrying to lock the first data file
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
..... several thousands of these lines...
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
140510 19:16:12 InnoDB: Unable to open the first data file
InnoDB: Error in opening ./ibdata1
140510 19:16:12 InnoDB: Operating system error number 11 in a file operation.
InnoDB: Error number 11 means 'Resource temporarily unavailable'.
InnoDB: Some operating system error numbers are described at
InnoDB: http://dev.mysql.com/doc/refman/5.5/en/operating-system-error-codes.html
140510 19:16:12 InnoDB: Could not open or create data files.
140510 19:16:12 InnoDB: If you tried to add new data files, and it failed here,
140510 19:16:12 InnoDB: you should now edit innodb_data_file_path in my.cnf back
140510 19:16:12 InnoDB: to what it was, and remove the new ibdata files InnoDB created
140510 19:16:12 InnoDB: in this failed attempt. InnoDB only wrote those files full of
140510 19:16:12 InnoDB: zeros, but did not yet use them in any way. But be careful: do not
140510 19:16:12 InnoDB: remove old data files which contain your precious data!
140510 19:16:12 [ERROR] Plugin 'InnoDB' init function returned error.
140510 19:16:12 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
140510 19:16:12 [ERROR] Unknown/unsupported storage engine: InnoDB
140510 19:16:12 [ERROR] Aborting
140510 19:16:12 [Note] /usr/sbin/mysqld: Shutdown complete
140510 19:16:12 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
VPS を再起動するだけで問題が解決するようです。コンソール接続を 1 回または 2 回行うと、問題が再発します。
init.d
Mysql をシャットダウンしてもシャットダウンしないことがわかりました。停止するには、Mysql を停止する必要がありkill
、その後、Mysql は自動的に再起動します。
$ ps -ef | grep mysq[l]
mysql 31063 1 0 May19 ? 00:00:34 /usr/sbin/mysqld
$ sudo /etc/init.d/mysql stop
* Stopping MySQL database server mysqld [ OK ]
$ ps -ef | grep mysq[l]
mysql 31063 1 0 May19 ? 00:00:34 /usr/sbin/mysqld
$ sudo kill -9 31063
$ ps -ef | grep mysq[l]
mysql 6052 1 3 09:23 ? 00:00:00 /usr/sbin/mysqld
$
どんな手掛かり?
答え1
これは起動中に mysql が起動できなかったためである可能性があります。
この治療法を試してください
/etc/init.d/mysqld を再起動
またはこれを試してみてください
/etc/my.cnf ファイルから basedir 属性を削除します。
なぜ?
basedir ディレクティブは、MySQL が機能するために必要なすべてのもの (バイナリ、ライブラリ、データなど) がどこにあるかを指示します。したがって、basedir=/var/lib と指定すると、MySQL は機能を実行するために必要なすべてのものを /var/lib で検索します。
RPM による通常のインストールでは、このディレクティブは不要であり、使用しないでください。このディレクティブの詳細な説明については、MySQL マニュアルを参照してください。
注記
実際、メモリの問題でこの問題に直面したこともあります。空きメモリも確認してみてください。