データベースサーバー構築(MySQL)

最終更新日: 2014.02.19

<<トップページ <<新着情報 <<逆引き集 <<アンケート <<リンク集 <<サイト内検索 <<管理人へメール <<CentOSで自宅サーバー構築 <<Scientific Linuxで自宅サーバー構築

■概要

データベースサーバーは、サーバー上に作成したデータベースをデータベース管理システムを介してクライアントから操作できるようにするためのサーバー。
ここでは、フリーのリレーショナルデータベースサーバーであるMySQLを使用する。


■MySQLインストール

[root@fedora ~]# yum -y install mysql-server ← mysql-serverインストール

■MySQL設定

[root@fedora ~]# vi /etc/my.cnf ← MySQL設定ファイル編集
[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 ← 追加(MySQLサーバーの文字コードをUTF-8にする)

■MySQL起動

[root@fedora ~]# /etc/rc.d/init.d/mysqld start ← MySQL起動
Starting mysqld (via systemctl):                           [  OK  ]

[root@fedora ~]# chkconfig mysqld on ← MySQL自動起動設定


■MySQL初期設定

[root@fedora ~]# mysql_secure_installation ← MySQL初期設定




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):  ← 空ENTER
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]  ← 空ENTER(rootパスワード設定)
New password:  ← rootパスワード応答
Re-enter new password:  ← rootパスワード応答(確認)
Password updated successfully!
Reloading privilege tables..
 ... Success!


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]  ← 空ENTER(匿名ユーザー削除)
 ... 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]  ← 空ENTER(リモートからのrootログイン禁止)
 ... 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]  ← 空ENTER(testデータベース削除)
 - 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]  ← 空ENTER
 ... 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!

■MySQL確認

[root@fedora ~]# mysql -u root -p ← MySQLへrootでログイン
Enter password:  ← 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> grant all privileges on test.* to fedora@localhost identified by 'fedorapass';
 ← testデータベースへの全てのアクセス権限を持った、新規ユーザfedoraを登録
Query OK, 0 rows affected (0.00 sec)

mysql> select user from mysql.user where user='fedora'; ← fedoraユーザ登録確認
+--------+
| user   |
+--------+
| fedora |
+--------+
1 row in set (0.00 sec)

mysql> exit ← ログアウト
Bye

[root@fedora ~]# mysql -u fedora -pfedorapass ← fedoraユーザで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> create database test; ← testデータベース作成
Query OK, 1 row affected (0.00 sec)

mysql> show databases; ← データベース作成確認
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)

mysql> use test ← testデータベースへ接続
Database changed

mysql> create table test(num int, name varchar(50)); ← testテーブル作成
Query OK, 0 rows affected (0.01 sec)

mysql> show tables; ← テーブル作成確認
+----------------+
| Tables_in_test |
+----------------+
| test           |
+----------------+
1 row in set (0.00 sec)

mysql> insert into test values(1,'山田太郎'); ← testテーブルへデータ登録
Query OK, 1 row affected (0.00 sec)

mysql> select * from test; ← データ登録確認
+------+----------+
| num  | name     |
+------+----------+
|    1 | 山田太郎 |
+------+----------+
1 row in set (0.00 sec)

mysql> update test set name='山田次郎'; ← testテーブル内データ更新
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test; ← データ更新確認
+------+----------+
| num  | name     |
+------+----------+
|    1 | 山田次郎 |
+------+----------+
1 row in set (0.01 sec)

mysql> delete from test where num=1; ← testテーブル内データ削除
Query OK, 1 row affected (0.03 sec)

mysql> select * from test; ← データ削除確認
Empty set (0.00 sec)

mysql> drop table test; ← testテーブル削除
Query OK, 0 rows affected (0.00 sec)

mysql> show tables; ← テーブル削除確認
Empty set (0.00 sec)

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

mysql> show databases; ← データベース削除確認
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)

mysql> exit ← ログアウト
Bye

[root@fedora ~]# mysql -u root -p ← MySQLへrootでログイン
Enter password:  ← 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> revoke all privileges on *.* from fedora@localhost; ← fedoraユーザから全てのデータベースへのアクセス権限を剥奪
Query OK, 0 rows affected (0.00 sec)

mysql> delete from mysql.user where user='fedora' and host='localhost'; ← fedoraユーザ削除
Query OK, 1 row affected (0.01 sec)

mysql> select user from mysql.user where user='fedora'; ← fedoraユーザ削除確認
Empty set (0.00 sec)

mysql> flush privileges; ← fedoraユーザの削除をMySQLサーバーへ反映
Query OK, 0 rows affected (0.01 sec)

mysql> exit ← ログアウト
Bye




▲このページのトップへ戻る

LPIロゴ Copyright© 2012 fallenangels, All rights reserved.
ご自由にリンクしてください(連絡は不要です)
本ページへのご意見・ご要望、誤字・脱字・リンク切れ等のご連絡はこちらからお願いします