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

最終更新日: 2017.02.18

<<トップページ <<新着情報 <<リンク集 <<サイト内検索 <<Scientific Linuxで自宅サーバー構築 <<Fedoraで自宅サーバー構築

■概要

データベースサーバーは、サーバー上のデータベースをクライアントから操作できるようにするためのサーバー。
ここでは、リレーショナル型データベースのデータベースサーバーであるMariaDBを使用する。

※CentOS7より標準DBがMySQLからMariaDBへ切り替えられた⇒従来のMySQLはこちら


■MariaDBインストール

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

■MariaDB設定

[root@centos ~]# vi /etc/my.cnf.d/server.cnf ← MariaDB設定ファイル編集
[mysqld]
character-set-server = utf8 ← 追加(MariaDBサーバーの文字コードをUTF-8にする)

■MariaDB起動

[root@centos ~]# systemctl start mariadb ← MariaDB起動

[root@centos ~]# systemctl enable mariadb ← MariaDB自動起動設定


■MariaDB初期設定

[root@centos ~]# mysql_secure_installation ← MariaDB初期設定
/usr/bin/mysql_secure_installation: 行 379: find_mysql_client: コマンドが見つか  りません

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, 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 MariaDB
root user without the proper authorisation.

Set root password? [Y/n] ← 空ENTER(rootパスワード設定)
New password: ← 任意のrootパスワードを応答
Re-enter new password: ← 上記と同じパスワードを応答(確認)
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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, MariaDB 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 MariaDB
installation should now be secure.

Thanks for using MariaDB!

■MariaDB確認

[root@centos ~]# mysql -u root -p ← MariaDBへrootでログイン
Enter password: ← MariaDBのrootパスワード応答
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.37-MariaDB MariaDB Server

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant all privileges on test.* to centos@localhost identified by 'centospass';
 ← testデータベースへの全てのアクセス権限を持った、新規ユーザーcentosを登録
Query OK, 0 rows affected (0.00 sec)

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

MariaDB [(none)]> exit ← ログアウト
Bye

[root@centos ~]#  mysql -u centos -pcentospass ← MariaDBへcentosユーザーでログイン
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.37-MariaDB MariaDB Server

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database test; ← testデータベース作成
Query OK, 1 row affected (0.00 sec)

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

MariaDB [(none)]> use test; ← testデータベースへ接続
Database changed
MariaDB [test]> create table test(num int, name varchar(50)); ← testテーブル作成
Query OK, 0 rows affected (0.09 sec)

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

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

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

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

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

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

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

MariaDB [test]> drop table test; ← testテーブル削除
Query OK, 0 rows affected (0.02 sec)

MariaDB [test]> show tables; ← テーブル削除確認
Empty set (0.00 sec)

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

MariaDB [(none)]> show databases; ← データベース削除確認
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.01 sec)

MariaDB [(none)]> exit ← ログアウト
Bye

[root@centos ~]# mysql -u root -p ← MariaDBへrootでログイン
Enter password: ← MariaDBのrootパスワード応答
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.37-MariaDB MariaDB Server

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> revoke all privileges on *.* from centos@localhost; ← centosユーザーから全てのデータベースへのアクセス権限を剥奪
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> delete from mysql.user where user='centos' and host='localhost'; ← centosユーザー削除
Query OK, 1 row affected (0.00 sec)

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

MariaDB [(none)]> flush privileges; ← centosユーザーの削除をMariaDBサーバーへ反映
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit ← ログアウト
Bye



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

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