mysql

Last-modified: 2006-10-09 (月) 07:12:10

[ubuntu] MySQLのインストール
old_passwords=1
default-character-set=ujis
init-connect=SET NAMES binary

root@swisspc:~# apt-get install mysql-server
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています... 完了
以下の特別パッケージがインストールされます:

 libdbd-mysql-perl libdbi-perl libnet-daemon-perl libplrpc-perl mysql-client-5.0
 mysql-server-5.0 perl perl-modules

提案パッケージ:

 dbishell libcompress-zlib-perl libterm-readline-gnu-perl libterm-readline-perl-perl

推奨パッケージ:

 mailx perl-doc

以下のパッケージが新たにインストールされます:

 libdbd-mysql-perl libdbi-perl libnet-daemon-perl libplrpc-perl mysql-client-5.0
 mysql-server mysql-server-5.0 perl perl-modules

アップグレード: 0 個、新規インストール: 9 個、削除: 0 個、保留: 1 個。
34.1MB のアーカイブを取得する必要があります。
展開後に追加で 89.3MB のディスク容量が消費されます。
続行しますか [Y/n]? y
取得:1 http://security.ubuntu.com dapper-security/main mysql-client-5.0 5.0.22-0ubuntu6.06.2 [6278kB]
取得:2 http://jp.archive.ubuntu.com dapper/main perl-modules 5.8.7-10ubuntu1 [2326kB]
取得:3 http://jp.archive.ubuntu.com dapper/main perl 5.8.7-10ubuntu1 [3297kB]
取得:4 http://jp.archive.ubuntu.com dapper/main libnet-daemon-perl 0.38-1 [46.0kB]
取得:5 http://jp.archive.ubuntu.com dapper/main libplrpc-perl 0.2017-1 [35.0kB]
取得:6 http://jp.archive.ubuntu.com dapper/main libdbi-perl 1.50-1 [623kB]
取得:7 http://jp.archive.ubuntu.com dapper/main libdbd-mysql-perl 3.0002-2build1 [139kB]
取得:8 http://security.ubuntu.com dapper-security/main mysql-server-5.0 5.0.22-0ubuntu6.06.2 [21.3MB]
取得:9 http://security.ubuntu.com dapper-security/main mysql-server 5.0.22-0ubuntu6.06.2 [36.9kB]
34.1MB を 2m32s で取得しました (224kB/s)
パッケージを事前設定しています ...
未選択パッケージ perl-modules を選択しています。
(データベースを読み込んでいます... 現在 14761 個のファイルとディレクトリがインストールされています。)
(.../perl-modules_5.8.7-10ubuntu1_all.deb から) perl-modules を展開しています...
未選択パッケージ perl を選択しています。
(.../perl_5.8.7-10ubuntu1_i386.deb から) perl を展開しています...
未選択パッケージ libnet-daemon-perl を選択しています。
(.../libnet-daemon-perl_0.38-1_all.deb から) libnet-daemon-perl を展開しています...
未選択パッケージ libplrpc-perl を選択しています。
(.../libplrpc-perl_0.2017-1_all.deb から) libplrpc-perl を展開しています...
未選択パッケージ libdbi-perl を選択しています。
(.../libdbi-perl_1.50-1_i386.deb から) libdbi-perl を展開しています...
未選択パッケージ libdbd-mysql-perl を選択しています。
(.../libdbd-mysql-perl_3.0002-2build1_i386.deb から) libdbd-mysql-perl を展開しています...
未選択パッケージ mysql-client-5.0 を選択しています。
(.../mysql-client-5.0_5.0.22-0ubuntu6.06.2_i386.deb から) mysql-client-5.0 を展開しています...
未選択パッケージ mysql-server-5.0 を選択しています。
(.../mysql-server-5.0_5.0.22-0ubuntu6.06.2_i386.deb から) mysql-server-5.0 を展開しています...
未選択パッケージ mysql-server を選択しています。
(.../mysql-server_5.0.22-0ubuntu6.06.2_all.deb から) mysql-server を展開しています...
perl-modules (5.8.7-10ubuntu1) を設定しています ...
perl (5.8.7-10ubuntu1) を設定しています ...

libnet-daemon-perl (0.38-1) を設定しています ...

libplrpc-perl (0.2017-1) を設定しています ...

libdbi-perl (1.50-1) を設定しています ...
libdbd-mysql-perl (3.0002-2build1) を設定しています ...
mysql-client-5.0 (5.0.22-0ubuntu6.06.2) を設定しています ...
mysql-server-5.0 (5.0.22-0ubuntu6.06.2) を設定しています ...
Stopping MySQL database server: mysqld.
Starting MySQL database server: mysqld.
.

