MacPorts で MySQL5 と Apach2 と PHP5 を入れたメモ。

インストール完了まで意外と時間をくってしまった。

MySQL5

ややこしいことに mysql5 と mysql5-server の2つが対象としてあるので要注意。

mysql5 を選ぶと mysql_install_db5 で一手間必要だったり、 plistを自前で書く必要がでてくるので、mysql5-server を選ぶ。

$ sudo port install mysql5-server
--->  Computing dependencies for mysql5-server
--->  Fetching mysql5
--->  Verifying checksum(s) for mysql5
--->  Extracting mysql5
--->  Applying patches to mysql5
--->  Configuring mysql5
--->  Building mysql5
--->  Staging mysql5 into destroot
--->  Installing mysql5 @5.1.46_0
The MySQL client has been installed.
If you also want a MySQL server, install the mysql5-server port.
--->  Activating mysql5 @5.1.46_0
--->  Cleaning mysql5
--->  Fetching mysql5-server
--->  Verifying checksum(s) for mysql5-server
--->  Extracting mysql5-server
--->  Configuring mysql5-server
--->  Building mysql5-server
--->  Staging mysql5-server into destroot
--->  Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting mysql5-server with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
###########################################################
--->  Installing mysql5-server @5.1.46_0
******************************************************
* In order to setup the database, you might want to run
* sudo -u _mysql mysql_install_db5
* if this is a new install
******************************************************
--->  Activating mysql5-server @5.1.46_0
--->  Cleaning mysql5-server

無事インストールが終わったら、mysql_install_db5 を実行する。

$ sudo -u _mysql mysql_install_db5
Installing MySQL system tables...
...略
Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!

ログイン時、MySQLが自動で起動するように設定。

$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

これで、mysqld が勝手に立ち上がる。
最後にログイン出来るか確認して、インストール完了。

$ mysql5 -u root
...略
mysql>

Apach2 と PHP5

Apach2 と PHP5 をいっぺんにインストール。

$ sudo port install php5 +apache2 +mysql5 +pear
Password:
--->  Computing dependencies for php5
...略
--->  Building apache2
--->  Staging apache2 into destroot
--->  Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting apache2 with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
###########################################################
Note: apache2 installs files outside the common directory structure.
--->  Installing apache2 @2.2.15_1+darwin+preforkmpm
--->  Activating apache2 @2.2.15_1+darwin+preforkmpm
--->  Cleaning apache2
...略
--->  Configuring php5
The +mysql5 variant is obsolete. Please install the php5-mysql port instead.
--->  Building php5
--->  Staging php5 into destroot
Note: php5 installs files outside the common directory structure.
--->  Installing php5 @5.3.2_0+apache2+darwin_10+macosx+mysql5+pear
--->  Activating php5 @5.3.2_0+apache2+darwin_10+macosx+mysql5+pear
To customize php, copy
/opt/local/etc/php5/php.ini-development (if this is a development server) or
/opt/local/etc/php5/php.ini-production (if this is a production server) to
/opt/local/etc/php5/php.ini and then make changes.
 
If this is your first install, you need to activate PHP in your web server.
 
To enable PHP in Apache, run
  cd /opt/local/apache2/modules
  /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
--->  Cleaning php5

ログイン時、Apache2 が自動で起動するように設定。

$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

php.ini をコピー。

$ sudo cp /opt/local/etc/php5/php.ini-development /opt/local/etc/php5/php.ini

php5 単体では、MySQLインターフェースがインストールされない。
なので、別途インストールする必要がある。

$ sudo port install php5-mysql
...略
--->  Staging php5-mysql into destroot
--->  Installing php5-mysql @5.3.2_0+mysqlnd
To use mysqlnd with a local MySQL server, edit /opt/local/etc/php5/php.ini and set
mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket
to /opt/local/var/run/mysql5/mysqld.sock
--->  Activating php5-mysql @5.3.2_0+mysqlnd
--->  Cleaning php5-mysql

