LinuxへのMariaDBのインストールとデータベース作成

Linux上でMariaDBをインストールする手順とデータベース作成手順を紹介します。

MariaDBとは

  • MySQLを元に作成されてオープンソースデータベース
  • 代表的なLinuxのディストリビューションでは標準データベースになりつつある
  • GoogleやWikipediaなどもMariaDBを採用している

これからのデファクトスタンダードになるデータベースです。

目次

インストール環境

今回のインストール環境は以下になります。

  • CentOS:CentOS Linux release 7.4.1708 (Core)
  • mariadb:1:5.5.52-1.el7
  • mariadb-server:1:5.5.52-1.el7

インストール手順

今回はyumを使用してMariaDBをインストールします。

yum install mariadb-server 

いろいろなブログでインストール手順を確認すると、

yum install mariadb mariadb-server 

と紹介されていましたが、

mariadb-serverをインストールするだけでMariaDBに必要なコンポ―ネントはインストールできます。

MariaDBの物理構成ファイルについては調査中です。

↓が実行ログになります。

※一部省略しています。

[root@centos7-local ~]$ yum install mysql-server
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.fairway.ne.jp
* epel: mirror.dmmlabs.jp
* extras: www.ftp.ne.jp
* ius: hkg.mirror.rackspace.com
* updates: mirror.fairway.ne.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ mariadb-server.x86_64 1:5.5.56-2.el7 を インストール
--> 依存性の処理をしています: mariadb(x86-64) = 1:5.5.56-2.el7 のパッケージ: 1:mariadb-server-5.5.56-2.el7.x86_64
--> 依存性の処理をしています: perl-DBI のパッケージ: 1:mariadb-server-5.5.56-2.el7.x86_64
--> 依存性の処理をしています: perl-DBD-MySQL のパッケージ: 1:mariadb-server-5.5.56-2.el7.x86_64
--> 依存性の処理をしています: perl(Data::Dumper) のパッケージ: 1:mariadb-server-5.5.56-2.el7.x86_64
--> 依存性の処理をしています: perl(DBI) のパッケージ: 1:mariadb-server-5.5.56-2.el7.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ mariadb.x86_64 1:5.5.56-2.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-Data-Dumper.x86_64 0:2.145-3.el7 を インストール
--> トランザクションの確認を実行しています。
---> パッケージ 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-server x86_64 1:5.5.56-2.el7 base 11 M
依存性関連でのインストールをします:
mariadb x86_64 1:5.5.56-2.el7 base 8.7 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-Data-Dumper x86_64 2.145-3.el7 base 47 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

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

総ダウンロード容量: 21 M
インストール容量: 110 M
Is this ok [y/d/N]: y
Downloading packages:
--------------------------------------------------------------------------------------------------------------
合計 1.2 MB/s | 21 MB 00:00:17
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : perl-Data-Dumper-2.145-3.el7.x86_64
検証中 : perl-DBI-1.627-4.el7.x86_64 1/10
検証中 : 1:mariadb-5.5.56-2.el7.x86_64 10/10

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

依存性関連をインストールしました:
mariadb.x86_64 1:5.5.56-2.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-Data-Dumper.x86_64 0:2.145-3.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の初期設定

インストールが完了したらMariaDBの初期設定を行います。

初期設定は

mysql_secure_installation 

コマンドで行います。

↓が実行ログになります。

途中、対話式で設定項目を求められます。

★の箇所に注意してください。

[root@centos7-local ~]$ mysql_seurucure_installation

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
New password: ★ MariaDBに”設定する”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
... 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
- 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のインストールは完了です。お疲れ様です!

MariaDBにデータベースを作成

MariaDBでデータベースを作成します。手順の流れは以下になります。

  1. rootでログイン
  2. データベース作成
  3. データベース作成ユーザの作成と権限の付与

↓がデータベース作成コマンドになります。

# mysql -u root -p
> create database <データベース名> character set utf8;
> grant all privileges on <データベース名>.* to <ユーザ名>@localhost identified by '<ログインパスワード>';

↓が実行ログです。

[root@centos7-local ~]$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.56-MariaDB MariaDB Server

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

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

MariaDB [(none)]> create database testdb character set utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all privileges on testdb.* to testdb_user@localhost identified by 'testdb_user';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>
MariaDB [(none)]>
MariaDB [(none)]> exit
Bye
[root@centos7-local ~]$
[root@centos7-local ~]$ mysql -u testdb_user -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
show
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases; 
+--------------------+
| Database |
+--------------------+
| information_schema |
| testdb |
+--------------------+
2 rows in set (0.00 sec)

補足②:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)が出力された時の対処法

これはMariaDBが起動していない時に出力されることが多いようです。

対象方法はMariaDBを起動すればよいのです。

↓が手順になります。

systemctl enable mariadb.service
systemctl start mariadb.service

要はサービスに登録してあげて、MariaDBのサービスを起動させればよいのです。

[root@centos7-local ~]$ systemctl enable mariadb.service
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
[root@centos7-local ~]$ systemctl start mariadb.service
[root@centos7-local ~]$ systemctl list-units | grep mariadb
mariadb.service loaded active running MariaDB database server
[root@centos7-local ~]$ systemctl status mariadb
mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled)
Active: active (running) since Sun 2018-02-04 22:49:44 JST; 9min ago
Process: 2726 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 2645 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 2725 (mysqld_safe)
CGroup: /system.slice/mariadb.service
|-2725 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
`-2886 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql...

Feb 04 22:49:41 centos7-local.com mariadb-prepare-db-dir[2645]: MySQL manual for more instructions.
Feb 04 22:49:41 centos7-local.com mariadb-prepare-db-dir[2645]: Please report any problems at http://mari...ra
Feb 04 22:49:41 centos7-local.com mariadb-prepare-db-dir[2645]: The latest information about MariaDB is a.../.
Feb 04 22:49:41 centos7-local.com mariadb-prepare-db-dir[2645]: You can find additional information about...t:
Feb 04 22:49:41 centos7-local.com mariadb-prepare-db-dir[2645]: http://dev.mysql.com
Feb 04 22:49:41 centos7-local.com mariadb-prepare-db-dir[2645]: Consider joining MariaDB's strong and vib...y:
Feb 04 22:49:41 centos7-local.com mariadb-prepare-db-dir[2645]: https://mariadb.org/get-involved/
Feb 04 22:49:42 centos7-local.com mysqld_safe[2725]: 180204 22:49:42 mysqld_safe Logging to '/var/log/ma...g'.
Feb 04 22:49:42 centos7-local.com mysqld_safe[2725]: 180204 22:49:42 mysqld_safe Starting mysqld daemon ...sql
Feb 04 22:49:44 centos7-local.com systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.

MariaDB & MySQL全機能バイブルposted with ヨメレバ

鈴木啓修/山田奈緒子 技術評論社 2015年01月

Amazon

Kindle

楽天ブックス

7net

honto

e-hon

紀伊國屋書店

よかったらシェアしてね!
  • URLをコピーしました!
目次