はじめての自宅サーバ構築 - Fedora/CentOS -
Last Update 2018/05/30
[ 更新履歴 ] [ サイト マップ ] [ 質問掲示板 ] [ 雑談掲示板 ] [ リンク ]
トップ >> データベースサーバ >> データベースサーバの構築(MySQL)
動作確認 [ FC1 / FC2 / FC3 / FC4 / FC5 / FC6 / Fedora7 / Fedora8 / Fedora9 / Fedora10 / Fedora11 / Fedora12 / Fedora13 / Fedora14 / Fedora15 / Fedora16 / Fedora17 / CentOS4 / CentOS5 / CentOS6 ]
  1. MySQLとは・・・
  2. MySQLのインストール
  3. MySQLの設定
  4. MySQLの起動
  5. MySQLの初期設定
  6. データベースを操作してみる
    データベースの作成
    データベースを操作するユーザの作成
    テーブル/フィールドの作成
    レコードの挿入
    レコードの削除
    レコードの更新
  7. こんな時は・・・

■ MySQLとは・・・

MySQLとは世界的に広く利用されているRDBMSで、マルチスレッドをサポートし数千万のレコードを持つデータベースも高速に処理を行えます。
また、CGIやJava・PHPなどの言語よりMySQLに手軽にアクセスが可能です。

■ MySQLのインストール

# yum -y install mysql-server

■ MySQLの設定

MySQL設定ファイルの変更
# 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
出力メッセージを日本語にする(追加)
注:システムロケールを UTF-8 以外にしてある場合、文字化けする場合があります。
   下記は特に追加する必要はありません。(お好みでどうぞ)
language=/usr/share/mysql/japanese/

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

■ MySQLの起動

MySQLの起動
【FC1 から Fedora15 / CentOS4 / CentOS5 / CentOS6 の場合】
# /etc/rc.d/init.d/mysqld start
OS起動時にMySQLを起動する
# chkconfig mysqld on

【Fedora16以降 の場合】
# systemctl start mysqld.service
OS起動時にMySQLを起動する
# systemctl enable mysqld.service

■ MySQLの初期設定

MySQL monitorに接続
# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.13 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)


「mysql」データベースのテーブル名表示
mysql> show tables from mysql;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)

MySQLアカウントのrootが自動的に作成されているがパスワードが設定されていないので設定する。
rootにパスワードを設定する(パスワードを"himitsu"とする場合)
mysql> SET PASSWORD FOR root@localhost=PASSWORD('himitsu');
Query OK, 0 rows affected (0.00 sec)

MySQL monitorの終了
mysql> exit
Bye


MySQL monitorにrootで接続する(この時、"-p"オプションを指定しパスワード入力をする)
# mysql -u root -p
上記で設定したパスワード"himitsu"を入力
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.13 MySQL Community Server (GPL)

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

「use」コマンドで「mysql」データベースに切り替える。
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に接続できるユーザの表示
mysql> select host,user,password from user;
+---------------------+------+-------------------------------------------+
| host                | user | password                                  |
+---------------------+------+-------------------------------------------+
| localhost           | root | *E66CFAC34A241C069A88922EFA68D1428A99AE945  |
| fedora.kajuhome.com | root |                                           |
| 127.0.0.1           | root |                                           |
| ::1                 | root |                                           |
| localhost           |      |                                           |
| fedora.kajuhome.com |      |                                           |
+---------------------+------+-------------------------------------------+
6 rows in set (0.00 sec)

パスワード無しのユーザ(匿名ユーザ)の削除
mysql> delete from user where user="";
Query OK, 2 rows affected (0.00 sec)

MySQLに接続できるユーザの表示
mysql> select host,user,password from user;
+---------------------+------+-------------------------------------------+
| host                | user | password                                  |
+---------------------+------+-------------------------------------------+
| localhost           | root | *E66CFAC34A241C069A88922EFA68D1428A99AE94 |
| fedora.kajuhome.com | root |                                           |
| 127.0.0.1           | root |                                           |
| ::1                 | root |                                           |
+---------------------+------+-------------------------------------------+
4 rows in set (0.00 sec)


パスワード無しのユーザが削除されたホスト「fedora.kajuhome.com」にもパスワードを設定する
mysql> SET PASSWORD FOR root@fedora.kajuhome.com=PASSWORD('himitsu');
Query OK, 0 rows affected (0.00 sec)

MySQLに接続できるユーザの表示
mysql> select host,user,password from user;
+---------------------+------+-------------------------------------------+
| host                | user | password                                  |
+---------------------+------+-------------------------------------------+
| localhost           | root | *E66CFAC34A241C069A88922EFA68D1428A99AE94 |
| fedora.kajuhome.com | root | *E66CFAC34A241C069A88922EFA68D1428A99AE94 |
| 127.0.0.1           | root |                                           |
| ::1                 | root |                                           |
+---------------------+------+-------------------------------------------+
4 rows in set (0.00 sec)