MySQLに関わる設定をする為、php.ini を修正する。

$ sudo vi /opt/local/etc/php5/php.ini
pdo_mysql.default_socket =/opt/local/var/run/mysql5/mysqld.sock
mysql.default_socket =/opt/local/var/run/mysql5/mysqld.sock
mysqli.default_socket =/opt/local/var/run/mysql5/mysqld.sock

mbstring も分離されているので、別途インストールする。

$ sudo port install php5-mbstring
...略
--->  Installing php5-mbstring @5.3.2_0
--->  Activating php5-mbstring @5.3.2_0
--->  Cleaning php5-mbstring

以上で、port を使ったインストール作業は完了。
続いて、httpd.conf の設定を行う。

httpd.conf に php5 モジュールを追加。

$ sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so 
[activating module `php5' in /opt/local/apache2/conf/httpd.conf]

httpd.conf に php の設定を追加。

$ sudo vi /opt/local/apache2/conf/httpd.conf
...
<IfModule php5_module>
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
 
    <IfModule dir_module>
        DirectoryIndex index.html index.php
    </IfModule>
</IfModule>

httpd を再起動。

$ sudo /opt/local/apache2/bin/apachectl restart

最後に、php ファイルを用意しブラウザからアクセス出来ることを確認して、インストール完了。

なんか・・・

意外と手間取った。

1月 242010
 

mysqld (MySQL 5.1.41) ソースコードざっくり勉強メモ。
関数の呼び出し順番とかを主に。そして適当に。
動作環境は CentOS 5.4。

オプション解析

my.cnf とか。

sql/mysqld.cc       main():4301
  init_common_variables() が出発点
 
sql/mysqld.cc       init_common_variables():3286
  load_defaults()
 
mysys/default.c     load_defaults():383
  my_load_defaults()
 
mysys/default.c     my_load_defaults():427
  my_search_option_files() 通過後、argc, argv を読み込んで、値を整形。
 
mysys/default.c     my_search_option_files():153
  my.cnf を fgets() してる
 
sql/mysqld.cc       init_common_variables():3289
  get_options()
 
sql/mysqld.cc       get_options():8495
  handle_options()
 
mysys/my_getopt.c   handle_options():112
  オプション構造体や変数に設定値を格納してる

ネットワーク初期化

listen socket 作成とか。

sql/mysqld.cc   main():4403
  network_init()
 
sql/mysqld.cc   network_init():1619
  socket(), bind(), listen() してる。

接続受付

accept とか。

sql/mysqld.cc   main():4517
  handle_connections_sockets() を呼んでる。
 
sql/mysqld.cc   handle_connections_sockets():4993
  select(), accept() してる。
  成功したら create_new_thread() を呼んでる。

受信

パケット受信とか。

sql/mysqld.cc   create_new_thread():4918
  accept 後など、コネクションに対して新しくスレッドを作成するとき呼ばれると思われる関数。
  いろいろ設定後、scheduler_functions::add_connection() を呼んでる。
  add_connection() は関数ポインタで create_thread_to_handle_connection() がセットされてる。
 
sql/mysqld.cc   create_thread_to_handle_connection():4853
  実際にスレッドを作成してる関数。
  特に何事もなければ handle_one_connection() をコールバック関数にしてスレッドを作成してる。
 
sql/sql_connect.cc  handle_one_connection():1074
  1コネクションに対するスレッドのハンドラ。
  スレッド初期化とかパケット受信とかコマンド発行とかしてる。
  1080行目で、scheduler_functions::init_new_connection_thread() を呼んでる。
  init_new_connection_thread() は関数ポインタで init_new_connection_handler_thread() がセットされてる。
 
sql/sql_connect.cc  init_new_connection_handler_thread:611
  1コネクションに対するスレッドの初期化処理が行われる関数と思われる。
  618行目で、my_thread_init()  を呼んでる。
11月 262009
 

MySQL 5.1.41 を GDB 使って解析するメモ。取っかかり編。
環境 : CentOS 5.2

ダウンロード

MySQL 5.1.41 のソースをダウンロードする。

インストール

/usr/local/mysql-5.1.41 へインストールする。

$ tar zxvf mysql-5.1.41.tar.gz
$ cd mysql-5.1.41
$ ./configure --prefix=/usr/local/mysql-5.1.41 --with-unix-socket-path=/usr/local/mysql-5.1.41/tmp/mysql.sock --with-charset=utf8 --with-extra-charsets=all --enable-thread-safe-client --with-plugins=innodb_plugin --with-readline --with-debug
$ make
$ make install
$ cd /usr/local/mysql-5.1.41
$ sudo cp share/mysql/my-medium.cnf /etc/my.cnf

GDB

最初に mysqld を gdb 経由で実行する。

$ gdb ./libexec/mysqld
GNU gdb Red Hat Linux (6.5-25.el5rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

立ち上がったら続けて、main に break ポイントを打ってみる。

(gdb) b main
Breakpoint 1 at 0x81b697a: file mysqld.cc, line 4261.

main は sql/mysqld.cc の 4261 行目にある。
とりあえず実行。

(gdb) r
Starting program: /usr/local/mysql-5.1.41/libexec/mysqld
[Thread debugging using libthread_db enabled]
[New Thread -1209051440 (LWP 10008)]
[Switching to Thread -1209051440 (LWP 10008)]
 
Breakpoint 1, main (argc=1, argv=0xbf96e304) at mysqld.cc:4261
4261      MY_INIT(argv[0]);             // init my_sys library & pthreads

MY_INIT(argv[0]) のとこで止まった。(ちなみに MY_INIT() はマクロで、include/my_sys.h の 40 行目にある)
いま止まってるとこ付近のソースを表示する。

(gdb) l
4256    int win_main(int argc, char **argv)
4257    #else
4258    int main(int argc, char **argv)
4259    #endif
4260    {
4261      MY_INIT(argv[0]);             // init my_sys library & pthreads
4262      /* nothing should come before this line ^^^ */
4263
4264      /* Set signal used to kill MySQL */
4265    #if defined(SIGUSR2)

main の直後 に MY_INIT() が呼ばれており、#else 〜 #endif で windows とそれ以外で main を切り分けてるのがなんとなく分かる。
ステップ実行して MY_INIT() に入る。

(gdb) s
my_init () at my_init.c:73
73        if (my_init_done)
Current language:  auto; currently c

my_init() へ辿り着いた。my_init() は MY_INIT() の中で呼ばれており、mysys/my_init.c の 73 行目にある。
my_init_done の値を確認する。

(gdb) p my_init_done
$1 = 0 '\0'

0 が入ってる。my_init() 実行前だから当然っちゃ当然。(my_init_done は my_bool型(typedef char))

やっぱりステップ実行は便利ですね。

参考

GDB マニュアル

6月 142009
 

すぐ忘れる脳の為に、よくやる MySQL 設定メモ。

configure オプション

./configure \
--prefix=/usr/local/mysql-5.1.35 \
--with-unix-socket-path=/usr/local/mysql-5.1.35/tmp/mysql.sock \
--with-charset=utf8 \
--with-extra-charsets=all \
--enable-thread-safe-client \
--with-plugins=ndbcluster, innobase, partition \
--with-readline \
--with-debug=full

with-readline = 日本語入力など。
with-debug = デバッグしたいときなど。

my.cnf

max_connections=300
connect_timeout=86400
interactive_timeout=86400
debug=d:t:o,/usr/local/mysql-5.1.35/tmp/mysqld.trace

max_connections = 接続受付数
connect_timeout = 接続タイムアウトまでの秒数
interactive_timeout = 接続先から何も要求がない状態タイムアウトまでの秒数
debug = デバッグトレースなど。