MySQL > Mac OS XにMySQL5.1をMacPortsでインストール

更新日 2012-11-13
広告
MacPortsを利用してMySQLをインストールする方法を紹介します。

インストール

$ sudo port -d install mysql5-server
つづいて、データベースを初期化します。
$ sudo -u mysql mysql_install_db5
Installing MySQL system tables...
OK
Filling help tables...
OK
 
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
 
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
 
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h mypc.local password 'new-password'
 
Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation
 
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
 
See the manual for more instructions.
 
You can start the MySQL daemon with:
cd /opt/local ; /opt/local/lib/mysql5/bin/mysqld_safe &
 
You can test the MySQL daemon with mysql-test-run.pl
cd /opt/local/mysql-test ; perl mysql-test-run.pl
 
Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!
 
The latest information about MySQL is available at http://www.mysql.com/
Support MySQL by buying support/licenses from http://shop.mysql.com/
上記コマンドを実行した際に、データベース用のディレクトリが無い場合はエラーになるので、その場合は、以下のように用意してあげる必要があります。最近のMacPortsであれば、自動的にディレクトリも用意してくれると思います。
$ sudo mkdir /opt/local/var/db/mysql5
$ sudo mkdir /opt/local/var/db/mysql5/mysql
$ sudo mkdir /opt/local/var/db/mysql5/test
$ sudo chown -R mysql:mysql /opt/local/var/db/mysql5
必要であれば、設定ファイルをコピーしてください。以下は、my-small.cnfをコピーしていますが、CPUやメモリに余裕があればmy-medium.cnfとかでもよいと思います。 コピー後のファイル名は my.cnf としてください。
$ sudo cp /opt/local/share/mysql5/mysql/my-small.cnf /opt/local/etc/mysql5/my.cnf
ちなみに、サーバが利用する文字コードをUTF-8に変更するには、設定ファイルmy.cnfの[client]と[mysqld]と[mysql]の下に、
default-character-set = utf8
を、追加すればよいです。ただし、この設定は MySQL5.5 ではエラーになるので注意してください。

起動

以下のコマンドでサーバを起動できます。mysqld_safeを使用してもよいのですが、停止するのが少し面倒なので、ちょっとした用途に使うために起動するだけなら、以下のコマンドのほうが楽だと思います。
$ sudo /opt/local/share/mysql5/mysql/mysql.server start
Starting MySQL
... SUCCESS!
実行ファイル用のディレクトリがなかったり、mysqlユーザにpermissionがないとエラーになります。その場合は、以下のようにディレクトリを作成してpermissionを与えるとよいです。
$ sudo mkdir /opt/local/var/run/mysql5
$ sudo chown mysql:mysql /opt/local/var/run/mysql5

停止

サーバの停止は以下のコマンドになります。
$ sudo /opt/local/share/mysql5/mysql/mysql.server stop
Shutting down MySQL
... SUCCESS! 

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

MySQLサーバを起動したら、まずはrootユーザのパスワードを設定します。
$ mysqladmin5 -u root password 'hogehoge'

MySQLサーバにログイン

$ mysql5 -u root -p
パスワードの入力を求められるので、先ほど設定したパスワードを入力してください。
広告
お問い合わせは sweng.tips@gmail.com まで。
inserted by FC2 system