「use」コマンドで「test」データベースに切り替える。
mysql> use test
Database changed
mysql> show tables;
Empty set (0.00 sec)

「test」データベースのテーブル名表示
mysql> show tables;
Empty set (0.00 sec)

テーブルが存在しない(「test」データベースは試験用の為不要)「test」データベースの削除
mysql> drop database test;
Query OK, 0 rows affected (0.01 sec)

データベースの表示
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

「test」データベースが削除されたMySQL monitorの終了
mysql> exit
Bye

■ データベースを操作してみる

MySQL monitorに接続
# 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.5.13 MySQL Community Server (GPL)

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

「meibo」データベースを作成
mysql> create database meibo;
Query OK, 1 row affected (0.00 sec)

データベースの表示
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| meibo              |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
「meibo」データベースが作成された

「meibo」データベースを操作する"linux"ユーザの作成
mysql> grant all on meibo.* TO linux@localhost identified BY 'himitsu';
Query OK, 0 rows affected (0.00 sec)
上記は「meibo」データベースにローカルからの接続ですべての操作を許す
ユーザ"lunux"をパスワード"himitsu"で作成しています

設定を反映する
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

作成した"linux"ユーザで操作できるか、ログインし直す
MySQL monitorの終了
mysql> exit
Bye

"linux"ユーザでログイン
# mysql -u linux -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.13 MySQL Community Server (GPL)

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

「meibo」データベースに「user」テーブルを作成
(この時、「Num」と「Name」フィールドを作成する)
mysql> create table meibo.user (
    -> Num int NOT NULL primary key auto_increment,
    -> Name varchar(32) NOT NULL
    -> );
Query OK, 0 rows affected (0.06 sec)

「meibo」データベースの「user」テーブルを表示
mysql> show fields from meibo.user;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| Num   | int(11)     | NO   | PRI | NULL    | auto_increment |
| Name  | varchar(32) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
「user」テーブルが作成され、指定したフィールドも作成された

レコードの挿入(値を"linux"にする)
mysql> insert into meibo.user values(NULL,"linux");
Query OK, 1 row affected (0.00 sec)

レコードの表示
mysql> select * from meibo.user;
+-----+-------+
| Num | Name  |
+-----+-------+
|   1 | linux |
+-----+-------+
1 row in set (0.00 sec)
"linux"のレコードが登録された

レコードの挿入(値を"hogehoge"にする)
mysql> insert into meibo.user values(NULL,"hogehoge");
Query OK, 1 row affected (0.01 sec)

レコードの表示
mysql> select * from meibo.user;
+-----+----------+
| Num | Name     |
+-----+----------+
|   1 | linux    |
|   2 | hogehoge |
+-----+----------+
2 rows in set (0.00 sec)
"hogehoge"のレコードが登録された

レコードの削除(キー「Num」が2のレコードを削除)
mysql> delete from meibo.user where Num=2;
Query OK, 1 row affected (0.01 sec)

レコードの表示
mysql> select * from meibo.user;
+-----+-------+
| Num | Name  |
+-----+-------+
|   1 | linux |
+-----+-------+
1 row in set (0.00 sec)
"hogehoge"のレコードが削除された

再度、レコードの挿入(値を"hogehoge"にする)
mysql> insert into meibo.user values(NULL,"hogehoge");
Query OK, 1 row affected (0.00 sec)

レコードの表示
mysql> select * from meibo.user;
+-----+----------+
| Num | Name     |
+-----+----------+
|   1 | linux    |
|   3 | hogehoge |
+-----+----------+
2 rows in set (0.00 sec)
"hogehoge"のレコードが登録された

レコードの更新(「Name」が"hogehoge"のレコードを"fedora"に変更)
mysql> update meibo.user set Name="fedora" where Name="hogehoge";
Query OK, 1 row affected (0.00 sec)
一致数(Rows matched): 1  変更: 1  Warnings: 0

レコードの表示
mysql> select * from meibo.user;
+-----+--------+
| Num | Name   |
+-----+--------+
|   1 | linux  |
|   3 | fedora |
+-----+--------+
2 rows in set (0.00 sec)
"hogehoge"のレコードが"fedora"に変更された

MySQL monitorの終了
mysql> exit
Bye

■ こんな時は・・・

サービスを起動した時に以下のメッセージが出力された場合、新しい MySQL 権限テーブルを生成すれば解決できる場合があります。

「mysql-server」起動時のログ

 Table 'mysql.host' doesn't exist

テーブル生成
# mysql_install_db

■ コンテンツ関連

■ その他

ページ先頭へ

Copyright(©)2004-2018 First home server construction. All Right Reserved.