LoginSignup
1
1

More than 5 years have passed since last update.

CentOS7にmariaDBを入れる

Last updated at Posted at 2017-01-21

参考

yumでインストール

# yum install mariadb mariadb-server



読み込んだプラグイン:fastestmirror, langpacks
base                                                       | 3.6 kB  00:00:00
epel/x86_64/metalink                                       | 5.8 kB  00:00:00
epel                                                       | 4.3 kB  00:00:00
extras                                                     | 3.4 kB  00:00:00
updates                                                    | 3.4 kB  00:00:00
(1/3): epel/x86_64/updateinfo                              | 719 kB  00:00:07
(2/3): updates/7/x86_64/primary_db                         | 2.2 MB  00:00:22
(3/3): epel/x86_64/primary_db                              | 4.5 MB  00:00:33
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.riken.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ mariadb.x86_64 1:5.5.52-1.el7 を インストール
--> 依存性の処理をしています: mariadb-libs(x86-64) = 1:5.5.52-1.el7 のパッケージ: 1:mariadb-5.5.52-1.el7.x86_64
---> パッケージ mariadb-server.x86_64 1:5.5.52-1.el7 を インストール
--> 依存性の処理をしています: perl-DBI のパッケージ: 1:mariadb-server-5.5.52-1.el7.x86_64
--> 依存性の処理をしています: perl-DBD-MySQL のパッケージ: 1:mariadb-server-5.5.52-1.el7.x86_64
--> 依存性の処理をしています: perl(DBI) のパッケージ: 1:mariadb-server-5.5.52-1.el7.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ mariadb-libs.x86_64 1:5.5.50-1.el7_2 を 更新
---> パッケージ mariadb-libs.x86_64 1:5.5.52-1.el7 を アップデート
---> パッケージ perl-DBD-MySQL.x86_64 0:4.023-5.el7 を インストール
---> パッケージ perl-DBI.x86_64 0:1.627-4.el7 を インストール
--> 依存性の処理をしています: perl(RPC::PlServer) >= 0.2001 のパッケージ: perl-DBI-1.627-4.el7.x86_64
--> 依存性の処理をしています: perl(RPC::PlClient) >= 0.2000 のパッケージ: perl-DBI-1.627-4.el7.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ perl-PlRPC.noarch 0:0.2020-14.el7 を インストール
--> 依存性の処理をしています: perl(Net::Daemon) >= 0.13 のパッケージ: perl-PlRPC-0.2020-14.el7.noarch
--> 依存性の処理をしています: perl(Net::Daemon::Test) のパッケージ: perl-PlRPC-0.2020-14.el7.noarch
--> 依存性の処理をしています: perl(Net::Daemon::Log) のパッケージ: perl-PlRPC-0.2020-14.el7.noarch
--> 依存性の処理をしています: perl(Compress::Zlib) のパッケージ: perl-PlRPC-0.2020-14.el7.noarch
--> トランザクションの確認を実行しています。
---> パッケージ perl-IO-Compress.noarch 0:2.061-2.el7 を インストール
--> 依存性の処理をしています: perl(Compress::Raw::Zlib) >= 2.061 のパッケージ: perl-IO-Compress-2.061-2.el7.noarch
--> 依存性の処理をしています: perl(Compress::Raw::Bzip2) >= 2.061 のパッケージ: perl-IO-Compress-2.061-2.el7.noarch
---> パッケージ perl-Net-Daemon.noarch 0:0.48-5.el7 を インストール
--> トランザクションの確認を実行しています。
---> パッケージ perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 を インストール
---> パッケージ perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

==================================================================================
 Package                       アーキテクチャー
                                            バージョン           リポジトリー
                                                                             容量
==================================================================================
インストール中:
 mariadb                       x86_64       1:5.5.52-1.el7       base       8.7 M
 mariadb-server                x86_64       1:5.5.52-1.el7       base        11 M
