さくらのVPS導入手順 8.MySQLの利用準備

MySQLのインストール

さくらのVPSでは初期状態でMySQLもインストールされていません
MySQLをyumコマンドでインストールします

$ sudo yum -y install mysql-server

2014年1月の段階ではインストールされるのは5.1.71になるようです
こちらも最新は5.5系なのでapache同様一つ前のものになりますが、特殊なものを入れなければ特に問題は起きないでしょう
そこまで深く使いこなす人は、初期設定にここの記事など読んでいないと思いますので……

$ mysql --version
mysql  Ver 14.14 Distrib 5.1.71, for redhat-linux-gnu (x86_64) using readline 5.1

設定ファイルの編集

では、こちらも同じようにまずは設定ファイルのバックアップを取った後で編集を行います

$ sudo cp /etc/my.cnf /etc/my.cnf.original
$ sudo vi /etc/my.cnf

ハイライトされている行を追加してください

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

character-set-server=utf8
skip-character-set-client-handshake

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqldump]
default-character-set=utf8

サービスの起動

ではサービスを起動してみましょう
いっぱい説明文が出ますが、[Warning]とかが特に出ず、最後がOKになればほぼ大丈夫

$ sudo service mysqld start
MySQL データベースを初期化中:  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:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h www4380ue.sakura.ne.jp password 'new-password'

Alternatively you can run:
/usr/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 /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

                                                           [  OK  ]
mysqld を起動中:                                           [  OK  ]

他のサイトを参考にして設定すると、以下のような警告が出るケースもありますが、[mysqld]中の”–default-character-set”を”–character-set-server”に変更してください

140127 00:00:00 [Warning] '--default-character-set' is deprecated and will be removed in a future release. Please use '--character-set-server' instead.
OK

サービスの登録

では、ここでも同じようにサービスを登録します

$ sudo chkconfig mysqld on
$ chkconfig --list mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

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

次に、起動したときの説明文でも注意を受けていましたが、このままではMySQL用のrootユーザーにパスワードが設定されていない状態なので、それを設定しておきましょう
以下の”で囲まれた部分に任意のパスワードを設定してください

$ sudo mysqladmin -u root password '任意のパスワード'

成功したら、早速そのパスワードを使ってログインしてみます

$ mysql -u root -p
Enter password:(先ほど設定したパスワード)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.71 Source distribution

Copyright (c) 2000, 2013, 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>

このような表示になればログイン成功です

不要なユーザーの削除

最初に自動的に作られるユーザーとデータベースに不要なものが含まれていますので、それもここで削除しておきましょう
ハイライトしている行がコマンドの入力になります
1行目でmysqlクライアントを起動します
15行目で使用するデータベースを指定しています
20行目で、今登録されているユーザーを表示してみています。パスワードは皆さんそれぞれ、またhostのwww0000xの部分は皆さんのサーバー名になっていると思います
32行目で、そのうちの不要なものを削除し
35行目で再表示して不要なものが消えたことを確認しています

$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.1.71 Source distribution

Copyright (c) 2000, 2013, 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> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SELECT user, password, host FROM user;
+------+-------------------------------------------+-----------------------+
| user | password                                  | host                  |
+------+-------------------------------------------+-----------------------+
| root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | localhost             |
| root |                                           | www0000x.sakura.ne.jp |
| root |                                           | 127.0.0.1             |
|      |                                           | localhost             |
|      |                                           | www0000x.sakura.ne.jp |
+------+-------------------------------------------+-----------------------+
5 rows in set (0.00 sec)

mysql> DELETE FROM user WHERE user = '' OR ( user = 'root' AND host != 'localhost' );
Query OK, 4 rows affected (0.00 sec)

mysql> SELECT user, password, host FROM user;
+------+-------------------------------------------+-----------+
| user | password                                  | host      |
+------+-------------------------------------------+-----------+
| root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | localhost |
+------+-------------------------------------------+-----------+
1 row in set (0.00 sec)

不要なデータベースの削除

続けて不要なデータベースを削除しておきます
データベースの一覧を表示させると、初期状態では3つのデータベースが用意されていることが分かります

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

このうちtestデータベースは使うことはないので、削除してしまいましょう

mysql> DROP DATABASE test;
Query OK, 0 rows affected (0.02 sec)

これで消えました

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.00 sec)

次は さくらのVPS導入手順 9.WordPressの利用準備

はじめに戻る さくらのVPS導入手順 0.はじめに

↓↓↓ブログランキングに参加しています↓↓↓

にほんブログ村 IT技術ブログへ

↑↑↑応援よろしくお願いします↑↑↑

■ AD ■

コメントを残す

メールアドレスが公開されることはありません。