OS X Mountaion Lion で MacPorts を使って開発環境を整える(MySQL編)
引き続きMySQLのインストールへ。
まずは、
$ sudo port install mysql5-server
終わり際に、こんなメッセージと、
###########################################################
# 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 port load mysql5-server
###########################################################
こんなメッセージが出る。
If this is a new install, in order to setup the database you might want to run:
sudo -u _mysql mysql_install_db5
悩ましいね。
とりあえず、
$ sudo port load 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 bohr.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!
だーっと何か出た。
さっぱりわからんのでとりあえず、
$ /opt/local/lib/mysql5/bin/mysql_secure_installation
をやってみる。
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (2)
現在のrootのパスワードをいれろってメッセージが出るが設定してないじゃん。
設定してないから何もいれなくてもいいのか?
が、空でenterしても反応なし。
これは何かオカシイ。
なんか間違ってる。
ということでmac本体を再起動だ!
・・・・・
で、もう一回ここから。
$ /opt/local/lib/mysql5/bin/mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none):
OK, successfully used password, moving on...
やっぱり、ただエンターキーだけ打てばいいってことらしい。
進む。
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
引き続きrootのパスワード設定になったので、2回入れる。
成功!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
あとは、なんじゃようわからんが全部yesで完了!
いやあ、こんな設定初めてやったかも。
$ /opt/local/lib/mysql5/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.63 Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
オッケーだ。
パスが通っていないので、
$ vim ~/.profile
して、
export PATH=/opt/local/lib/mysql5/bin:$PATH
を追加。
次へ。
$ sudo port install php5-mysql
終わり際に、以下のメッセージが出るので、
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
php.ini を編集する。
$ sudo vim /opt/local/etc/php5/php.ini
で、default_socket の空になっている = に書きこむ。
PDO。
; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
; http://php.net/pdo_mysql.default-socket
pdo_mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock
mysql。
; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysql.default-socket
mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock
mysqli。
; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysqli.default-socket
mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock
アパッチ再起動。
$ sudo /opt/local/apache2/bin/apachectl restart
infophp()でmysqlが入ってることを確認。
テスト用データベースを作成する。
mysql> CREATE DATABASE sample_db DEFAULT CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)
ユーザー権限を設定。
mysql> GRANT ALL ON sample_db.* TO sample_user@localhost IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)
データベースを指定して、
mysql> use sample_db
Database changed
テーブルを作成。
mysql> CREATE TABLE users (
-> id INT AUTO_INCREMENT,
-> name varchar(255),
-> PRIMARY KEY (id)
-> );
Query OK, 0 rows affected (0.04 sec)
テーブルを確認。
mysql> show tables;
+---------------------+
| Tables_in_sample_db |
+---------------------+
| users |
+---------------------+
1 row in set (0.00 sec)
一個データをインサート。
mysql> INSERT INTO users (name) VALUES ('yamada');
Query OK, 1 row affected (0.00 sec)
セレクトして確認。
mysql> select * from users;
+----+--------+
| id | name |
+----+--------+
| 1 | yamada |
+----+--------+
1 row in set (0.00 sec)
DBに接続してセレクトする簡単なphpを書く。
php.netからそのまま。
<?php
$link = mysqli_connect('localhost', 'sample_user', 'password', 'sample_db');
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
if ($result = mysqli_query($link, "SELECT name FROM users")) {
printf("Select returned %d rows.\n", mysqli_num_rows($result));
/* 結果セットを開放します */
mysqli_free_result($result);
}
mysqli_close($link);
画面に表示される。
Success... Localhost via UNIX socket Select returned 1 rows.
よさそうだ。