依存性関連でのインストールをします:
 perl-Compress-Raw-Bzip2       x86_64       2.061-3.el7          base        32 k
 perl-Compress-Raw-Zlib        x86_64       1:2.061-4.el7        base        57 k
 perl-DBD-MySQL                x86_64       4.023-5.el7          base       140 k
 perl-DBI                      x86_64       1.627-4.el7          base       802 k
 perl-IO-Compress              noarch       2.061-2.el7          base       260 k
 perl-Net-Daemon               noarch       0.48-5.el7           base        51 k
 perl-PlRPC                    noarch       0.2020-14.el7        base        36 k
依存性関連での更新をします:
 mariadb-libs                  x86_64       1:5.5.52-1.el7       base       761 k

トランザクションの要約
==================================================================================
インストール  2 パッケージ (+7 個の依存関係のパッケージ)
更新                       ( 1 個の依存関係のパッケージ)

総ダウンロード容量: 22 M
Is this ok [y/d/N]: y
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/10): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm      |  57 kB  00:00:06
(2/10): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm     |  32 kB  00:00:08
(3/10): perl-DBD-MySQL-4.023-5.el7.x86_64.rpm              | 140 kB  00:00:04
(4/10): mariadb-libs-5.5.52-1.el7.x86_64.rpm               | 761 kB  00:00:12
(5/10): perl-Net-Daemon-0.48-5.el7.noarch.rpm              |  51 kB  00:00:01
(6/10): perl-PlRPC-0.2020-14.el7.noarch.rpm                |  36 kB  00:00:00
(7/10): perl-IO-Compress-2.061-2.el7.noarch.rpm            | 260 kB  00:00:06
(8/10): perl-DBI-1.627-4.el7.x86_64.rpm                    | 802 kB  00:00:14
(9/10): mariadb-5.5.52-1.el7.x86_64.rpm                    | 8.7 MB  00:01:28
(10/10): mariadb-server-5.5.52-1.el7.x86_64.rpm            |  11 MB  00:01:37
----------------------------------------------------------------------------------
合計                                                 227 kB/s |  22 MB  01:37
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  更新します              : 1:mariadb-libs-5.5.52-1.el7.x86_64               1/11
  インストール中          : 1:mariadb-5.5.52-1.el7.x86_64                    2/11
  インストール中          : perl-Net-Daemon-0.48-5.el7.noarch                3/11
  インストール中          : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64      4/11
  インストール中          : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64       5/11
  インストール中          : perl-IO-Compress-2.061-2.el7.noarch              6/11
  インストール中          : perl-PlRPC-0.2020-14.el7.noarch                  7/11
  インストール中          : perl-DBI-1.627-4.el7.x86_64                      8/11
  インストール中          : perl-DBD-MySQL-4.023-5.el7.x86_64                9/11
  インストール中          : 1:mariadb-server-5.5.52-1.el7.x86_64            10/11
  整理中                  : 1:mariadb-libs-5.5.50-1.el7_2.x86_64            11/11
  検証中                  : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64       1/11
  検証中                  : 1:mariadb-5.5.52-1.el7.x86_64                    2/11
  検証中                  : 1:mariadb-server-5.5.52-1.el7.x86_64             3/11
  検証中                  : 1:mariadb-libs-5.5.52-1.el7.x86_64               4/11
  検証中                  : perl-PlRPC-0.2020-14.el7.noarch                  5/11
  検証中                  : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64      6/11
  検証中                  : perl-Net-Daemon-0.48-5.el7.noarch                7/11
  検証中                  : perl-DBI-1.627-4.el7.x86_64                      8/11
  検証中                  : perl-IO-Compress-2.061-2.el7.noarch              9/11
  検証中                  : perl-DBD-MySQL-4.023-5.el7.x86_64               10/11
  検証中                  : 1:mariadb-libs-5.5.50-1.el7_2.x86_64            11/11

インストール:
  mariadb.x86_64 1:5.5.52-1.el7        mariadb-server.x86_64 1:5.5.52-1.el7

依存性関連をインストールしました:
  perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7
  perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7
  perl-DBD-MySQL.x86_64 0:4.023-5.el7
  perl-DBI.x86_64 0:1.627-4.el7
  perl-IO-Compress.noarch 0:2.061-2.el7
  perl-Net-Daemon.noarch 0:0.48-5.el7
  perl-PlRPC.noarch 0:0.2020-14.el7