mysql-server (5.0.22-0ubuntu6.06.2) を設定しています ...
root@swisspc:~#

root@swisspc:~# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8 to server version: 5.0.22-Debian_0ubuntu6.06.2-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> select user,host,password from mysql.user;

  1. ------------------+-----------+------------------+
    userhostpassword
  2. ------------------+-----------+------------------+
    rootlocalhost
    rootswisspc
    debian-sys-maintlocalhostxxxxxxxxxxxxxxxx
  3. ------------------+-----------+------------------+
    3 rows in set (0.00 sec)

mysql> set password for root@localhost=password('zzswiss');
Query OK, 0 rows affected (0.01 sec)

mysql> set password for root@swisspc=password('zzswiss');
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from mysql.user;

  1. ------------------+-----------+------------------+
    userhostpassword
  2. ------------------+-----------+------------------+
    rootlocalhostxxxxxxxxxxxxxxxx
    rootswisspcxxxxxxxxxxxxxxxx
    debian-sys-maintlocalhostxxxxxxxxxxxxxxxx
  3. ------------------+-----------+------------------+
    3 rows in set (0.00 sec)

mysql> exit
Bye
root@swisspc:~#

インストール

$ apt-cache search mysql
で調べてみると、mysql-serverとmysql-server-4.1がある。
mysql-serverの方は、5.0.xなので、mysql-server-4.1をインストール。

ついでに、perlのDBIやら、DBD::mysqlもインストールしてくれた。

とりあえず、mysqlのrootのパスワードを設定。

あと、my.cnfのなかで、

bind-address=127.0.0.1
となっているので、

bind-address=192.168.XXX.XXX
と変更。

■MySQL設定

[root@osx ~]# vi /etc/my.cnf ← MySQL設定ファイル編集
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
default-character-set = utf8 ← 追加(MySQLサーバーの文字コードをUTF-8にする)
以下を追加(MySQLクライアントの文字コードをUTF-8にする)
[mysql]
default-character-set = utf8

■MySQL設定
(1)rootユーザへのパスワード設定
MySQLの管理ユーザであるrootユーザ(Linuxのrootユーザとは別)にはデフォルトではパスワードが設定されていないため、安全のためにパスワードを設定する

[root@fedora ~]# mysql -u root ← MySQLサーバーへrootユーザでログイン
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6 to server version: 5.0.21
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> select user,host,password from mysql.user; ← 登録済ユーザ、パスワード確認
+------+-----------------------+----------+
| user | host                  | password |
+------+-----------------------+----------+
| root | localhost             |          | ← 接続元ホストがlocalhostのrootユーザにパスワードが設定されていない
| root | fedora.fedorasrv.com |          | ← 接続元ホストが自ホストのrootユーザにパスワードが設定されていない
|      | localhost             |          |
|      | fedora.fedorasrv.com |          |
+------+-----------------------+----------+
4 rows in set (0.00 sec)
mysql> set password for root@localhost=password('rootパスワード'); ← 接続元ホストがlocalhostのrootユーザにパスワード設定
Query OK, 0 rows affected (0.00 sec)
mysql> set password for root@'fedora.fedorasrv.com'=password('rootパスワード'); ← 接続元ホストが自ホストのrootユーザにパスワード設定

Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from mysql.user; ← 登録済ユーザ、パスワード確認

  1. ------+-----------------------+------------------+
    userhostpassword
  2. ------+-----------------------+------------------+
    | root | localhost | **************** | ← 接続元ホストがlocalhostのrootユーザにパスワードが設定された
    | root | fedora.fedorasrv.com | **************** | ← 接続元ホストが自ホストのrootユーザにパスワードが設定された
    localhost
    fedora.fedorasrv.com
  3. ------+-----------------------+------------------+
    4 rows in set (0.00 sec)

mysql> exit ← MySQLサーバーからログアウト
Bye

[root@fedora ~]# mysql -u root
 ← 接続元ホストがlocalhostのrootユーザでパスワードなしでMySQLサーバーへログインできないことを確認
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@fedora ~]# mysql -u root -h fedora.fedorasrv.com
 ← 接続元ホストが自ホストのrootユーザでパスワードなしでMySQLサーバーへログインできないことを確認
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@fedora ~]# mysql -u root -prootパスワード
 ← 接続元ホストがlocalhostのrootユーザでパスワードありでMySQLサーバーへログインできることを確認
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6 to server version: 5.0.21

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> exit ← MySQLサーバーからログアウト
Bye

