インストール - MariaDB

提供:MochiuWiki - SUSE, Electronic Circuit, PCB
2020年4月9日 (木) 10:39時点におけるWiki (トーク | 投稿記録)による版 (→‎MariaDBのインストール)
ナビゲーションに移動 検索に移動

概要

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


MariaDBのインストール

パッケージ管理システムからインストール

MariaDBをインストールするため、以下のコマンドを実行する。

sudo zypper install mariadb mariadb-tools


正常にインストールされているか確認する。

mariadb –version


リポジトリを追加してインストール

次のコマンドを実行して、MariaDBのリポジトリを追加する。

# openSUSE 15の場合
sudo zypper addrepo --gpgcheck --refresh https://yum.mariadb.org/10.5.2/opensuse/15/x86_64/ mariadb

# SLE 15の場合
sudo zypper addrepo --gpgcheck --refresh https://yum.mariadb.org/10.5.2/sles/15/x86_64/ mariadb


次に、リポジトリのGPGキーを取得する。

sudo rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
sudo zypper --gpg-auto-import-keys refresh


リポジトリを追加および設定を行った後、以下のコマンドを実行してMariaDBをインストールする。
(追加したリポジトリには、MariaDBサーバ / クライアントの他に、ユーティリティやライブラリ、プラグイン等、関連するソフトウェアも含まれる)

sudo zypper install MariaDB-server MariaDB-client galera MariaDB-shared MariaDB-backup MariaDB-common



MariaDBの設定

MariaDBのセキュリティを向上させるため、以下のコマンドを実行する。

sudo mysql_secure_installation


下記の説明が表示されるので、翻訳付きで説明する。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
注意: 実運用環境のすべての MariaDB サーバにおいて、当スクリプトのすべての処理を実行することが推奨されます!
各手順をよくお読みください!

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.
MariaDBにログインしてそれを安全にするには、rootユーザの現在のパスワードが必要です。
MariaDBをインストールした直後でrootパスワードをまだセットしていない場合、パスワードは空白であるため、そのままEnterキーを押してください。

Enter current password for root (enter for none): 
root の現在のパスワードを入力してください(未設定なら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.
root パスワードを設定することで、適切な認証なしではMariaDBのrootユーザにログインできなくなります。

You already have a root password set, so you can safely answer 'n'.
すでにrootパスワードがセットされているので、「n」と回答しても安全です。

Change the root password? [Y/n] n
root パスワードを変更しますか?
... skipping.
... スキップ

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.
既定で、MariaDBのインストール作業においては匿名ユーザを利用することができ、ユーザアカウントの作成なしで誰でもログインすることが
できます。これは、インストールを若干スムーズに行うために存在し、テストのみで利用することが想定されています。
これらは、実運用環境に移行する前に削除する必要があります。

Remove anonymous users? [Y/n] y
匿名ユーザを削除しますか?
... Success!
... 成功しました!

Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at
the root password from the network.
通常、root ログインはlocalhostからの接続のみで許可されるべきです。それにより、ネットワークからrootパスワードを推測できないようになります。

Disallow root login remotely? [Y/n] y
リモートからの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.
既定で、MariaDBには、だれでもアクセス可能なtestという名前のデータベースが付属しています。
これもテストのみで利用されることが想定されており、実運用環境に移行する前に削除する必要があります。

Remove test database and access to it? [Y/n] y
testデータベースとそのデータベースへのアクセスを削除しますか?
- Dropping test database...
- testデータベースを削除しています...
... Success!
... 成功しました!
- Removing privileges on test database...
- testデータベースに対する権限を削除しています...
... Success!
... 成功しました!

Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
権限テーブルを再読み込みすると、ここまでのすべての変更をすぐに反映させることができます。

Reload privilege tables now? [Y/n] y
今すぐ権限テーブルを再読み込みしますか?
... Success!
... 成功しました!

Cleaning up...
クリーンアップしています...

All done!  If you've completed all of the above steps, your MariaDB installation should now be secure.
完了しました! 以上のすべての処理を実行していれば、安全にMariaDBをインストールできているはずです。

Thanks for using MariaDB!
MariaDBのご利用ありがとうございます!

MariaDBのユーザ登録

MariaDBにrootユーザで接続するため、以下のコマンドを実行する。

mysql -u root -p


上記で設定したMariaDBのrootパスワードを入力する。

Enter password:


ユーザ情報の一覧表示をするには、以下のクエリを発行する。

select user, host, password from mysql.user;


データベースの一覧表示をするには、以下のクエリを発行する。

show databases;


まず、全てのデータベースへの全てのアクセス権限を持った、新規ユーザsuseを登録する。

grant all privileges on *.* to suse@localhost identified by 'パスワード';


suseユーザの登録が成功したか確認する。

select user from mysql.user where user='centos';


suseユーザの登録をMariaDBサーバへ反映する。

flush privileges;


MariaDBからログアウトする。

exit



MariaDBにおけるデータベースの作成

suseユーザでMariaDBへログインする。

mysql -u centos -p 3ulank8y


D_Sampleデータベースを作成する。

create database D_Sample;


D_Sample用データベースが作成できたか確認する。

show databases;


D_Sampleデータベースへ接続する。

use D_Sample;


T_Employeeテーブルを作成する。

create table T_Employee(employee_no int, first_name varchar(50), last_name varchar(50));


T_Employeeテーブルが作成できたか確認する。

show tables;


T_Employeeテーブルにレコードを登録する。

insert into T_Employee values(1, '太郎', ‘山田’);


MariaDBからログアウトする。

exit



登録したユーザの削除

suseユーザを削除する。

delete from mysql.user where user='suse' and host='localhost';


suseユーザが削除されたか確認する。

select user from mysql.user where user='suse';


suseユーザの削除をMariaDBサーバへ反映する。

flush privileges;


MariaDBからログアウトする。

exit



ユーザの権限を削除

suseユーザから全てのデータベースへのアクセス権限を剥奪する。

revoke all privileges on *.* from suse@localhost;



MariaDBのアンインストール

まず、MariaDBを停止する。
MariaDBのステータスを確認して、起動していたら停止する。

sudo systemctl status mariadb
sudo systemctl stop mariadb


MariaDBをアンインストールする。

sudo zypper remove mariadb mariadb-client mariadb-errormessages


最後に、不要なファイルを削除する。
下記に示す通り、削除されずに残っているファイルやディレクトリがあるので、手動で削除する。

  • /var/lib/mysql
    デフォルトのデータファイル、ログファイルの格納先
  • /etc/my.cnf.d
    オプションファイルの格納先
  • /data
    本環境では、このディレクトリにデータファイル、ログファイルの格納先を変更しているので、手動で削除する
sudo rm -rf /var/lib/mysql
sudo rm -rf /etc/my.cnf.d
sudo rm -rf /data