依存性を更新しました:
  mariadb-libs.x86_64 1:5.5.52-1.el7

完了しました!

mariaDBの存在を確認

# rpm -qa | grep maria


rpm -qa | grep maria
mariadb-libs-5.5.52-1.el7.x86_64
mariadb-5.5.52-1.el7.x86_64
mariadb-server-5.5.52-1.el7.x86_64

mariaDBの設定ファイル

mariaDBの設定ファイルは/etc/my.cnfになり/etc/my.cnf.d以下の設定ファイルがインクルードされる形となっています。この中で/etc/my.cnfを編集し文字コードを設定しておきます。

スクリーンショット 2017-01-21 10.55.25.png


character-set-server=utf8

//追加箇所

スクリーンショット 2017-01-21 10.58.58.png

systemctlコマンドでmariadbを有効にする

systemctlコマンドでmariadbを有効にしてから起動します。 (有効にしてからでないと起動しませんでした)

# systemctl enable mariadb.service

Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

mariaDB起動

# systemctl start mariadb.service

MariaDBの初期設定

続いてMariaDBの初期設定を行います。初期セットアップコマンドがあり対話形式に進めていきますので、それを実行します。rootパスワードを設定するところのみ入力する必要がありますが、その他についてはそのままEnterで大丈夫です。

# mysql_secure_installation

パスワードの設定要求

Enter current password for root (enter for none):
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] Y
New password:
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]
 ... 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]
 ... 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]
 - 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]
 ... 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にログイン

MariaDBにログインします

下記では最初に設定したrootユーザでログインしています。

# mysql -u root -p

MariaDBでユーザ作成

データベース操作を行うには以下のようにコマンドを実施します。

mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]>

インストール直後のユーザ一覧を表示するSQL文

インストール直後のユーザ一覧を表示するSQL文が以下となります。初期ではrootのみですがホスト名は複数登録されています。MariaDBのユーザはユーザ名@ホスト名がセットとなり一意に識別されます。ただ、ホスト名はいずれも自ホストのことなので自身から接続してくるrootユーザということになります。

MariaDB [(none)]>  select user,host,user from mysql.user;
+------+-----------+------+
| user | host      | user |
+------+-----------+------+
| root | 127.0.0.1 | root |
| root | ::1       | root |
| root | localhost | root |
+------+-----------+------+
3 rows in set (0.00 sec)

MariaDBでデータベース操作

以下のコマンドでデータベースtestを作成します。


MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)

作成したデータベースを確認してみます。



MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

指定のデータベースに移動し、テーブルの作成等を行ってみます。

MariaDB [(none)]> use test;
Database changed
MariaDB [test]>

MariaDB [test]> create table test(num int,name varchar(50));
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test           |
+----------------+
1 row in set (0.00 sec)

作成したテーブルにデータを登録してみます。

testのテーブルに「ユニックスユーザ1」という値をインサートする

MariaDB [test]> insert into test values(1,'ユニックスユーザ1');
Query OK, 1 row affected (0.00 sec)

テーブルにデータが入ったか確認する

MariaDB [test]> select * from test;
+------+---------------------------+
| num  | name                      |
+------+---------------------------+
|    1 | ユニックスユーザ1         |
+------+---------------------------+
1 row in set (0.00 sec)

登録したデータを更新する

update test set name='ユニックスユーザ10' where num=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
select * from test;
+------+----------------------------+
| num  | name                       |
+------+----------------------------+
|    1 | ユニックスユーザ10         |
+------+----------------------------+
1 row in set (0.00 sec)

テーブルに登録されたデータを削除し、削除されたのを確認

delete from test where num=1;
Query OK, 1 row affected (0.00 sec)
select * from test;
Empty set (0.00 sec)

さらにテーブル自身を削除してみます。showコマンドでみたときにEmptyとなっていることを確認します。

MariaDB [test]> drop table test
    -> ;
Query OK, 0 rows affected (0.00 sec)

DB自体を消去する

ariaDB [test]> drop database test;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1