[root@fedora ~]# mysql -u root -h fedora.fedorasrv.com -prootパスワード
 ← 接続元ホストが自ホストのrootユーザでパスワードありでMySQLサーバーへログインできることを確認
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6 to server version: 5.0.21

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> exit ← MySQLサーバーからログアウト
Bye

(2)匿名ユーザ削除
MySQLには匿名ユーザ(ユーザ名が空白でパスワードが設定されていないユーザ)がデフォルトで登録されており、ユーザ名とパスワードなしでMySQLサーバーへログインできてしまうため、安全のためにこのユーザを削除する
[root@fedora ~]# mysql -u root -prootパスワード ← rootユーザでMySQLサーバーへログイン
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6 to server version: 5.0.21

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> select user,host from mysql.user; ← 登録ユーザ確認

  1. ------+----------------------+
    userhost
  2. ------+----------------------+
    rootfedora.fedorasrv.com
    fedora.fedorasrv.com
    rootlocalhost
    localhost
  3. ------+----------------------+
    4 rows in set (0.00 sec)

mysql> delete from mysql.user where user=''; ← 匿名ユーザ削除
Query OK, 2 rows affected (0.01 sec)

mysql> select user,host from mysql.user; ← 登録ユーザ確認

  1. ------+----------------------+
    userhost
  2. ------+----------------------+
    rootfedora.fedorasrv.com
    rootlocalhost
  3. ------+----------------------+
    2 rows in set (0.00 sec)

mysql> exit ← ログアウト
Bye

[root@fedora ~]# mysql ← 匿名ユーザでMySQLサーバーへログインできないことを確認
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

※mysql-server-3.23.58-14以降では以下の対処は不要
以下は匿名ユーザ削除後にMySQLの起動が以下のメッセージを出力して失敗することに対する対処
Timeout error occurred trying to start MySQL Daemon.

[root@fedora ~]# mysql -u root -prootパスワード ← rootユーザでMySQLサーバーへログイン
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6 to server version: 5.0.21

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> grant usage on *.* to guest@localhost; ← localhostからのみ接続できる無権限のguestユーザをパスワードなしで登録
Query OK, 0 rows affected (0.05 sec)

mysql> select user,host,password from mysql.user; ← guestユーザ登録確認

  1. -------+-----------------------+------------------+
    userhostpassword
  2. -------+-----------------------+------------------+
    rootlocalhost****************
    rootfedora.fedorasrv.com****************
    guestlocalhost
  3. -------+-----------------------+------------------+
    3 rows in set (0.00 sec)

mysql> select * from mysql.user\G; ← guestユーザの無権限を確認

************************ 3. row ***************************

          Host: localhost
          User: guest
      Password:
   Select_priv: N
   Insert_priv: N
   Update_priv: N
   Delete_priv: N
   Create_priv: N
     Drop_priv: N
   Reload_priv: N
 Shutdown_priv: N
  Process_priv: N
     File_priv: N
    Grant_priv: N

References_priv: N

    Index_priv: N
    Alter_priv: N

3 rows in set (0.00 sec)

mysql> exit ← ログアウト
Bye

[root@fedora ~]# vi /etc/rc.d/init.d/mysqld ← MySQL起動スクリプト修正
ping="/usr/bin/mysqladmin -uUNKNOWN_MYSQL_USER ping"

ping="/usr/bin/mysqladmin -u guest ping" ← MySQL起動確認用ユーザを匿名ユーザからguestユーザへ変更

[root@fedora ~]# /etc/rc.d/init.d/mysqld restart ← MySQLを再起動してみてエラーにならないことを確認
MySQL を停止中: [ OK ]
MySQL を起動中: [ OK ]

(3)testデータベース削除
MySQLにはデフォルトでtestという空のデータベースが登録されているが、不要のため、このデータベースを削除する
[root@fedora ~]# mysql -u root -prootパスワード ← rootユーザでMySQLサーバーへログイン
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6 to server version: 5.0.21

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases; ← 登録データベース確認

  1. --------------------+
    Database
  2. --------------------+
    information_schema
    mysql
    test
  3. --------------------+
    3 rows in set (0.00 sec)

mysql> drop database test; ← testデータベース削除
Query OK, 0 rows affected (0.00 sec)

mysql> show databases; ← 登録データベース確認

  1. --------------------+
    Database
  2. --------------------+
    information_schema
    mysql
  3. --------------------+
    2 rows in set (0.00 sec)

mysql> exit ← ログアウト
Bye